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

JButtons inside JTooltip

Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32
Hi. Is it possible to have cca. 6 buttons inside JToolTip?
I would really appreciate all the help and tips I can get.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I've tried it, and the answer is no. Even though JToolTip extends JComponent and therefore indirectly Container, thereby giving it all the methods to add and remove child components, these are ignored.

You'll have to do the hard work yourself. Create a JWindow with the JButtons, then use a MouseListener (mouseEntered) for the parent component to find out when to show this JWindow and a MouseListener (mouseExited) for the JWindow itself to find out when to close it. You could check the source code for JToolTip and ToolTipManager (both available in the src.zip file in your JDK folder) to see how it's implemented for those.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Rob Spoor wrote:I've tried it, and the answer is no. Even though JToolTip extends JComponent and therefore indirectly Container, thereby giving it all the methods to add and remove child components, these are ignored.


Actually, the JToolTip has a null layout, so any component added without specifying its bounds isn't seen. Setting a layout manager to the tooltip doesn't resize it either, so that too must be taken care of in client code. Third, a non-null tool tip text must be set to register with the tool tip manager. An empty String suffices for this.

This code can probably be improved on, but it's provided as a proof of concept.
I do suspect that what Andraz is really after is a JPopupMenu.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Darryl Burke wrote:
Rob Spoor wrote:I've tried it, and the answer is no. Even though JToolTip extends JComponent and therefore indirectly Container, thereby giving it all the methods to add and remove child components, these are ignored.


Actually, the JToolTip has a null layout, so any component added without specifying its bounds isn't seen. Setting a layout manager to the tooltip doesn't resize it either, so that too must be taken care of in client code. Third, a non-null tool tip text must be set to register with the tool tip manager. An empty String suffices for this.

This code can probably be improved on, but it's provided as a proof of concept.

I stand corrected. I didn't look deep enough to find the null layout manager.

I do suspect that what Andraz is really after is a JPopupMenu.

If he wants something that looks like your code then I agree. The only difference with usual JPopupMenu handling is that the popup should be trigged by a mouseEntered event, not a mouseClicked event.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Hmmm...

I'll be away from a computer for a few hours, but can anyone tell me why
1) JToolTip doesn't generate ComponentEvents? My initial attempt used a ComponentListener, but to no avail.
2) If the commented popupMenu.setVisible(false) is uncommented, the popup is shown repeatedly as long as it is shown in a heavyweight window (i.e. as long as the mouse pointer is kept near enough to the right or bottom of the JLabel/JFrame). After once being shown as a lightweight component in the same JFrame (or on its glass pane, or whatever...), the createToolTip() method isn't triggered any more, as shown by the sysout not printing to the console.
Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32
JPopup could work for me, but I have one more problem.
Triggering popup with left mouse button instead of right one.....
Any idea how this could be done
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

A JPopupMenu is never called automatically, you always have to do this yourself. You usually use a MouseListener and listen for the mouseClicked event. Just check which button is pressed:
Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32


I tryed adding (event.getButton() == MouseEvent.BUTTON1), but it doesnt work..... Only works on right mouse button.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Andraz Poje wrote:JPopup could work for me, but I have one more problem.
Triggering popup with left mouse button instead of right one.....


And why is that a requirement? When you decide to violate the standard user interface design, you should at least explain why it's necessary. And that's what you are doing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Andraz Poje wrote:I tryed adding (event.getButton() == MouseEvent.BUTTON1), but it doesnt work..... Only works on right mouse button.

No offence, but duh. e.isPopupTrigger() on Windows only returns true for right mouse buttons. So you check if the left mouse button is clicked inside an if-statement that already said that only the right mouse button was clicked.

Move that inner if to outside the other if and try again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37979
    
  22
I think this thread would sit better on our GUIs forum. Moving.
Andraz Poje
Ranch Hand

Joined: Sep 25, 2008
Posts: 32
Paul Clapham wrote:And why is that a requirement? When you decide to violate the standard user interface design, you should at least explain why it's necessary. And that's what you are doing.


Sorry. I am programming some rundown list of students which is on touchscreen monitor.

Rob Spoor wrote:Move that inner if to outside the other if and try again..

Thanks, that works.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Andraz Poje wrote:I am programming some rundown list of students which is on touchscreen monitor.


Does a tooltip even get displayed as a result of touching a touchscreen? This is a genuine question, the only touchscreens I've worked with are on phones, and there's no such thing as a tooltip.

For your problem, it's best to go with the popup menu.
 
 
subject: JButtons inside JTooltip
 
Similar Threads
Customizing JToolTip
JTooltip with a vertical scrollbar?
JToolTip Problems
JToolTip
JToolTip in an applet