Saturday, November 21, 2009

Swing Event Examples - JButton and JRadioButton

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

/*
* Copyright 2009 John Yeary <jyeary@bluelotussoftware.com>.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*

* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/

/**
* JButtonEvents.java

*
* Created on Dec 30, 2008, 3:00:00 PM
*

* $Id: JButtonEvents.java 155 2009-11-21 15:06:40Z jyeary $
*/

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 John Yeary <jyeary@bluelotussoftware.com>

* @version 1.0
*/
public class JButtonEvents extends javax.swing.JFrame {

JButton button;

public JButtonEvents() {
initialize();
}

private void initialize() {
button = new JButton("Press Me");

//Add Listeners as anonymous inner classes.

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!");
}
});


// Exit the application when the frame is closed

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(button);
this.pack();
}

/**
* @param args the command line arguments

*/
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

/*
* Copyright 2008-2009 John Yeary <jyeary@bluelotussoftware.com>.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*

* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/

/*
* JRadioButtonActionEvents.java

*
* Created on Dec 30, 2008, 3:45:09 PM
*
* $Id: JRadioButtonActionEvents.java 155 2009-11-21 15:06:40Z jyeary $
*/
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 John Yeary <jyeary@bluelotussoftware.com>
* @version 1.0

*/
public class JRadioButtonActionEvents extends javax.swing.JFrame {

/** Creates new form JRadioButtonActionEvents */

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!");
}
}
}

/** This method is called from within the constructor to

* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.
*/

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
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();
}// </editor-fold>

/**
* @param args the command line arguments

*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new JRadioButtonActionEvents().setVisible(true);
}
});
}

// Variables declaration - do not modify

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;
// End of variables declaration
}

NetBeans project files SwingEventsExamples-1.0.zip

0 comments :

Popular Posts