Tutorial Addendum on Beat - Beat JRadioButton
This affiliate describes:
- JRadioButton chic and its accompanying classes.
- Different types contest accompanying radio button s.
- How to acquisition the called radio button .
JRadioButton and Accompanying Classes
javax.swing.JRadioButton - A Beat chic apery a UI radio button . Some
interesting methods are:
- JRadioButton(String) - Architect to make a radio button with the
specified cord displayed next to the button .
- addActionListener(ActionListener) - Adjustment to add an activity adviser to this button
to handle activity events. A abrasion bang on this button will activate one activity event.
- addChangeListener(ChangeListener) - Adjustment to add a change adviser to this button
to handle change events. A abrasion bang on this button will activate some change events.
- addIteListener(ItemListener) - Adjustment to add an account adviser to this button
to handle account events. A abrasion bang on this button will activate one account event.
- setActionCommand(String) - Adjustment to set an activity command cord to this button .
javax.swing.ButtonGroup - A Beat chic apery a accumulation of button s.
If one radio button is called in a group, all additional button s in the aforementioned group
are un-selected.
- add(AbstractButton) - Adjustment to add a button to this button group.
- getSelection() - Adjustment to acknowledgment the called button in this button accumulation as
a ButtonModel object.
javax.swing.JToggleButton.ToggleButtonModel - A Beat chic apery a absence implementation
of toggle button s data model. ToggleButtonModel is an close chic nested inside
javax.swing.JToggleButton, which is a abject chic of JRadioButton.
- getActionCommand() - Adjustment to acknowledgment the activity command cord of the associated button .
JRadioButton and Accident Listeners
As you can see from the antecedent section, a radio button can accept 3 types of accident listensers:
ActionListener, ChangeListener, and ItemListener. The afterward sample program shows you
when those admirers are called, and how some times:
/**
* JRadioButtonTest.java
* Absorb (c) 2002 by Dr. Yang
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public chic JRadioButtonTest {
accessible changeless abandoned main(String[] a) {
JFrame f = new JFrame("My Radio Buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup g = new ButtonGroup();
MyRadioButton b1 = new MyRadioButton("On");
g.add(b1);
f.getContentPane().add(b1,BorderLayout.NORTH);
MyRadioButton b2 = new MyRadioButton("Off");
g.add(b2);
f.getContentPane().add(b2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
clandestine changeless chic MyRadioButton extends JRadioButton
accouterments ActionListener, ChangeListener, ItemListener {
changeless int calculation = 0;
Cord argument = null;
accessible MyRadioButton(String t) {
super(t);
argument = t;
addActionListener(this);
addChangeListener(this);
addItemListener(this);
}
accessible abandoned actionPerformed(ActionEvent e) {
count++;
System.out.println(count+": Activity performed - "+text);
}
accessible abandoned stateChanged(ChangeEvent e) {
count++;
System.out.println(count+": Accompaniment afflicted on - "+text);
}
accessible abandoned itemStateChanged(ItemEvent e) {
count++;
System.out.println(count+": Account accompaniment afflicted - "+text);
}
}
}
|
Tags: action, change, system, class, radio, handle, click, swing, button, mouse, event, method, group, buttons, related, events, command, public, string button, swing, jradiobutton, string, class, method, count, radio, public, event, action, myradiobutton, events, group, buttons, selected, javax, import, buttongroup, static, jframe, println, system, mouse, listener, actionlistener, representing, related, handle, click, change, changelistener, trigger, itemlistener, , radio button, javax swing, public void, count system, println count, system out, button will, swing class, class representing, mouse click, swing jradiobutton, swing class representing, button will trigger, import javax swing, action command string, swing swing jradiobutton, |
Also see ...
PermalinkArticle In : Computers & Technology - swing