• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Event feedback loop

 
Ian Calegory
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic