Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How resource intensive are Listeners in java?

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]);
        }
    }
like image 428
Nadewad Avatar asked Jul 05 '09 06:07

Nadewad


People also ask

How are listeners created in Java?

You probably want to look into the observer pattern. class Test { public static void main(String[] args) { Initiater initiater = new Initiater(); Responder responder = new Responder(); initiater. addListener(responder); initiater.

What are all the listeners in Java and explain?

Event listeners represent the interfaces responsible to handle events. Java provides various Event listener classes, however, only those which are more frequently used will be discussed. Every method of an event listener method has a single argument as an object which is the subclass of EventObject class.

What is the use of listener class in Java?

You define a listener class as an implementation of a listener interface. Table 10–1 lists the events that can be monitored and the corresponding interface that must be implemented. When a listener method is invoked, it is passed an event that contains information appropriate to the event.


1 Answers

CPU usage: close to none. Listeners are only called when the state of the object they listen to is changed. They don't really "listen". The object to which they are listening calls them when needed. (Note: this is a simplification)

Memory usage: Mostly an ActionListener has no state. So the total enduring memory usage is the minimum required for any object. In your example, there is state, as you have a setColor field. But memory usage is low.

IMO, listeners are efficient and you can use as many as you want.

like image 132
Steve McLeod Avatar answered Sep 22 '22 08:09

Steve McLeod