3

How to make a toggle button toggled automatically when someone types anything in text field?? i am using java Net-beans

mKorbel
  • 109,525
  • 20
  • 134
  • 319
remi yazigi
  • 85
  • 1
  • 10

4 Answers4

5
      jToggleButton1.setSelected(true);

and if you are using netbeans go to properties of button and choose selected property mark it checked if you want someone to enter the text in the textfield then automatically let the toggle button select use like this

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
     jToggleButton1.setSelected(true);
    }         
Tech Nerd
  • 822
  • 1
  • 13
  • 39
3

Add a listener to the textfield and call toggleButton.setSelected(boolean selected).

Stefan
  • 12,108
  • 5
  • 47
  • 66
3
   if(t.getText().length()==0){

       jToggleButton1.setSelected(true);
   }
   else if(t.getText().length()>0){

      jToggleButton1.setSelected(false);
   }

you have to check the length of the text field and then by using the if-else you can select or dis select the toggle button

Tech Nerd
  • 822
  • 1
  • 13
  • 39
-1

int count=0;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    for(int i=0; i<1; i++){count++;}
    count=count;
    System.out.println(count);
    if(count==1){
      System.out.println("open");
      jButton1.setText("open");
     }
    if(count==2){
      System.out.println("close");
      jButton1.setText("close");
    count=0;
    }

}