Search code examples
javaswingjpanelgridbaglayout

How to switch gridpaglayout panel on a jframe


I have a jframe, and 4 jpanel with gridbaglayout. I want to switch from a jpanel to another with a click on a button. But instead of getting the first panel, I get the last one. Here's what I did :

public static void main(String args[]){

        JFrame myframe = new JFrame();

        try {
            contentPane = new BackgroundPanel();
            contentPane.setLayout(new GridBagLayout());
            contentPane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            principalpane = new BackgroundPanel();
            principalpane.setLayout(new GridBagLayout());
            principalpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            optionspane = new BackgroundPanel();
            optionspane.setLayout(new GridBagLayout());
            optionspane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            datapane = new BackgroundPanel();
            datapane.setLayout(new GridBagLayout());
            datapane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            genpane = new BackgroundPanel();
            genpane.setLayout(new GridBagLayout());
            genpane.setBounds(0, 0, myframe.getWidth(), myframe.getHeight());
            //principalpane.setBackground(Color.cyan);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /***************************************************************************/
        /*****************************First*****************************************/
        GridBagConstraints gbc=new GridBagConstraints();
        GridBagConstraints gbcl=new GridBagConstraints();
        JLabel welcome=new JLabel("Optimizing & Visualizing Process Progression");
        welcome.setFont(new Font("Tahoma", Font.BOLD, 14));
        gbcl.gridx=0;
        gbcl.gridy=0;
        gbcl.gridwidth = 2;
        gbcl.gridheight=1;
        gbcl.insets=new Insets(10,200,5,100);
        gbcl.fill=GridBagConstraints.BOTH;
        principalpane.add(welcome, gbcl);


        gbc.gridx=0;
        gbc.gridy=1;
        gbc.weightx = 1;
        gbc.weighty = 1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(10,10,10,10);
        try {
            principalpane.add(Mypanel.secondpane(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        gbc.gridx=1;
        gbc.gridy=1;

    //  gbc.anchor=GridBagConstraints.NORTHEAST;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(10,10,10,10);
        try {
            principalpane.add(Mypanel.firstpanel(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        gbc.gridx=0;
        gbc.gridy=2;
        gbc.gridwidth = 2;
        gbc.gridheight=2;
    //  gbc.anchor=GridBagConstraints.SOUTH;
        gbc.fill=GridBagConstraints.BOTH;
        gbc.insets=new Insets(0,10,5,10);
        try {
            principalpane.add(Mypanel.thirdpanel(), gbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /******************Second ***************************/
        GridBagConstraints ogbc=new GridBagConstraints();
        GridBagConstraints ogbcl=new GridBagConstraints();

        JLabel optwelcome=new JLabel("Abnormal Process Progression");
        optwelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
        ogbcl.gridx=0;
        ogbcl.gridy=0;
        ogbcl.gridwidth = 2;
        ogbcl.gridheight=1;
        ogbcl.insets=new Insets(10,280,5,10);
        ogbcl.fill=GridBagConstraints.BOTH;
        optionspane.add(optwelcome, ogbcl);

        gbc.gridx=0;
        ogbc.gridy=1;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.forthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=1;
        ogbc.gridy=1;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbcl.gridwidth = 1;
        ogbc.gridheight=2;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.sixthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=0;
        ogbc.gridy=2;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbc.gridheight=1;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.fifthpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ogbc.gridx=0;
        ogbc.gridy=3;
        ogbc.weightx = 1;
        ogbc.weighty = 1;
        ogbc.gridwidth=2;
    //  gbc.anchor=GridBagConstraints.NORTHWEST;
        ogbc.fill=GridBagConstraints.BOTH;
        ogbc.insets=new Insets(10,10,10,10);
        try {
            optionspane.add(Mypanel.seventhpanel(), ogbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*************************Third*************************************/
        GridBagConstraints dgbc=new GridBagConstraints();
        GridBagConstraints dgbcl=new GridBagConstraints();

        JLabel datawelcome=new JLabel("Please Enter the QoS' values");
        datawelcome.setFont(new Font("Ubuntu", Font.BOLD, 16));
        dgbcl.gridx=0;
        dgbcl.gridy=0;
        dgbcl.gridwidth = 1;
        dgbcl.gridheight=1;
        dgbcl.anchor=GridBagConstraints.NORTH;
        dgbcl.insets=new Insets(0,250,35,10);
        dgbcl.fill=GridBagConstraints.BOTH;
        datapane.add(datawelcome, dgbcl);


        dgbc.gridx=0;
        dgbc.gridy=0;
        dgbc.weightx = 1;
        dgbc.weighty = 1;
        dgbcl.anchor=GridBagConstraints.CENTER;
        dgbc.fill=GridBagConstraints.BOTH;
        dgbc.insets=new Insets(5,10,10,5);
        try {
            datapane.add(Mypanel.eighthpanel(), dgbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        dgbc.gridx=0;
        dgbc.gridy=1;
        gbc.anchor=GridBagConstraints.SOUTH;
        dgbc.fill=GridBagConstraints.BOTH;
        dgbc.insets=new Insets(5,0,5,5);
        try {
            datapane.add(Mypanel.ninethpanel(), dgbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    /****************Fourth*************************************/
        GridBagConstraints ggbc=new GridBagConstraints();

        ggbc.gridx=0;
        ggbc.gridy=0;
        ggbc.weightx = 1;
        ggbc.weighty = 1;
        ggbc.anchor=GridBagConstraints.CENTER;
        ggbc.fill=GridBagConstraints.BOTH;
        ggbc.insets=new Insets(5,10,10,5);
        try {
            genpane.add(Mypanel.tenthpanel(), ggbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        ggbc.gridx=0;
        ggbc.gridy=1;
        ggbc.anchor=GridBagConstraints.SOUTH;
        ggbc.fill=GridBagConstraints.BOTH;
        ggbc.insets=new Insets(5,0,5,5);
        try {
            genpane.add(Mypanel.eleventhpanel(), ggbc);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        GridBagConstraints cgbc=new GridBagConstraints();

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        principalpane.setVisible(true);
        contentPane.add(principalpane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        optionspane.setVisible(false);
        contentPane.add(optionspane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        datapane.setVisible(false);
        contentPane.add(datapane, cgbc);

        cgbc.gridx=0;
        cgbc.gridy=0;
        cgbc.weightx = 1;
        cgbc.weighty = 1;
        cgbc.anchor=GridBagConstraints.CENTER;
        cgbc.fill=GridBagConstraints.BOTH;
        genpane.setVisible(false);
        contentPane.add(genpane, cgbc);

        contentPane.setVisible(true);
        myframe.getContentPane().add(contentPane);

        myframe.pack();
        myframe.setLocationRelativeTo(null);
        myframe.setSize(new Dimension(800, 600));

        myframe.setVisible(true);
        myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

Solution

  • Too much code to understand what you are doing but a GridBagLayout is used for static layouts where the components don't dynamically change.

    I want to switch from a jpanel to another with a click on a button.

    Sound to me like you should be using a Card Layout which will only display one panel at a time and allow your to swap panels are required.

    The tutorial example shows how to swap panels when an ActionEvent is generated.