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

有帮助吗?

解决方案

      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);
    }         

其他提示

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

   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

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;
    }

}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top