multiple listeners

From: The Chief (TheChief_at_xxx.xx)
Date: 02/25/04


Date: Wed, 25 Feb 2004 03:31:59 GMT

Hi, I'm new to Java and some help here would be greatly appreciated.

My homework is to create a program where you can add() numbers (and
store them internally) into an array, then sort() the numbers (from
smallest to largest), print() them - no matter sorted or not. And also
an option to search() for a specific number in the array. The methods
are easy but I want to do everything with gui. I've set up the whole
layout and now I have to add the listeners for ALL the buttons - they
are four. The problem is that I don't know how to create multiple
listeners - one for each button - since every button is doing a
different work - add, sort, print, search.

Here's my code so far:

//import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;

public class Frame extends JFrame
{
        private ActionListener listener;
        
        public Frame()
        {
                createPanel();
                pack();
        }
        
        private void createPanel()
        {
        
                class ButtonListener implements ActionListener
                {
                        public void actionPerformed(ActionEvent event)
                        {
                                < code for one of the buttons >
                        }
                }
                
                // PRINT TEXT AREA
                final JTextArea textArea = new JTextArea(7,50);
                textArea.setEditable(false);
                JScrollPane scrollPane = new JScrollPane(textArea);
                
                //Frame Creation
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(scrollPane);
                frame.pack();
                frame.show();
                
                //Creation of panels.
                JPanel controlPanel = new JPanel();
                JPanel personalPanel= new JPanel();
                JPanel salaryPanel=new JPanel();
                
                // ARRAY MANIPULATION ELEMENTS
                
                        // ADD # TO ARRAY
                final JTextField addField=new JTextField(3);
                JButton addButton=new JButton("Add # to Array");
                        
                        // SORT THE ARRAY
                JButton sortButton=new JButton("Sort the Array");
                
                        // PRINT THE ARRAY
                JButton printButton=new JButton("Print the Array");
        
                // ARRAY SEARCH ELEMENTS
        
                        // ENTER # TO SEARCH FOR
                final JTextField searchField=new JTextField(3);
        
                        // FIND # IN ARRAY
                JButton findButton=new JButton("Find # in Array");
                ActionListener listener= new ButtonListener();
                findButton.addActionListener(listener); // Add listener
                
                //Array Manipulation Add
                 personalPanel.setBorder(new TitledBorder(new EtchedBorder(), "Array
Manipulation"));
                 personalPanel.add(addField);
                 personalPanel.add(addButton);
                 personalPanel.add(sortButton);
                 personalPanel.add(printButton);

                //Array Search Add
                controlPanel.setBorder(new TitledBorder(new EtchedBorder(), "Array
Search"));
                controlPanel.add(searchField);
                controlPanel.add(findButton);

                getContentPane().add(personalPanel,BorderLayout.CENTER);
                getContentPane().add(controlPanel,BorderLayout.SOUTH);
        }
}

I tried to create another inner class for the second listener but it
wouldn't let me! Any suggestions?

Thanks in advance!