473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Mauslisener

Hallo,
ich möchte einen Mauslistner der bei gepresster Maustaste eine
bestimmte Methode in einer schleife ausführt.
jetzt habe ich die while schleife eingebaut aber das ganze läuft zu
schnell so das der
Mausreleased nicht mehr reagiert.

Kann mir jemand weiterhelfen wie ich eine Schleife verlangsame.

Danke für eure hilfe
anbei code:
private void configureMyCustomSpinboxMouseListener()
{
fldItemQuantity.addMouseListener( new MouseListener() {
boolean pressed = false;
// mouse listener code follows here

public void mouseClicked( MouseEvent e ) {}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mousePressed( MouseEvent e )
{
// Klickhöhe ist in e.getY(), Feststellung der
Maustaste aus e.getModifiers
// Wenn obere Hälfte angeklickt ist, bedeutet das
Stückzahl erhöhen, wenn untere, erniedrigen.
if ( isMouseButton1( e ) ) {
OldMatom4ItemCollection containingCollection =
( (MatomguiConfigboxForEntrytypeQuantityExtensible2) getParent()
).itemDataCollection;

if ( isPosInUpperHalf( e ) )
{
//Hier setzte ich den Wert pressed auf true damit die Schleife
losläuft
pressed = true;

while(pressed)
{
performIncrement();
}
}
else
{
performDecrement();
}
}
}

//Invoked when a mouse button has been released on a
component.
public void mouseReleased( MouseEvent e )
{
if ( ( e.getModifiers() & InputEvent.BUTTON1_MASK
) != 0 )
{
adjustArrowIconAsInactive();
invalidate();
validate();
//Hier setzte ich den Wert pressed auf false damit die Schleife stoppt
pressed = false;

}
}

private boolean isPosInUpperHalf( MouseEvent e )
{
return ( e.getY() < ( getHeight() / 2 ) );
}

// end mouse listener code
} // added anonymous mouse listener
); // addMouseListener
}
Jul 17 '05 #1
1 1892
You may want to repost your question in English.

But to answer your question: the fatal flaw is in the while loop in
mousePressed - it never returns. This actually has nothing to do with how
fast the loop runs; even if you put in a sleep, it still would be
happening. Java can only process one event at a time, and won't process
another one until the first one has completed processing - which in your
code never happens.

Instead, use the mousePressed and mouseReleased methods to simply set and
clear a flag, respectively. Add a timer and have that perform the
increment if the flag is set. I don't think you can do it with an
anonymous mouse listener, though; you'll need a bit more code.

Oh - and IF it was possible to use multiple threads for event handling,
there still would be a flaw: the access to the pressed variable would have
to be synchronized.

On Tue, 29 Jul 2003 05:55:04 -0700, jo****@web.de wrote:
Hallo,
ich möchte einen Mauslistner der bei gepresster Maustaste eine
bestimmte Methode in einer schleife ausführt. jetzt habe ich die while
schleife eingebaut aber das ganze läuft zu schnell so das der
Mausreleased nicht mehr reagiert.

Kann mir jemand weiterhelfen wie ich eine Schleife verlangsame.

Danke für eure hilfe
anbei code:
private void configureMyCustomSpinboxMouseListener() {
fldItemQuantity.addMouseListener( new MouseListener() {
boolean pressed = false;
// mouse listener code follows here

public void mouseClicked( MouseEvent e ) {} public void
mouseEntered( MouseEvent e ) {} public void
mouseExited( MouseEvent e ) {} public void
mousePressed( MouseEvent e ) {
// Klickhöhe ist in e.getY(), Feststellung der
Maustaste aus e.getModifiers
// Wenn obere Hälfte angeklickt ist, bedeutet das
Stückzahl erhöhen, wenn untere, erniedrigen.
if ( isMouseButton1( e ) ) {
OldMatom4ItemCollection containingCollection =
( (MatomguiConfigboxForEntrytypeQuantityExtensible2) getParent()
).itemDataCollection;

if ( isPosInUpperHalf( e ) )
{
//Hier setzte ich den Wert pressed auf true damit die Schleife losläuft
pressed = true;

while(pressed)
{
performIncrement();
}
}
else
{
performDecrement();
}
}
}

//Invoked when a mouse button has been released on a
component.
public void mouseReleased( MouseEvent e ) {
if ( ( e.getModifiers() & InputEvent.BUTTON1_MASK
) != 0 )
{
adjustArrowIconAsInactive();
invalidate();
validate();
//Hier setzte ich den Wert pressed auf false damit die Schleife stoppt
pressed = false;

}
}

private boolean isPosInUpperHalf( MouseEvent e ) {
return ( e.getY() < ( getHeight() / 2 ) );
}

// end mouse listener code
} // added anonymous mouse listener
); // addMouseListener
}


--
Keep American Families united! Support H.R. 539 and H.R. 832
For more information, see http://www.kkeane.com/lobbyspousal-faq.shtml

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.