how to show tooltip on mouse right click...

Hi everyone!

I'm trying to show a tooltip when the user clicks on a button with the mouse right button but I'm having some problems starting, for example, with the tooltip position. For example, look at the the following example(without mouse listener yet):

publicclass testextends JFrame{

private JToolTip jtp =new JToolTip();

private JButton button =new JButton("a button");

private JPanel panel =new JPanel();

public test(){

add(panel);

panel.add(button);

jtp.setTipText("a tooltip");

jtp.setBounds(10,10,10,10);

jtp.setVisible(true);

panel.add(jtp);

}

publicstaticvoid main(String[] args){

test x =new test();

x.setVisible(true);

}

}

Why the setBounds

doesn't set the position? In fact it doesn't do anything.

Is it possible to the tooltip to partially overlap the button. As far I checked they only stay side by side.

I hope you understand my english...

Filipe

[1759 byte] By [filipea] at [2007-11-26 16:50:14]
# 1

Use this code to show custom tool tip.

You should also handle the hiding of the tool tip.

JButton button = new JButton("test");

button.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

if (e.getButton() == MouseEvent.BUTTON3) {

JButton button = (JButton) e.getSource();

JToolTip tooltip = button.createToolTip();

PopupFactory popupFactory = PopupFactory.getSharedInstance();

tooltip.setTipText("a tooltip");

final Popup tooltip1 = popupFactory.getPopup(button, tooltip,

button.getLocationOnScreen().x + e.getX(),

button.getLocationOnScreen().y + e.getY() + 20);

tooltip1.show();

}

}

});

Rodney_McKaya at 2007-7-8 23:17:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

It was posted by Camickr

Action toolTipAction = getActionMap().get("postTip");

if (toolTipAction != null) {

ActionEvent postTip = new ActionEvent(FoldEditor.this, ActionEvent.ACTION_PERFORMED, "");

toolTipAction.actionPerformed(postTip);

}

See also

public Point getToolTipLocation(MouseEvent event) {

method of JComponent. Just override ithe method of your button.

regards,

Stas

StanislavLa at 2007-7-8 23:17:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
hi there!Sorry if I'm replying too late. I'm newbie in this forum and I didn't know how it works so I thought nobody replied me.Tanks!Filipe
filipea at 2007-7-8 23:17:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

Tried this but there are some problems if the popup windows gets out of main window of java application:

1) if the component inside popup has some transparent area it is not corectly painted in this case.

2) if main application window is resized the popup becomes invisible.

Is there any way to address these problems ?

sandi_roa at 2007-7-8 23:17:51 > top of Java-index,Desktop,Core GUI APIs...