The Artima Developer Community
Sponsored Link

Legacy Design Forum
Event Generator Idiom



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


how to fake mouse event

Posted by Shay Gaghe on October 22, 2001 at 6:22 PM

Im using Java 1.3 for developing a Swing interface that contains a label and a button. The label associate with ToolTip. I need to perform a simple task , I want the ToolTip to be displayed when I press on the button (instead of mouse over on the label).

I know that I have to fake a mouse event on the label to get this trick to work. I did it with the following code but its not work:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NewToolTipTest extends JFrame
private JLabel l;
private JButton showTip;

public NewToolTipTest()
Image i = Toolkit.getDefaultToolkit().getImage( "images/miri.gif" );
ImageIcon ii = new ImageIcon( i );
l = new JLabel( ii );
l.setToolTipText( "Look, it's a ToolTip!" );
getContentPane().add( l, BorderLayout.CENTER );

showTip = new JButton( "Show Tip" );
// Don't give the button a tooltip! It will replace the
// tooltip you want to show!
//showTip.setToolTipText( "Push Me!" );
showTip.addActionListener( new TipActivate() );
getContentPane().add( showTip, BorderLayout.SOUTH );

addWindowListener( new ExitHandler() );

public static void main( String[] args )
NewToolTipTest nttt = new NewToolTipTest();
nttt.setVisible( true );

private class ExitHandler extends WindowAdapter
public void windowClosing( WindowEvent event )
System.exit( 0 );

private class TipActivate implements ActionListener
public void actionPerformed( ActionEvent event )
ToolTipManager theManager = ToolTipManager.sharedInstance();
MouseEvent toolTipEvent = new MouseEvent( l, MouseEvent.MOUSE_MOVED,
System.currentTimeMillis(), 0, 0, 0, 0, false );
theManager.mouseMoved( toolTipEvent );

System.out.println( "ActionEvent launched!");
whats wrong?

Any replies will be best appreciated


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us