How to make a toggle button toggled automatically when someone types anything in text field?? i am using java Net-beans
Asked
Active
Viewed 4,899 times
4 Answers
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
-
3you can check if it is selected or not using if(jToggleButton1.isSelected()) { jTextField1.setText("BUTTON IS SELECTED"); } – Tech Nerd Apr 27 '13 at 17:12
-
1 more minute and i will:P – remi yazigi Apr 27 '13 at 17:14
-
Thanks for your positive reply dear so nice of you – Tech Nerd Apr 27 '13 at 17:14
-
can i use something like if the text field is empty toggle button is off its not empty button is toggle? – remi yazigi Apr 27 '13 at 17:16
-
Yes you can let me make another answer for you – Tech Nerd Apr 27 '13 at 17:32
-
i have made another answer waiting for your reply – Tech Nerd Apr 27 '13 at 17:36
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
-
thank you again i would vote +1 but still i need 2 popularity points – remi yazigi Apr 27 '13 at 17:40
-
-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;
}
}