1

How do I switch between JPanels of different classes? I do not wish to use a Card Layout.

I have 2 classes - MainPage & MenuPage. For instance, I would like to clear the contentPane (a JPanel) @ MainPage and replace it with the content pane @ MenuPage. For testing purposes, I included a button @ MenuPage.

Please see my following attempt - it gives an error:

java.lang.IllegalArgumentException: adding a window to a container

MainPage

public class MainPage extends JFrame {

    private static JPanel contentPane;
    private JLabel imgBackground;
    private JLabel lblTitle;
    private JLabel imgLogo;
    private Dimension dim;

    //timer
    private final static int interval = 40;
    private int i;
    private Timer t;
    //private JButton start;
    //private JLabel lblLoading;
    private JProgressBar pbar;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPage frame = new MainPage();                    
                    frame.setVisible(true);             
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });     
    }

    public MainPage() {             
        dim = Toolkit.getDefaultToolkit().getScreenSize();      
        System.out.println(dim);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        contentPane.setBounds(0,0,dim.width,dim.height);
        setContentPane(contentPane);

        this.setExtendedState(JFrame.MAXIMIZED_BOTH);       

        t = new Timer (interval, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (i == 20){
                    t.stop();
                    //start.setEnabled(true);   


                    //refresh + load next page ???              
                    contentPane.removeAll();
                    MenuPage loadpanel2 = new MenuPage();
                    setContentPane(loadpanel2);


                    revalidate();                   
                    repaint();
                }
                else{
                    i++;
                    pbar.setValue(i);
                }               
            }           
        });
        t.start();

MenuPage

public class MenuPage extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MenuPage frame = new MenuPage();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public MenuPage() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        JButton btnSadfsafsa = new JButton("sadfsafsa");
        btnSadfsafsa.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnSadfsafsa.setBounds(10, 52, 89, 23);
        btnSadfsafsa.setEnabled(true);
        btnSadfsafsa.setVisible(true);
        contentPane.add(btnSadfsafsa);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
miiike test
  • 103
  • 6
  • 1
    Why don't you want to use a `CardLayout`? – James Taylor Jan 08 '15 at 00:28
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Jan 08 '15 at 00:40
  • .. 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 08 '15 at 00:44
  • 1
    "I want to drive nails into a board, but I do not want to use a hammer" - In essence, that is what you are saying. What I am proposing can be done. However, it is a lot easier to do with the proper tool. So one has to question why you wish to go in that direction. Card Layout was designed to do EXACTLY what you are proposing, but for some reason you do not want to use it. – hfontanez Jan 08 '15 at 00:47

1 Answers1

2
java.lang.IllegalArgumentException: adding a window to a container

That is pretty straightforward. Both GUIs extend JFrame and are therefore top level containers. We cannot add one top level container to another.

Instead of extending frame, both GUIs might extend JPanel. A JPanel (or more than one) can then be added to a JFrame instantiated in the main(String[]) or a showGUI() method.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you. If MainPage extends JFrame and MenuPage extends JPanel now, i did a repaint() and revalidate() but it still displays a grey screen, how do i resolve this? – miiike test Jan 09 '15 at 07:50
  • Ive done so @ http://stackoverflow.com/questions/27856290/grey-screen-on-repaint-and-revalidate. Please have a look thank you. – miiike test Jan 09 '15 at 08:14
  • Yes I've seen that. An MCVE should be one source file (possibly containing more than one class) with imports and one `main(String[])`.. – Andrew Thompson Jan 09 '15 at 08:24
  • Im sorry, was trying to illustrate the problem. I do not see what went wrong with the grey screen. please enlighten me. – miiike test Jan 09 '15 at 08:29