Hello, I'm new to Java Programming but an experienced C++ programmer. I was learning how to program GUIs using swing. I was wondering how resource intensive (runtime as well as memory) are ActionListeners? Is there a general guideline to the total number of listeners that one should create in a particular program? How many until performance is affected?
I am currently learning Java through the Deitel Developer Series Java for Programmers book. In a particular example they have an array of JRadioButtonItems as a private variable for the class. They also had created an ItemHandler class that extended from the ActionListener class that conducted a linear search on the entire array of radio buttons in order to determine the one that was selected and changes the state of the program accordingly. All of the radio buttons in the array shared the same Action Listener. This seemed rather inefficient to conduct a linear search of information so I had rewritten the ActionListener class to take in the proposed value to modify in the constructor and gave each radio button its own ActionListener with the proposed value passed in by the constructor in order to avoid doing a linear search. Which method would be better performance-wise? I apologize for sounding like a noob, I am just trying to develop a good set of habits for programming in Java. Attached is a small example of the code. Thanks.
/************************************************************************
Original code in Deitel book with linear search of selected Radio button in Actionlistener
****************************************************************************/
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class MenuTest extends JFrame{
private final Color colorValues[] = {Color.BLACK, Color.WHITE, Color.GREEN};
private JRadioButtonMenuItem colorItems[];
private ButtonGroup colorButtonGroup;
public MenuTest(){
super("Menu Test");
JMenu fileMenu = new JMenu("File");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
String colors[] = {"Black", "White", "Green"};
JMenu colorMenu = new JMenu("Color");
colorItems = new JRadioButtonMenuItem[colors.length];
colorButtonGroup = new ButtonGroup();
ItemHandler itemHandler = new ItemHandler();
for(int count = 0; count < colors.length; count++){
colorItems[count] = new JRadioButtonMenuItem(colors[count]);
colorMenu.add(colorItems[count]);
colorButtonGroup.add(colorItems[count]);
colorItems[count].addActionListener(itemHandler);
}
colorItems[0].setSelected(true);
fileMenu.add(colorMenu);
fileMenu.addSeparator();
}
private class ItemHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
for(int count = 0; count < colorItems.length; count++){
if(colorItems[count].isSelected()){
getContentPane().setBackground(colorValues[count]);
}
}
}
}
public static void main(String args[]){
MenuTest menuFrame = new MenuTest();
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.setSize(600,400);
menuFrame.setVisible(true);
menuFrame.getContentPane().setBackground(menuFrame.colorValues[0]);
}
}
/************************************************************************
My Code redefined version of Deitel's w/o linear search in ActionListener
************************************************************************/
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class MenuTest extends JFrame{
private final Color colorValues[] = {Color.BLACK, Color.WHITE, Color.GREEN};
private JRadioButtonMenuItem colorItems[];
private ButtonGroup colorButtonGroup;
public MenuTest(){
super("Menu Test");
JMenu fileMenu = new JMenu("File");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
String colors[] = {"Black", "White", "Green"};
JMenu colorMenu = new JMenu("Color");
colorItems = new JRadioButtonMenuItem[colors.length];
colorButtonGroup = new ButtonGroup();
ItemHandler itemHandler = new ItemHandler();
for(int count = 0; count < colors.length; count++){
colorItems[count] = new JRadioButtonMenuItem(colors[count]);
colorMenu.add(colorItems[count]);
colorButtonGroup.add(colorItems[count]);
colorItems[count].addActionListener(new ItemHandler(colorValues[count]));
}
colorItems[0].setSelected(true);
fileMenu.add(colorMenu);
fileMenu.addSeparator();
}
private class ItemHandler implements ActionListener{
private Color setColor;
public ItemHandler(Color inColor){
super();
setColor = inColor;
}
public void actionPerformed(ActionEvent event){
getContentPane().setBackground(setColor);
repaint();
}
}
public static void main(String args[]){
MenuTest menuFrame = new MenuTest();
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.setSize(600,400);
menuFrame.setVisible(true);
menuFrame.getContentPane().setBackground(menuFrame.colorValues[0]);
}
}