Hussain
Hussain

Reputation: 917

Using mouse Motion Listener on JButton?

hii I have used Image as JButton for set in to panel but now i want to use mouse motion listener actions on that image for that purpose what can i do

following is the code for image

JButton buttonForPicture = new JButton();

    buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
    buttonForPicture.setOpaque(false);
    buttonForPicture.setIcon(new ImageIcon("/Users/hussainalisyed/Documents/Images/pic9.jpg"));
    panel5.add(buttonForPicture,BorderLayout.CENTER);

is there any another way to do that or ...

Upvotes: 0

Views: 1967

Answers (2)

camickr
camickr

Reputation: 324078

Read the JButton API there are methods to change the icon on a mouse rollover, if thats what you are trying to do. Search the API for methods containing "icon" to see what your options are.

If you just want to know how to write a MouseMotionListener, then read the section from the Swing tutorail on How to Write a Mouse Motion Listener for a working example.

Upvotes: 1

tom
tom

Reputation: 1037

I'm not sure exactly what you're asking?

Your button is like any other JButton:

buttonForPicture.addMouseMotionListener(new MouseMotionListener() {
    @Override
    public void mouseMoved(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }
});

That catches movement events for the whole button, not just the image.

Upvotes: 1

Related Questions