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. !!

[340 byte] By [ravisinha2001a] at [2007-9-27 8:49:04]
# 1

I guess you could write a global variable flag to indicate start and stop of a thread(process)

in the mouseEntered function

process=true;

in the mouseExited function

process=false;

but this has some problem because a thread doesn't stop(thread==null) when is told to because it only suggests.

joeylta79a at 2007-7-8 17:24:10 > top of Java-index,Archived Forums,Swing...
# 2

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.

leonhmha at 2007-7-8 17:24:10 > top of Java-index,Archived Forums,Swing...
# 3

Hi Manfred,

Thanks a lot for the code and it is very enlightening for any further ideas. I too was thinking on the same lines. Actually what I have to implement is a text scroller(both ways), depending on the button on which the mouse over is there.

I was just wondering whether we could do without a Thread(messy business) And should we write to SUN to provide a mouse over event. Ha Ha. You example code was(is) admirably succinct and useful.

Thanks again

Ravi

ravisinha2001a at 2007-7-8 17:24:11 > top of Java-index,Archived Forums,Swing...