File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Event feedback loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event feedback loop" Watch "Event feedback loop" New topic
Author

Event feedback loop

Ian Calegory
Greenhorn

Joined: Feb 28, 2001
Posts: 3
Hello,
I'm catching a component's (in this case a JFrame) move event, and am changing the component's location, which fires off another move event, and so on. Does someone know how to suppress the subsequent move events? Thanks!
Bryan Fagan
Greenhorn

Joined: Feb 26, 2001
Posts: 17
Hi Ian:
I believe what you are looking for is the SwingUtilities.invokeLater method. The method waits for all events to occur and then executes your code. This means your componentMoved method is only executed once.
See code below.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class Test extends JFrame {
public Test() {
setSize(100,100);
setVisible(true);
addComponentListener(new ComponentListen());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});
}
public static void main(String[] args) {
Test tst = new Test();
}
}

class ComponentListen implements ComponentListener {
public void componentHidden(ComponentEvent ce) {
}

public void componentMoved(ComponentEvent me) {
SwingUtilities.invokeLater(new moveFrame(me));
}
// moveFrame inner class to move the frame.
class moveFrame implements Runnable {
ComponentEvent mo;
moveFrame(ComponentEvent mo) {
this.mo = mo;
}
public void run() {
Component com = (Component)mo.getComponent();
com.setLocation(500,23);
}
}
public void componentResized(ComponentEvent cce) {
}
public void componentShown(ComponentEvent cs) {
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event feedback loop