I was looking at some code examples I created, and decided to polish them up and post them. One of the most challenging issues for most Swing developers is learning, and using events. I have included to examples below: One using events associated with a JButton, and the other using ActionEvents when a JRadioButton is pressed.
Note: The attached project is a NetBeans 6.5.1 project.
Here is the
JButtonsEvent.java example
package com.bluelotussoftware.examples.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@author <jyeary@bluelotussoftware.com>
@version
public class JButtonEvents
extends javax.swing.JFrame {
JButton button;
public JButtonEvents() {
initialize();
}
private void initialize() {
button =
new JButton(
"Press Me");
button.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(
"ActionEvent!");
}
});
button.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println(
"ItemEvent!");
}
});
button.addChangeListener(
new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println(
"ChangeEvent!");
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.pack();
}
@param
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(
new Runnable() {
public void run() {
new JButtonEvents().setVisible(
true);
}
});
}
}
Here is the
JRadioButtonActionEvents.java example
package com.bluelotussoftware.examples.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.ButtonModel;
import javax.swing.JRadioButton;
@author <jyeary@bluelotussoftware.com>
@version
public class JRadioButtonActionEvents
extends javax.swing.JFrame {
public JRadioButtonActionEvents() {
initComponents();
int value = 0;
ButtonModel[] models =
new ButtonModel[3];
int ctr = 0;
Enumeration e = buttonGroup1.getElements();
while (e.hasMoreElements()) {
JRadioButton x = (JRadioButton) e.nextElement();
models[ctr++] = x.getModel();
}
jRadioButton1.setActionCommand(
"A");
jRadioButton2.setActionCommand(
"B");
jRadioButton3.setActionCommand(
"C");
ActionListener action =
new MyActionListener();
jRadioButton1.addActionListener(action);
jRadioButton2.addActionListener(action);
jRadioButton3.addActionListener(action);
buttonGroup1.setSelected(models[value],
true);
}
class MyActionListener
implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (
"A".equalsIgnoreCase(s)) {
System.out.println(
"Radio Button \"A\" ActionEvent!");
}
else if (
"B".equalsIgnoreCase(s)) {
System.out.println(
"Radio Button \"B\" ActionEvent!");
}
else if (
"C".equalsIgnoreCase(s)) {
System.out.println(
"Radio Button \"C\" ActionEvent!");
}
}
}
@SuppressWarnings(
"unchecked")
private void initComponents() {
buttonGroup1 =
new javax.swing.ButtonGroup();
jRadioButton1 =
new javax.swing.JRadioButton();
jRadioButton2 =
new javax.swing.JRadioButton();
jRadioButton3 =
new javax.swing.JRadioButton();
jLabel1 =
new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
buttonGroup1.add(jRadioButton1);
jRadioButton1.setText(
"A");
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText(
"B");
buttonGroup1.add(jRadioButton3);
jRadioButton3.setText(
"C");
jLabel1.setText(
"Select a radio button to generate an ActionEvent");
org.jdesktop.layout.GroupLayout layout =
new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel1)
.add(layout.createSequentialGroup()
.add(21, 21, 21)
.add(jRadioButton1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jRadioButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jRadioButton3)))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jRadioButton1)
.add(jRadioButton2)
.add(jRadioButton3))
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
@param
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(
new Runnable() {
public void run() {
new JRadioButtonActionEvents().setVisible(
true);
}
});
}
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
}
NetBeans project files
SwingEventsExamples-1.0.zip