Search code examples
javaswingactionlistenerjtextfield

How can i print the button name that trigger the event?


I have made this program and I want to print a message and the name of the button, when I click some button. But it does not print anything and does not make any error.

public class Example {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Hello");
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame.setLayout(new BorderLayout());

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(2, 2));

        JTextArea txtArea = new JTextArea();
        HelloActionListener listen = new HelloActionListener(txtArea);
        JButton b1 = new JButton("TOP");
        b1.addActionListener(listen);
        JButton b2 = new JButton("LEFT");
        JButton b3 = new JButton("RIGHT");
        JButton b4 = new JButton("BOTTOM");
        b4.addActionListener(listen);
    }
}

And the class HelloActionListener:

public class HelloActionListener implements ActionListener {

    public JTextArea area;

    public HelloActionListener(JTextArea area) {
        this.area = area;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton x = (JButton) e.getSource();
        area.setText("Hello Folks" + x.getText());
    }
}

Does anyone know where my error is?


Solution

  • I see three main problems. The first one is that you aren't adding your Buttons to the Jpanel you've created (buttonsPanel), nor are you adding the buttonsPanel to your frame, and you also aren't adding the txtArea to the frame. Second, your order of operations is off. I recommend calling the frame.setVisible(true) after you've finished building the frame. The third issue is you're using the BorderLayout on your JFrame, which is just not a very good layout to work with (in my opinion). Your listener and the other code all looks fine.

    Changing your code to this seems to make it run as desired:

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("Hello");
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2,1));
    
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(2, 2));
    
        JTextArea txtArea = new JTextArea("Place Holder");
        txtArea.setColumns(2);
        HelloActionListener listen = new HelloActionListener(txtArea);
        JButton b1 = new JButton("TOP");
        b1.addActionListener(listen);
        buttonsPanel.add(b1);
        JButton b2 = new JButton("LEFT");
        buttonsPanel.add(b2);
        JButton b3 = new JButton("RIGHT");
        buttonsPanel.add(b3);
        JButton b4 = new JButton("BOTTOM");
        b4.addActionListener(listen);
        buttonsPanel.add(b4);
    
        frame.add(txtArea);
        frame.add(buttonsPanel);
    
        frame.setVisible(true);
    }
    

    I'd recommend reading this guide about layout managers in Java though. Even though it works here, this isn't a great way to have your layout.