I'm creating a Java GUI using Swing with Eclipse and Window Builder Pro. I'm using JButtons
and JToggleButtons
. I want to change toggle button's state from another button.
我正在使用Swing with Eclipse和Window Builder Pro创建Java GUI。我正在使用JButtons和JToggleButtons。我想从另一个按钮更改切换按钮的状态。
For example, when I click the clear grid, all the toggle buttons will be 'not selected'.
例如,当我单击清除网格时,所有切换按钮都将被“未选中”。
How can I do this? What are the methods that I have to use for toggle buttons and buttons?
我怎样才能做到这一点?我必须使用哪些方法来切换按钮和按钮?
4 个解决方案
#1
5
toggleButton.setSelected(boolean b)
toggleButton.setSelected(boolean b)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonAction {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
toggleButton1.setSelected(selected);
}
};
toggleButton.addActionListener(actionListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.add(toggleButton1, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
#2
4
Add actionListener to your JButton and in actionPerformed(ActionEvent) method change the state of all JToggleButtons. Make sure all your JToggleButton is accessible in this method. A simple example will be..
将actionListener添加到JButton并在actionPerformed(ActionEvent)方法中更改所有JToggleButtons的状态。确保在此方法中可以访问所有JToggleButton。一个简单的例子就是......
JFrame frame = new JFrame("Panel image demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());
final JToggleButton[] button = new JToggleButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JToggleButton("Toggle Us");
frame.add(button[i]);
}
JButton jButton = new JButton("Toggle that button");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JToggleButton jToggleButton : button) {
jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
}
}
});
frame.add(jButton);
frame.setVisible(true);
#3
3
Register an ActionListener
to the JButton
instance and make sure you can access the toggle buttons therein to manipulate their state.
将ActionListener注册到JButton实例,并确保您可以访问其中的切换按钮来操纵它们的状态。
#4
1
i was looking for this, this might help somebody out
我正在寻找这个,这可能会帮助别人
B1.setSelected(false);
i made a method that make all my button false (unselect the toggles when i wanted it)
我做了一个方法,使我的所有按钮都是假的(当我需要时取消选择切换)
#1
5
toggleButton.setSelected(boolean b)
toggleButton.setSelected(boolean b)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class JToggleButtonAction {
public static void main(String args[]) {
JFrame frame = new JFrame("Selecting Toggle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton toggleButton = new JToggleButton("Toggle Button");
final JToggleButton toggleButton1 = new JToggleButton("Another Toggle Button");
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
toggleButton1.setSelected(selected);
}
};
toggleButton.addActionListener(actionListener);
frame.add(toggleButton, BorderLayout.NORTH);
frame.add(toggleButton1, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
#2
4
Add actionListener to your JButton and in actionPerformed(ActionEvent) method change the state of all JToggleButtons. Make sure all your JToggleButton is accessible in this method. A simple example will be..
将actionListener添加到JButton并在actionPerformed(ActionEvent)方法中更改所有JToggleButtons的状态。确保在此方法中可以访问所有JToggleButton。一个简单的例子就是......
JFrame frame = new JFrame("Panel image demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());
final JToggleButton[] button = new JToggleButton[10];
for (int i = 0; i < button.length; i++) {
button[i] = new JToggleButton("Toggle Us");
frame.add(button[i]);
}
JButton jButton = new JButton("Toggle that button");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (JToggleButton jToggleButton : button) {
jToggleButton.setSelected(!jToggleButton.isSelected()); // <-- this will change the state of toggle button
}
}
});
frame.add(jButton);
frame.setVisible(true);
#3
3
Register an ActionListener
to the JButton
instance and make sure you can access the toggle buttons therein to manipulate their state.
将ActionListener注册到JButton实例,并确保您可以访问其中的切换按钮来操纵它们的状态。
#4
1
i was looking for this, this might help somebody out
我正在寻找这个,这可能会帮助别人
B1.setSelected(false);
i made a method that make all my button false (unselect the toggles when i wanted it)
我做了一个方法,使我的所有按钮都是假的(当我需要时取消选择切换)