Urgent : Sustaining Mouse Over Event
Hi All,
Good Morning,
I will appreciate much if somebody would help me about how to sustain a mouse over event. I mean when mouse is over, a process should run continuously and stop when the mouse exits.
Similarly for mouse pressed and mouse exited events.
Waiting for reply. Two duke dollars for this. !!
Hi,
Morning? Your post states 6:15PM you must work nights.
How about the code below. It might give you some ideas.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class MouseOver extends JFrame implements Runnable
{
private JLabel eeLabel, peLabel;
private boolean mousePressed;
private boolean mouseInside;
private boolean running;
public MouseOver()
{
Container cont = getContentPane();
eeLabel = new JLabel( " ", JLabel.CENTER );
cont.add( eeLabel, "North" );
peLabel = new JLabel( " ", JLabel.CENTER );
cont.add( peLabel, "South" );
setSize( 400, 400 );
cont.addMouseListener(
new MouseAdapter()
{
public void mouseEntered( MouseEvent me )
{
//
// Start running the upper clock if it is not already running.
//
mouseInside = true;
System.out.println( running );
if( !running )
{
running = true;
Thread t = new Thread( MouseOver.this );
t.start();
}
}
public void mouseExited( MouseEvent me )
{
//
// Stop any running clock.
//
mouseInside = false;
mousePressed = false;
running = false;
}
public void mouseClicked( MouseEvent me )
{
//
// Toggle click to allow start/stop with mouse click.
// Start running the lower clock if it is not already running.
//
mousePressed = !mousePressed;
if( !running )
{
running = true;
Thread t = new Thread( MouseOver.this );
t.start();
}
}
});
addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent we )
{
System.exit( 0 );
}
});
}
//
// Method to update either or both clocks depending on user actions.
//
public void run()
{
SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss" );
while( mousePressed || mouseInside )
{
GregorianCalendar gc = new GregorianCalendar();
if( mousePressed )
{
peLabel.setText( sdf.format( gc.getTime() ) );
}
eeLabel.setText( sdf.format( gc.getTime() ) );
repaint();
try
{
Thread.sleep( 1000 );
}
catch( InterruptedException ie )
{
return;
}
}
}
public static void main(String[] args)
{
MouseOver mo = new MouseOver();
mo.setVisible( true );
}
}
Regards,
Manfred.