java swing button listener code example

Example 1: java add listent to button

JButton button = new JButton("Do Something");
button.addActionListener( new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Do Something Clicked");
    }
});

Example 2: actionListener java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*in main*/
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
			System.out.println('x');
  }
});

Example 3: how to create an action listener in java

button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) 
            {
              
            }
        });

Tags:

Java Example