Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I enable a text field when a button is clicked?
    text
    copied!<p>before I start, I'm a beginner programmer.</p> <p>How can I enable a text field when a button is clicked.</p> <p>I have two frames, one that has the JFields and the other for the exception. When the exception occurs > <strong>setEditable(false)</strong> but what statement should I make to enable the JFields once the user click on okay button -that i've made in the exception-? </p> <p>I've tried to add static boolean to exception frame, and inside the action performed of this class I initialized that boolean to <strong>true</strong>. </p> <p>in the other class, I added an if statment, if that boolean is true, then setEditable(true)</p> <p>-========- The point of this program, that when the exception occurs the user cannot enter anything in the fields until he closes the exception window.</p> <p>I wish you'd help me.</p> <p>With all love, programmers. </p> <p><strong>The code of action performed for THE EXCEPTION WINDOW FRAME ( having Okay button. )</strong></p> <pre><code> public void actionPerformed(ActionEvent e){ { allow=true; //static boolean Container TheFrame = OKButton.getParent(); do TheFrame = TheFrame.getParent(); while (!(TheFrame instanceof JFrame)); ((JFrame) TheFrame).dispose(); } </code></pre> <p><strong>The code of action performed for THE MAIN PROGRAM (having three fields, an exception will occur once the user enters non digits )</strong> I added some comments to clarify.</p> <pre><code> public void actionPerformed(ActionEvent event) { try{ r =Double.parseDouble(RField.getText()); s=Double.parseDouble(SField.getText()); h=Double.parseDouble(HField.getText()); Cone C = new Cone(r,s,h);//class cone if (event.getSource() instanceof JButton) { JButton clickedButton = (JButton) event.getSource(); if (clickedButton == VolumeButton) { Result.append("VOLUME = "+C.volume()+ "\n"); ifV= true;//this's for clearing the fields for new entries. } if (clickedButton == AreaButton) { Result.append("SURFACE AREA = "+C.surfaceArea()+ "\n"); ifA= true;//this's for clearing the fields for new entries. } if(ifA&amp;&amp;ifV){ // clearing the fields for new entries. SField.setText(CLEAR); HField.setText(CLEAR); RField.setText(CLEAR); ifV=false; ifA= false;} } SList.addShape(C); } catch(NumberFormatException e){ //Object of type "Exception__" already created Ex.setVisible(true);//class "Exception__" is the one i've made for Exception window SField.setText(CLEAR); HField.setText(CLEAR); RField.setText(CLEAR); SField.setEditable(false); HField.setEditable(false); RField.setEditable(false); }/*here, if the user clicked on -that okay in Exception window- and variable allow initialized to "true" those statements should extend. I guess? - everything worked correctly except for this ?*/ if(Ex.allow){ SField.setEditable(true); HField.setEditable(true); RField.setEditable(true); } } </code></pre> <p><strong>THANK YOU ALL IT FINALLY WORKED.</strong> </p> <p>I added </p> <pre><code>Ex.allow(SField,HField,RField); </code></pre> <p>to the catch. </p> <p>and added this method in class Exception__:</p> <pre><code> public void allow(JTextField js,JTextField jh,JTextField jr){ HField =jh; SField =js; RField =jr; } </code></pre> <p>finally, to the action performed of class Exception__: </p> <pre><code> SField.setEditable(true); HField.setEditable(true); RField.setEditable(true); </code></pre> <p>WOHOOOO. It feels so awesome lol. Thanks all. should I delete my question or leave it for others who might face the same problem as mine? :P</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload