Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tooltips

 
Marc Beck
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marc Beck
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope.
Just use the get/setToolTipText().
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I figured it out on my own when I looked on the code again shortly after my last post. But thanks for trying.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic