• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Tooltips

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope.
Just use the get/setToolTipText().
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah, but is it art? What do you think tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic