JPanel's child components paint/layout problem

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-03-10T17:15:18Z Indexed on 2010/04/16 12:53 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

I'm having a problem that when my frame is shown (after a login dialog) the buttons are not on correct position, then in some miliseconds they go to the right position (the center of the panel with border layout).

When I make a SSCCE, it works correct, but when I run my whole code I have this fast-miliseconds delay to the buttons to go to the correct place.

Unfortunately, I can't post the whole code, but the method that shows the frame is:

public void login(JComponent userView) {
    centerPanel.removeAll();
    centerPanel.add(userView);
    centerPanel.revalidate();
    centerPanel.repaint();
    frame.setVisible(true);
}

What would cause this delay to the panel layout? (I'm running everything in the EDT)

-- update

In my machine, this SSCCE shows the layout problem in 2 of 10 times I run it:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TEST {

    public static void main(String[] args) throws Exception {

    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {

        System.out.println("Debug test...");

        JPanel btnPnl = new JPanel();
        btnPnl.add(new JButton("TEST"));

        JFrame f = new JFrame("TEST");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(btnPnl);
        f.pack();
        f.setSize(800, 600);
        f.setVisible(true);

        System.out.println("End debug test!");

        }
    });

    }

}

The button first appers in the up-left, and then it goes to the center. Please, note that I'm understand, not just correct. Is it a java bug?

--update

OK, so the SSCCE don't show the problem with you that tried till now. Maybe it's my computer performance problem. But this don't answer the question, I still think Java Swing is creating new threads for make the layout behind the scenes.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing