weardstuff
weardstuff

Reputation: 807

java swing events

I want when I enter a button, text to appear in the console. How can I combine the methods there is my comfusing, can someone explain and give example.

Upvotes: 1

Views: 157

Answers (3)

jzd
jzd

Reputation: 23629

Add an ActionListener to the button. In the actionPerformed() method print text on the console or whatever else you want.

Upvotes: 0

Mike Wasson
Mike Wasson

Reputation: 316

Use a MouseListener. For example:

JComponent button = new JButton();
component.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.println("Mouse entered the button");
    }
});

MouseAdapter is a special MouseListener that has default empty implementations of all the other methods that the MouseListener provides, so you don't have to override them. You may want to look at the Javadoc for MouseAdapter, MouseListener, and MouseEvent.

Upvotes: 0

Bala R
Bala R

Reputation: 108937

Try

    JButton button = new JButton("Button1");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
        System.out.println("Button1 was Clicked!");

        }
    });

    // add button to a container

Upvotes: 1

Related Questions