Search code examples
javaeclipseswinguser-interfacejbutton

Why are my JButton Action Listeners not responding?


I have a small GUI Application made on eclipse for Mac using swing to create and navigate through a Binary Search Tree containing student information. I have created JButtons to do various things such as create the binary search tree from a file of data, add a students information node by node, and browse through the data manually. This is working and a screenshot of the GUI is linked below. However none of the buttons are working!! I have implemented an inner Listener class. The buttons are displayed and can be clicked, but nothing happens.

Current GUI produced by code below

Any help is appreciated!

public class MyFrame extends JFrame 
{
    /**
     * Member variables include a serialversionUID
     * various required Labels and Buttons,
     * the container, a listener and the BST
     */
    static final long serialVersionUID = 2L;

    private JLabel titleLabel, mainLabel;
    private JButton insertButton, findButton,   
        browseButton, createButton;

    private Container c;

    MyListener listener;

    BinSearchTree bst;
    /**
     * Inner class MyListener implements the 
     * ActionListener used by the program to determine
     * which button was clicked and how to proceed
     */
    class MyListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            if (e.getSource() == insertButton)
            {
                JTextField field1 = new JTextField();
                JTextField field2 = new JTextField();
                JTextField field3 = new JTextField();
                JTextField field4 = new JTextField();
                Object[] message = {
                    "StudentID:", field1,
                    "Faculty", field2,
                    "Major:", field3,
                    "Year:", field4,
                };
                int option = JOptionPane.showConfirmDialog(null,message,"Please Enter the following:", JOptionPane.OK_CANCEL_OPTION);
                String studentID="";
                String faculty="";
                String major="";
                String year="";
                if (option == JOptionPane.OK_OPTION)
                {
                    studentID = field1.getText();
                    faculty = field2.getText();
                    major = field3.getText();
                    year = field4.getText();
                }
                mainLabel.setText(mainLabel.getText() + 
                        studentID + '\t' + faculty + '\t' + major 
                        + '\t' + year + '\n');

                bst.insert(studentID, faculty, major, year);
            }

            else if (e.getSource() == findButton)
            {
                String studentID = JOptionPane.
                        showInputDialog("Please enter a Student ID:");

                Node student = bst.find(bst.root,studentID);

                JOptionPane.showMessageDialog(null, student.toString());
            }

            else if (e.getSource() == browseButton)
            {
                c.add("Center",mainLabel);
                c.add("East",new Scrollbar(Scrollbar.VERTICAL));
                validate();
                repaint();
            }

            else if (e.getSource() == createButton)
            {

                String fileName = JOptionPane.
                        showInputDialog("Please specify which file to use:");
                try
                {
                    Scanner scan = new Scanner(new File(fileName));

                    bst = createTree(scan);

                    scan.close();

                } catch(FileNotFoundException fnfe) 
                {
                    System.err.println(fnfe.getMessage());

                }
            }
        }
    }

    /**
     * Constructor takes the frame handle as a string argument
     * and initiates the member Buttons and Labels.
     * The inner class MyListener
     * 
     * @param MyFrame handle
     */
    public MyFrame(String s)
    {
        super(s);
        titleLabel = new JLabel("Title");
        titleLabel.setText("Student Information");

        listener = new MyListener();

        insertButton = new JButton("Insert");
        findButton = new JButton("Find");
        browseButton = new JButton("Browse");
        createButton = new JButton("Create Tree From File");

        JPanel buttonPanel = new JPanel();

        buttonPanel.setLayout(new FlowLayout());

        buttonPanel.add(insertButton);
        buttonPanel.add(findButton);
        buttonPanel.add(browseButton);
        buttonPanel.add(createButton);

        c = getContentPane();
        c.setLayout(new BorderLayout());

        c.add("North", titleLabel);
        c.add("South", buttonPanel);
    }
    /**
     * Creates the member Binary Search Tree and populates
     * it with the input from the text file specified
     * 
     * @param scanner
     * 
     * @return populated Binary Search Tree
     */
    public BinSearchTree createTree(Scanner scan)
    {
        BinSearchTree bstree = new BinSearchTree();

        while(scan.hasNext())
        {
            mainLabel.setText("");

            String[] lineData = scan.nextLine().split(" ");

            mainLabel.setText
                (mainLabel.getText() + scan.nextLine() + "\n");

            bstree.insert(lineData[0],lineData[1],
                    lineData[2],lineData[3]);
        }

        return bstree;

    }

    public static void main(String[] args)
    {
        MyFrame frame = new MyFrame("Frame1");
        frame.pack();
        frame.setVisible(true);
    }    
}   

Solution

  • You've created the buttons and the listener but you need to add the listener to the button(s).

    Info: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

    E.g:

    insertButton.addActionListener(listener);