set a hand cursor for JLabel

Hello,

I am trying to change the cursor when I go with the mouse over a JLabel, but no results. Please help. I would like to get a Cursor.HAND_CURSOR.

This JLabel is on the JPanel component. The JLabel component is inside a JSplitPane component. How can I solve this problem ?

Thanks.

[309 byte] By [vpopovici@ro.soluziona.coma] at [2007-11-27 5:09:02]
# 1
What have you tried? Code?
Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 2

Yes. After I have placed all the elements, I tried to set the Hand cursor for a JLabel component. I have tried setCursor(new Cursor(Cursor.HAND_CURSOR)) or setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)) and nothing happens. The cursor still remains DEFAULT_CURSOR. Should I write some code for JPanel or for the JSplitPane components ?

Thanks.

vpopovici@ro.soluziona.coma at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 3

Try using a MouseListener:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class LabelExample extends MouseAdapter implements Runnable {

public void run() {

JLabel label1 = new JLabel("label with cursor");

label1.addMouseListener(this);

label1.setForeground(Color.RED);

JLabel label2 = new JLabel("label without cursor");

JPanel p = new JPanel();

p.add(label1);

p.add(label2);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(p);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public void mouseEntered(MouseEvent e) {

e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

public static void main(String[] args) {

EventQueue.invokeLater(new LabelExample());

}

}

Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 4
I have wrote the following code:empLabel.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent e) {e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));}});but nothing happened.
vpopovici@ro.soluziona.coma at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 5
Does my code work?
Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 6
Yes, your code is working. But how can I pass through my problem in my code ?
vpopovici@ro.soluziona.coma at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 7
Could you post a small (<1 page), complete example program that demonstrates how setCoursor fails to work for you? I've posted a sample program that shows it works for me. There must be some reason it's failing in your code.
Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 8

I am afraid it is a bit complicated, but I will try. I try to do a system for some purposes. And in the JPanel for entering some data, I would like to make this thing in order to show the user that when doubleclicking on a JLabel, a new window will be opened. Somthing like this. But I will try to put some code:

import javax.swing.JPanel;

import java.util.Vector;

import javax.swing.JOptionPane;

import java.sql.SQLException;

import java.awt.Cursor;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class BooksSupport extends SFCJPanel {

private SFCTextLabel dateLabel, hourLabel, stateLabel, cliLabel, cliNameLabel,

empLabel, empNameLabel, activityLabel;

private SFCDateField dateField;

private SFCTimeField timeField;

private SFCJComboBox stateComboBox;

private SFCComboList stateList;

private SFCJComboBox activityComboBox;

private SFCComboList activityList;

private Employee employee;

private Client client;

private String sql;

public BooksSupport() {

setBorder(new SFCTitledBorder("INTRETINERE PROGRAMARI"));

clearActivePanels();

addActivePanel();

addElements();

String [] enabledButtons = {"INSERT", "FIND"};

SFCToolBar.setEnabledButtons(enabledButtons, true, true);

}

private void addElements() {

SFCEgoista.ACTIVE_COMPONENTS = new Vector(10, 1);

dateLabel = new SFCTextLabel("", "Data programarii:");

dateLabel.setBounds(20, 30, 150, 20);

dateField = new SFCDateField(10, "dateField", true);

dateField.setBounds(30, 55, dateField.getWidth(), dateField.getHeight());

add(dateLabel);

add(dateField);

SFCEgoista.ACTIVE_COMPONENTS.add(dateField);

empLabel = new SFCTextLabel("ANGAJAT", "Angajat:");

empLabel.setBounds(220, 85, 100, 20);

empLabel.addMouseListener(new MouseAdapter() {

public void mouseEntered(MouseEvent e) {

System.out.println(e.getComponent().getClass().toString());

e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

});

......................

vpopovici@ro.soluziona.coma at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 9

> I am afraid it is a bit complicated

That's why it's so important to post a small (<1 page), complete example program that demonstrates how setCursor fails to work for you.

When I have persistent bugs like this, I always create a small program to solve them in isolation. Try it.

Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 10
Ok, I will try. Thanks.
vpopovici@ro.soluziona.coma at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 11

In the future, Swing related questions should be posted in the Swing forum.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

> Yes, your code is working. But how can I pass through my problem in my code ?

Well, you take the time to understand the demo code and then incorporate the concepts into your program.

camickra at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 12
Don't forget to implement the mouseExited method as well.
floundera at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...
# 13
> Don't forget to implement the mouseExited method as well.I had one at first, in my example, but it behaved the same without one. Is it really needed in the case of cursors?
Hippolytea at 2007-7-12 10:28:36 > top of Java-index,Java Essentials,Java Programming...