aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Tooltips Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Tooltips" Watch "Tooltips" New topic
Author

Tooltips

Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
I would like to add a tooltip to a JLabel. I read the tooltip tutorial: web page.
I wrote the following code and it is just being ignored. No error message or anything.


I would like for it to display the name of the object on that icon in a tooltip when hovering over it.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Marc Beck:


I would like for it to display the name of the object on that icon in a tooltip when hovering over it.[/QB]


You really dont need to take this approach. The JComponent has a getToolTipText() method which returns a String which is displayed as the tooltip. This method is called internally whenever the tooltip is to be displayed. All JComponents are registered with the ToolTipManager by default.

So, what you need to do is make your IconLabel extend the JLabel or some other JComponent. In the constructor, you can accept the object which is to be represented by this IconLabel. Then you just override the getToolTipText() which will return the appropriate String based on the Object.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
My IconLabel is already an instance of JLabel. Do I have to add something in the MouseListener in order to display the tooltip when the mouse is moved over the icon?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Nope.
Just use the get/setToolTipText().
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I want to display my button in a label.here is my code.pls help me what i have to do further?

Code:

public class AstMonitor extends JApplet implements ActionListener, Runnable {
Thread astThread;
JButton pause= new JButton(spause);

public void init() {
GridLayout gl;
getContentPane().setLayout(new GridLayout(3,10));
pause.setActionCommand("pause");
pause.addActionListener(this);
this.getContentPane().add(pause);
pause.setMnemonic('p');
pause.setToolTipText("press pause");
pause.setBackground(Color.yellow);

astThread = new Thread(this);
astThread.start();
}
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
Thanks for trying to help. I have been very busy with other things and just got back to this problem. I am using the setTooltipText() method, but it is being completely ignored. Am I missing something? Where do I need to put the getToolTipText?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Marc Beck:
I am using the setTooltipText() method, but it is being completely ignored. Am I missing something? Where do I need to put the getToolTipText?


Usually its something like label.setToolTipText("This is a tool tip");
Why dont you post your code where you are setting the tool tip so we can figure out whats missing?
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
The code is part of a game that is about 4000 lines and I've been working on it for over a year.



The picture of the planet is displayed fine. I would like for the tooltip to show the name of the planet when the player moves the cursor over the picture, so I can get rid of the NameLabel.
Marc Beck
Ranch Hand

Joined: Sep 09, 2006
Posts: 36
Nevermind, I figured it out on my own when I looked on the code again shortly after my last post. But thanks for trying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tooltips