David Gay
David Gay

Reputation: 1154

How can I change the layout manager of my JFrame after startup?

Background: I'm working on a "GUI from Hell" program.

Problem: I need to change the layout manager of my JFrame from the constructor-set BorderLayout to FlowLayout.

Purpose: It's going to end up doing that many times really fast after a button is pressed, then end up on one layout manager or the other.

How should I accomplish this best? Here's some (working) code, if it helps at all:

if (goodVibes)
        {
            final Timer t = new Timer(100, null);
            ActionListener changeStyle = new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    int count = 100;

                    // Insert layout manager switching code here

                    count--;
                    if (count <= 0)
                        t.stop();
                }
            };
        }

Thanks in advance for any guidance!

Upvotes: 0

Views: 2949

Answers (1)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Presumably you are going to have all different components? So put the components on a JPanel for each layout. Switch with frame.setContentPane(panel);.

Alternatively, use java.awt.CardLayout to switch between panels.

Upvotes: 5

Related Questions