ButtonModel interface of java
Hi All,
I am beginner to the java programming. I want to be ware about the ButtonModel interface of java. I don't know anything about the constructors and method of ButtonModel interface of java. I have referred manu java books but nothing was helpful. I wonder if you are able let me know something about ButtonModel interface of java. I will be great if you provide proper example for the ButtonModel interface.
Re: ButtonModel interface of java
A ButtonModel interface is a state Model for buttons. This model is used for check boxes and radio buttons, which are special kinds of buttons, as well as for normal buttons. For check boxes and radio buttons, pressing the mouse selects the button. For normal buttons, pressing the mouse "arms" the button. Releasing the mouse over the button then initiates a button press, firing its action event. Releasing the mouse elsewhere disarms the button.
Re: ButtonModel interface of java
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);
}
}
Re: ButtonModel interface of java
Following are some available methods of ButtonModel interface:
- setActionCommand(String strg)
- removeItemListener(ItemListener ltmlstner)
- removeChangeListener(ChangeListener chngln)
- removeActionListener(ActionListener actln)
- addItemListener(ItemListener itmln)
Re: ButtonModel interface of java
While using ButtonModel interface it is essential to invoke method known as 'setSelected(boolean)'. That is when a click of mouse occurs over the radio button. It will call method setArmed(boolean) only when the mouse is pressed button. A superinterface of 'ButtonModel interface' is nothing but 'ItemSelectable'. This interface implements all the classes of the 'DefaultButtonModel' class of java.
Re: ButtonModel interface of java
In below java example I have used 'isPressed()' method of 'ButtonModel interface', please study it carefully:
Code:
import javax.swing.ButtonModel;
public class BMMainClass {
public static void main(String[] avnd) {
final String DL = "Deselected";
final String SL = "Selected";
JFrame FL = new JFrame("Selecting CheckBox");
FL.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkBox01 = new JCheckBox(DL);
} }
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
AbstractButton abstractButton = (AbstractButton) changeEvent
.getSource();
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
} } }
checkBox01.addActionListener(actionListener);
checkBox01.addChangeListener(changeListener);
checkBox01.addItemListener(itemListener);
checkBox01.setMnemonic(KeyEvent.VK_S);
FL.add(checkBox01, BorderLayout.NORTH);
FL.setSize(300, 100);
FL.setVisible(true);
}
}