0

I have a simple java gui (code below) which for some reason when displayed, will "jump" back to it's original position the first time I try to move or resize it. So basically I will have to move the gui twice to get it to move once because as soon as I release the mouse the first time it snaps back to where it originally came up.

import javax.swing.*;

public class JFrameTester {

  public static void main(String[] args) {

    JFrame f = new JFrame("A JFrame");
    f.setSize(250, 250);
    f.setLocation(300,200);
    f.getContentPane().add(new JTextArea(10, 40));    
    //f.pack();    
    f.setVisible(true);
    //f.validate();
  }

}

I am running on GNU Linux with java 1.6. I am exporting the display back to my Windows machine and wondering if it has something to do with the X11 forwarding because it does not show this behavior when I run the gui in Windows. However, when I run this gui on a Fedora Linux box (with java 1.7) it does not show this behavior at all - whether exporting the display or not.

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
user1585643
  • 201
  • 1
  • 2
  • 8
  • I would suspect the Xserver running on your Windows PC. Can you run the same app from the GNU/Linux Java 1.6 machine, and remotely display it on a different (non-windows) machine? – dashrb Nov 15 '12 at 21:37
  • 2
    1) The GUI should be started and updated on the EDT. 2) Always call `pack()` in order to cause the GUI to be laid out and validated. 3) `f.setSize(250, 250);` in counter-productive. Set the size of the GUI according to the rows and columns needed for the text area. – Andrew Thompson Nov 16 '12 at 02:26
  • 1
    Also please check the text of tags carefully before applying them to a question. This has ***nothing*** to do with [tag:export] as defined by SO. – Andrew Thompson Nov 16 '12 at 02:29
  • @dasrb I have tried from other machines and get the same behavior however they are all Windows – user1585643 Nov 16 '12 at 14:04

1 Answers1

4

Several problems are apparent:

  • Construct and manipulate Swing GUI objects only on the event dispatch thread. Failing to do so creates a race condition that may be obscure until a different platform or network latency exposes it.

  • Use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." Failing to do so causes the invalid Container to be validated when moved or resized. When the prematurely visible components move to the defined positions, they appear to "jump."

  • Make setVisible() the last operation that affects the initial appearance.

The following example combines these suggestions:

import java.awt.EventQueue;
import javax.swing.*;

public class JFrameTester {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("A JFrame");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new JScrollPane(new JTextArea(10, 40)));
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}
trashgod
  • 200,320
  • 28
  • 229
  • 974