Hi,
Please review following example of ButtonModel interface of java:
Code:
import javax.swing.ButtonModel;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class ButtonModelMain {
public static void main(String[] agr) {
JFrame frame01 = new JFrame();
frame01.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToggleButton tgb = new JToggleButton("Selected");
ActionListener actionListener01 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
System.out.println("Action - selected=" + selected + "\n");
} };
ItemListener itemListener01 = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
System.out.println("Selected");
} else {
System.out.println("Deselected");
} } };
tgb.addActionListener(actionListener01);
tgb.addChangeListener(changeListener);
frame01.add(tgb, BorderLayout.NORTH);
JToggleButton tgb2 = new JToggleButton("Focused");
frame01.add(tgb2, BorderLayout.CENTER);
frame01.setSize(300, 225);
frame01.setVisible(true);
}
}
Bookmarks