Problems w/ MouseEntered for a JButton

I have a JPanel with some JButtons in it. I've set up a MouseEntered method for each button to change the cursor to a hand:

private void courmayeurMouseEntered(java.awt.event.MouseEvent evt) {

setCursor(new Cursor(Cursor.HAND_CURSOR));

}

Here's the problem:

The cursor changes to a hand upon entering anywhere in the JPanel - not just the JButtons as intended.

I used Netbeans to add the MouseEntered method on each button.

What to do?

[487 byte] By [foolongcamudgeona] at [2007-11-26 18:05:14]
# 1

I think you're wrongly assuming that "setCursor()" is some kind of global method.

Presumably this method is in the class which extends your panel, or an inner class of it. In which case it is calling "setCursor()" on the panel. You'll need to change that line to "myButton.setCursor(...)" (the button is available via "evt.getSource()" anyway).

Note that you don't need to do this on an event - you can just set the cursor for the button once, up-front.

itchyscratchya at 2007-7-9 5:35:48 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks,

Using the dot operator on the button name fixed it:

private void courmayeurMouseEntered(java.awt.event.MouseEvent evt) {

courmayeur.setCursor(new Cursor(Cursor.HAND_CURSOR));

}

Foo

Also:

"Note that you don't need to do this on an event - you can just set the cursor for the button once, up-front".

I'm using Netbeans and tried to set it when I dropped the JButton into the JPanel. Kept getting a "void" return type not allowed here (for user code).

How :-)

Message was edited by:

foolongcamudgeon

foolongcamudgeona at 2007-7-9 5:35:48 > top of Java-index,Desktop,Core GUI APIs...
# 3
I'm using Netbeans and tried to set it when I dropped the JButton into the JPanel. Kept getting a "void" return type not allowed here (for user code).I can only assume NetBeans is writing rubbish code for you, which is standard for UI builders.
itchyscratchya at 2007-7-9 5:35:48 > top of Java-index,Desktop,Core GUI APIs...
# 4

by using netbeans5.0

/*

* NewJFrame.java

*

* Created on February 12, 2007, 10:20 PM

*/

package javaapplication2;

import java.awt.Cursor;

/**

*

* @author Anand

*/

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */

public NewJFrame() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jPanel1 = new javax.swing.JPanel();

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseEntered(java.awt.event.MouseEvent evt) {

jButton1MouseEntered(evt);

}

public void mouseExited(java.awt.event.MouseEvent evt) {

jButton1MouseExited(evt);

}

});

org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.add(jButton1)

.addContainerGap(19, Short.MAX_VALUE))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(jPanel1Layout.createSequentialGroup()

.add(38, 38, 38)

.add(jButton1)

.addContainerGap(39, Short.MAX_VALUE))

);

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.addContainerGap(137, Short.MAX_VALUE)

.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.add(163, 163, 163))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.addContainerGap(63, Short.MAX_VALUE)

.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.add(137, 137, 137))

);

pack();

}// </editor-fold>

private void jButton1MouseExited(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

Cursor c=new Cursor(Cursor.DEFAULT_CURSOR);

this.setCursor(c);

}

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

Cursor c=new Cursor(Cursor.HAND_CURSOR);

this.setCursor(c);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JPanel jPanel1;

// End of variables declaration

}

Anand_Agrawala at 2007-7-9 5:35:48 > top of Java-index,Desktop,Core GUI APIs...
# 5

I can only assume NetBeans is writing rubbish code for you, which is standard for UI builders.

...and thanks for posting some code that proves my point :o)

You could replace all this code,

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseEntered(java.awt.event.MouseEvent evt) {

jButton1MouseEntered(evt);

}

public void mouseExited(java.awt.event.MouseEvent evt) {

jButton1MouseExited(evt);

}

});

...

private void jButton1MouseExited(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

Cursor c=new Cursor(Cursor.DEFAULT_CURSOR);

this.setCursor(c);

}

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {

// TODO add your handling code here:

Cursor c=new Cursor(Cursor.HAND_CURSOR);

this.setCursor(c);

}

...with this...

jButton1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

There's no point switching cursor on mouse entry and exit, because Swing does that for you. There's also no point creating new Cursor objects when you can use pooled instances.

itchyscratchya at 2007-7-9 5:35:48 > top of Java-index,Desktop,Core GUI APIs...