1

I am working on a program using a JPanel to show a block of text,buttons,etc. and I am trying to set a JPanel equal to another one that i created but its not doing anything. Ignore my buttons and crap.

I have the panel declared publicly

    public JPanel panel;

And Here is its stuff...

    panel = new JPanel();
    panel.setBackground(PanelColor);
    panel.setBounds(0, 93, 594, 478);
    contentPane.add(panel);
    panel.setLayout(null);

I want to make it so when i hit a button (in this case: btnKinematics) it changes a the panel to a different class i have called KinematicsPanel.java

    btnKinematics = new JButton("Kinematics");
    btnKinematics.setFont(ButtonFont);
    btnKinematics.setFocusPainted(false);
    btnKinematics.setBorder(compound);
    btnKinematics.setBackground(Color.WHITE);
    btnKinematics.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            highlite(btnKinematics);
            panel = new KinematicsPanel();
            panel.setBackground(Color.WHITE);
        }
    });
    btnKinematics.setBounds(14, 60, 130, 23);
    contentPane.add(btnKinematics);

Thanks

UPDATE: FIXED

I made some changes and this worked

    contentPane.remove(panel);
    contentPane.add(new KinematicsPanel());
    contentPane.repaint();

thanks

  • You appear to be making a basic newbie mistake: you're confusing variable with object. I'm guessing that the panel JPanel is displayed in your GUI somewhere, and in the ActionListener your attempting to replace what is currently displayed with another JPanel, but the problem is you're simply swapping the panel variable's reference which has absolutely no effect on the currently displayed object. To do that you need to remove the old JPanel from its container, add the new one and then revalidate and repaint the container – Hovercraft Full Of Eels May 04 '17 at 23:44
  • -- or even better, use a [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Hovercraft Full Of Eels May 04 '17 at 23:44

0 Answers0