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?
# 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
}
# 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.