2

I know I can add ActionListener to JButton if I declare JButton with a name.

JButton showDialogButton = new JButton("Click Me");

    showDialogButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        // display/center the jdialog when the button is pressed
        JDialog d = new JDialog(frame, "Hello", true);
        d.setLocationRelativeTo(frame);
        d.setVisible(true);
      }
    });

But what should I do if I have the following code:

MyJFrame frame = new MyJFrame();
frame.setSize(500,300);
JPanel base = new JPanel();

base.setLayout(new BorderLayout());

JPanel north = new JPanel();

north.add(new JLabel("Name"));
north.add(new JTextField());
north.add(new JButton("Enter"));
north.add(new JButton("Exit"));

I would appreciate any answer.

CsharpLover
  • 47
  • 2
  • 6
  • 1
    How about `JButton enterButton = new JButton("Enter"); enterButton.addActionListener( whatever ); north.add( enterButton );`? – Thomas Feb 24 '16 at 14:21
  • 2
    @Thomas "SOreadytohelp" is what it says on your profile. Even if this is the comment section you could atleast try to live up to that motto, and to not post stuff like that. – Seth Feb 24 '16 at 14:22
  • 3
    @Thomas Wow, what an encouraging comment – Michael Faisst Feb 24 '16 at 14:23
  • @callyalater Should be an answer imo :) Just saying. – Seth Feb 24 '16 at 14:24
  • @Seth I'm ready to help if there are serious questions. But the OP almost gave the answer himself so this really feels like a troll question. – Thomas Feb 24 '16 at 14:25
  • @Thomas Then simply ignore it and move on with your life. Jesus Christ. – Seth Feb 24 '16 at 14:26
  • He wants to know if he could do it in one line without using a new variable... –  Feb 24 '16 at 14:26

3 Answers3

0

In order to add an ActionListener to your JButton you would have to keep a reference to it so that's why you can't just pass it as new to the constructor of your JPanel.
The solution is essentially doing what you did before, which is declare+initialize them separately: JButton myButton = new JButton("My Button"); and then just add the ActionListener like before to your JPanel:

myButton.addActionListener(new ActionListener() ...);

Then just north.add(myButton);.

Idos
  • 15,053
  • 14
  • 60
  • 75
0

Declare them outside the add

JButton exit = new JButton("exit");
exit.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        //do stuff
      }
    });
north.add(exit);

then do the same for all the other components you wish to add listeners to

Ronan
  • 583
  • 5
  • 20
  • I was showing an example based on his code on declaring then adding to the panel hoping it would clarify it. – Ronan Feb 24 '16 at 14:26
0

UPDATE:

A nicer way to add an ActionListener to an anonymous object can be done by using the double brace initialization syntax noted here. Here is an example of that:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GuiTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(500, 300);

        JPanel base = new JPanel();
        base.setLayout(new BorderLayout());

        JPanel north = new JPanel();

        Component comp1 = north.add(new JLabel("Name"));
        System.out.println("comp1 class type: " + comp1.getClass().getName());
        Component comp2 = north.add(new JTextField());
        System.out.println("comp2 class type: " + comp2.getClass().getName());
        Component comp3 = north.add(new JButton("Enter"));
        System.out.println("comp3 class type: " + comp3.getClass().getName());
        north.add(new JButton("Exit") {{
                      addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) {
                              System.out.println("EXIT");
                          }
                      });
                  }});
        base.add(north);

        frame.getContentPane().add(base);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

After searching through the Java API, I found that the add method returns the component being added. Unfortunately, it is just a generic Component object and can't be chained without a cast. But you can get the added object like this:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GuiTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(500, 300);
        JPanel base = new JPanel();

        base.setLayout(new BorderLayout());

        JPanel north = new JPanel();

        Component comp1 = north.add(new JLabel("Name"));
        System.out.println("comp1 class type: " + comp1.getClass().getName());
        Component comp2 = north.add(new JTextField());
        System.out.println("comp2 class type: " + comp2.getClass().getName());
        Component comp3 = north.add(new JButton("Enter"));
        System.out.println("comp3 class type: " + comp3.getClass().getName());
        ((JButton)north.add(new JButton("Exit")))
                                .addActionListener(new ActionListener() {
                                     public void actionPerformed(ActionEvent e) {
                                         System.out.println("EXIT");
                                     }
                                 });
        base.add(north);

        frame.getContentPane().add(base);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

This code is complete and verifiable (I tested it on my Arch Linux x64 machine). It is a little ugly, but works.

callyalater
  • 3,102
  • 8
  • 20
  • 27
  • I know it is ugly and awful, but it attaches an ActionListener to an anonymous JButton, which was the point of the question. I personally would never do this in code, but it demonstrates that it is possible. – callyalater Feb 24 '16 at 15:36