34

My task is to retrieve the value of a text field and display it in an alert box when clicking a button. How do I generate the on click event for a button in Java Swing?

5 Answers 5

64

For that, you need to use ActionListener, for example:

JButton b = new JButton("push me");
b.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        //your actions
    }
});

For generating click event programmatically, you can use doClick() method of JButton: b.doClick();

2
  • 1
    The actionPerformed method is used when a button is clicked normally. If you want to do some fancy interaction with the button you can also use other events like mousePressed in the MouseListener.
    – SebastianH
    Feb 19, 2014 at 11:59
  • 3
    @Suresh I know you've been using GUI Builder. What you need to do is right-click the button from the design view and select Events -> Action -> actionPerformed then you will see auto-generated code in the source code.. Alex +1 Feb 19, 2014 at 12:04
10

First, use a button, assign an ActionListener to it, in which you use JOptionPane to show the message.

class MyWindow extends JFrame {

    public static void main(String[] args) {

        final JTextBox textBox = new JTextBox("some text here");
        JButton button = new JButton("Click!");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(this, textBox.getText());
            }
        });
    }
}
1
  • 6
    I don't think JTextBox is a real class. This doesn't work… Feb 22, 2019 at 16:38
8

You can also use lambda-function:

JButton button = new JButton("click me");
button.addActionListener(e ->
{
    // your code here
});

However, if you mean signals and slots like in Qt, then Swing does not support this. But you can always implement this yourself using the "Observer" pattern (link).

0
JButton jEightButton=new JButton();
    

jEightButton.addActionListener(this);

public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    
    jframe.getContentPane().setBackground(Color.blue);
}
0

public GUI() {

JButton btn1 = new JButton("Click me!");

add(btn1);

btn1.addActionListener(new ActionListener()) {

        @Override
        public void actionPerformed(ActionEvent e) {

            //action for the button
        }
    }

}

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 25, 2023 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.