Search code examples
javaswingeventsmouseeventjtextfield

Adding multiple Panels and events to a GUI


I'm new to java and working on a GUI assignment and I'm running into some issues. The premise is a simple GUI window that has a few mouse events and a few keyboard events.

I put together a window with some mouse events and once that was working started to add a couple JTextFields to the window but they're not showing up on the window and I'm not exactly sure why.

Here is the problem now. I created a new panel (panel2) to add the JTextFields to the window and I now see the JTextFields but it overtakes the entire window and the Mouse Events do not work with it. If I add the JTF to the panel above the Mouse Events then the JTF do not show up and the Mouse Events work.......

code

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

    public class EventDemo extends JFrame {

        private JPanel mousePanel;
        private JPanel panel1;
        private JPanel panel2;
        private JPanel panel3;
        private JLabel statusBar;
        private JLabel directions1;
        private JLabel directions2;
        private JLabel directions3;
        private JTextField textField1;
        private JTextField textField2;

    public EventDemo() {
        super("EVENT DEMO PROGRAM");

    panel1 = new JPanel();
    panel2 = new JPanel();

    //Add directions for the events to top of the window.
    directions1 = new JLabel("Enter & Leave window." + 
                            "    Press & hold, release, drag, move cursor to display a message in statusbar." + 
                            "    Clicking in one spot will display coordinates.");  
    panel1.add(directions1);
    add(panel1, BorderLayout.PAGE_START);   

    //Add mouse and statusBar to panel.
    mousePanel = new JPanel();
    mousePanel.setBackground(Color.WHITE); 
    add(mousePanel, BorderLayout.CENTER);   
    statusBar = new JLabel("Default");
    add(statusBar, BorderLayout.SOUTH);

    //Create handler object for Mouse events
    TheHandler handler = new TheHandler();
    mousePanel.addMouseListener(handler);
    mousePanel.addMouseMotionListener(handler); 


    textField1 = new JTextField(10);
    panel2.add(textField1, BorderLayout.SOUTH);
    textField2 = new JTextField("Enter Text Here");
    panel2.add(textField2, BorderLayout.SOUTH);
    add(panel2);

    TheHandler handlerJTF = new TheHandler();
    textField1.addActionListener(handlerJTF);
    textField2.addActionListener(handlerJTF); 


    }

    private class TheHandler implements MouseListener, MouseMotionListener {
        public void mouseClicked(MouseEvent event) {
            statusBar.setText(String.format("YOU CLICKED THE MOUSE AT %d, %d", event.getX(), event.getY()));
        }

        public void mousePressed(MouseEvent event) {
            statusBar.setText("YOU HAVE PRESSED THE MOUSE BUTTON.");
        }

        public void mouseReleased(MouseEvent event) {
            statusBar.setText("YOU HAVE RELEASED THE MOUSE BUTTON.");
        }

        public void mouseEntered(MouseEvent event) {
            statusBar.setText("YOU HAVE ENTERED THE WINDOW THE BACKGROUND CHANGES TO RED.");
            mousePanel.setBackground(Color.RED);
        }

        public void mouseExited(MouseEvent event) {
            statusBar.setText("EXITING THE WINDOW, BACKGROUND CHANGES BACK TO WHITE.");
            mousePanel.setBackground(Color.WHITE);
        }

        //Mouse Motion events.
        public void mouseDragged(MouseEvent event) {
            statusBar.setText("YOU ARE DRAGGING THE MOUSE.");
        }
        public void mouseMoved(MouseEvent event) {
            statusBar.setText("YOU ARE MOVING THE MOUSE AROUND.");
        }
    }

    public void actionPerformed(ActionEvent event) {
        textField1.getText();
        textField2.getText();
    }
    public static void main(String[] args) {
        EventDemo go = new EventDemo();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(960, 300);
        go.setVisible(true);
        new EventDemo();

    }
    }

Solution

    1. TheHandler does not implement ActionListener which is the required type of JTextField#addActionListener. See How to Write an Action Listeners and How to Use Text Fields for more details
    2. Your actionPerformed method isn't actually defined in TheHandler class, but is a method of the EventDemo class
    3. You should use the @Override annotation when you think you're overriding methods (or implementing methods from an interface) as this will cause a compiler error when you're wrong (for some reason)
    4. Your fields aren't appearing for two reasons, first, textField is added to panel2 which is never added to anything and textField2 is been added to the frame (at the default position of BorderLayout.CENTER), but is been overriden/circumvented by add(mousePanel, BorderLayout.CENTER);, so it's actually never laid out by the frames BorderLayout. Have a look at How to Use Borders for more details