Components are not longer resizable after moving

Posted by Junior Software Developer on Stack Overflow See other posts from Stack Overflow or by Junior Software Developer
Published on 2010-06-15T16:22:52Z Indexed on 2010/06/15 20:32 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

Hi guys

My question relates to swing programming. I want to enlarge a component (component x) by removing it from its parent panel (component a) and adding it in one of component a's parent (component b). Before that, I call setVisible(false) on all components in b. After that I want to make this back by removing it from b and adding on a.

After that all components are not longer resizable.

Why that?

An easy example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class SwingTest {

    private static ViewPanel layer1;
    private static JFrame frame;
    private static JTabbedPane tabbedPane;
    private static ViewPanel root;

    public static void main(String[] args) {
        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setMinimumSize(new Dimension(800, 600));

        root = new ViewPanel();
        root.setBackground(Color.blue);
        root.setPreferredSize(new Dimension(400, 600));
        root.setLayout(new BorderLayout());
        root.add(new JLabel("blue area"));

        layer1 = new ViewPanel();
        layer1.setBackground(Color.red);
        layer1.setPreferredSize(new Dimension(400, 600));
        layer1.setLayout(new BorderLayout());

        tabbedPane = new JTabbedPane();
        tabbedPane.add("A", new JLabel("A label"));
        tabbedPane.setPreferredSize(new Dimension(400, 600));

        layer1.add(tabbedPane);

        root.add(layer1);
        frame.add(root, BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(8000);
                    System.out.println("start");
                    for (Component c : root.getComponents()) {
                        c.setVisible(false);
                    }
                    layer1.remove(tabbedPane);
                    root.add(tabbedPane);

                    Thread.sleep(8000);

                    root.remove(tabbedPane);
                    layer1.add(tabbedPane);

                    for (Component c : root.getComponents()) {
                        c.setVisible(true);
                        c.repaint();
                    }
                } catch (InterruptedException e) {
                    //...
                }
            }
        };
        t.start();
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about swing