Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to dispaly tooltip for a swing component on events other then mouse rollover

299 views
Skip to first unread message

Praveen homkar via JavaKB.com

unread,
Feb 25, 2005, 12:46:35 AM2/25/05
to
I want to know how we can programmatically display the the tooltip
for a swing components on focusgain event(for example when a textfield
gains focus the tooltip must appear)

--
Message posted via http://www.javakb.com

Thomas Weidenfeller

unread,
Feb 25, 2005, 5:12:26 AM2/25/05
to
Praveen homkar via JavaKB.com wrote:
> I want to know how we can programmatically display the the tooltip
> for a swing components on focusgain event(for example when a textfield
> gains focus the tooltip must appear)

You can't in a simple way, since the tooltip system triggers exclusively
on mouse events (plus two keys for accessibility).

You would have to subclass ToolTipManager and override
registerComponent() to register listeners for focus events, too. Then
you have to ensure that your components only use an instance or
instances of your ToolTipManager subclass, instead of the
ToolTipManager.sharedInstance() instance. You would have to override
each and every method of your swing components which use that shared
instance.

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Andrey Kuznetsov

unread,
Feb 25, 2005, 11:50:36 AM2/25/05
to
> You would have to subclass ToolTipManager

this coud be difficult job since ToolTipManager has package protected
constructor!
I suggest that you try to create and post MouseEvent if your component gains
or loses focus.

--
Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


Message has been deleted
Message has been deleted

Thomas Weidenfeller

unread,
Feb 28, 2005, 5:19:20 AM2/28/05
to
Andrey Kuznetsov wrote:
>>You would have to subclass ToolTipManager
>
>
> this coud be difficult job since ToolTipManager has package protected
> constructor!

Oh, I should have spent a few more seconds with the API documentation.
Then there isn't much one can do with the ToolTipManager.

Praveen homkar via JavaKB.com

unread,
Mar 4, 2005, 6:58:03 AM3/4/05
to
Thank you thomas and andrew for ur replies.

I thing we can move the cursor programaticall when a component gains focus
,do u both feel, that will be an easy solution

Praveen homkar via JavaKB.com

unread,
Mar 4, 2005, 6:59:55 AM3/4/05
to
Hi andrew can u plz tell me how to post MouseEvent if component gains
or loses focus.

--

Andrey Kuznetsov

unread,
Mar 4, 2005, 4:02:19 PM3/4/05
to
> Hi andrew can u plz tell me how to post MouseEvent if component gains
> or loses focus.

Hi Praveen,

I looked at source of ToolTipManager and found better solution.
Instead of MouseEvent you should dispatch KeyEvent (CTRL + F1).

______________________________________

package com.imagero.gui.swing;

import javax.swing.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.*;

public class FocusTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 10,
10));

JTextField [] field = new JTextField[8];
for (int i = 0; i < field.length; i++) {
field[i] = new JTextField("" + i, 20);
final JTextField jTextField = field[i];

jTextField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
jTextField.dispatchEvent(
new KeyEvent(
jTextField, 401,
System.currentTimeMillis(), Event.CTRL_MASK, KeyEvent.VK_F1, '?', 1));
}
});
jTextField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println(e.getWhen());
System.out.println(e.getID());
System.out.println(e.getKeyChar());
System.out.println(e.getKeyCode());
System.out.println(e.getKeyLocation());
System.out.println(e.getModifiers());
}
});
jTextField.setToolTipText(jTextField.getText());
jTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jTextField.setToolTipText(jTextField.getText());
}
});
ToolTipManager.sharedInstance().registerComponent(jTextField);
frame.getContentPane().add(jTextField);
}
frame.pack();
frame.show();
}
}

______________________________________

Praveen homkar via JavaKB.com

unread,
Mar 7, 2005, 12:27:37 AM3/7/05
to
Hi Andrew,

thank you very much for your help.

I have used your idea in my application it works 100%.

Thank u one again

praveen

0 new messages