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

Setting Tool Tip

Noel Alex
Greenhorn

Joined: Jul 25, 2011
Posts: 7

Hello,
I am trying to create a tool tip which will be displayed on painted text using graphics.
I am stuck because i do not know how to proceed.
i have achieved this so far:



The text "Show Me", "One", "Twozzzz" should be displayed as Tool Tip when hover.
I have tried using MouseMotionAdapter and then add the tool tip but it can not be displayed because the data "Show Me", "One", "Twozzzz" is moving and changes every time.
Hence when i try to display it again i can not!

Glad for any help or advice on this matter.


MWaNDuLu
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
The easy solution is to use JLabels instead of drawing the text yourself. Then you can just use the setToolTipText() method for each label.

If you want to paint each String then you need to override the getToolTipText(MouseEvent) method of the panel and caculate which word the mouse is positioned over and then set the tool tip text.
Noel Alex
Greenhorn

Joined: Jul 25, 2011
Posts: 7

Hello,
i did not really get and i tried in vain your solution.

But here is my sample implementation and i am really looking forward if someone could help me with at least new ideas on how to make this work.

The idea is that the values from method getString *forgot to change* have to be displayed when mouse is over component.. The values can change at anytime when the window has been re-sized just as in this one.

I would also like to make the size of the paintedComponent to be same as the size of the value from the getString()**

How can i make the toolTip work in this case??
I should display the tooltip when use is over lets say 0.21201 value??

Thanks for assistance in advance!

Unable to upload the files in any format!???





Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

One thing I noticed immediately is a missing call to super.paintComponent(g); at the start of your paintComponent method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I don't think you understand how Swing works.

The paintComponent() method will be called whenever Swing determines a component needs to be repainted. You painting code doesn't make sense because you calculate a random value. This logic should never exist as part of the painting code. It will change every time Swing decides to repaint the component.

Swing also supports borders, foreground and background colors. To use a JLabel for this you first need to make the label opaque and then your code would be something like:



The text color defaults to a shade of BLACK so you don't need to set that.

Thats all you need.

Again if this doesn't work for you then post a proper SSCCE that demonstrates your problems.
Noel Alex
Greenhorn

Joined: Jul 25, 2011
Posts: 7

Hello,
Thanks for the reply.
And thanks for the honestly good slap in the face...
Honestly, you are right... i have no idea how Swing works...
I took a Swing book and now i am reading it..

i will be back when i have my solution
Thanks for the honest response and your time..!!

Greetings!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting Tool Tip