user1296265
user1296265

Reputation: 999

Adding JPanels to JPanel array

I have declared an array:

private javax.swing.JPanel[] panelArray = new javax.swing.JPanel[3];

I also have 3 panels: panel0, panel1 and panel2. Can I add these panels to the array? i.e

panelArray[0] = panel0;
panelArray[1] = panel1;
panelArray[2] = panel2;

And then manipulate the arrays like this?

boolean[] myBools; .... then set them as true/false
for(int i=0; i<3; i++)
{
    if(myBools[i])
        panelArray[i].setVisible(true)
}

Because that does not work for me

Upvotes: 1

Views: 12686

Answers (3)

nIcE cOw
nIcE cOw

Reputation: 24616

On my side it's working fine, in this program :

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class MyPanel
{   
    private JPanel[] panelArray = new JPanel[3];
    private boolean[] myBools = new boolean[]{false, false, false};
    private int counter = 0; 
    private int prvPanelCounter = 0;
    private Timer timer;
    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            counter++;
            if (counter > 2)
                counter = 0;
            myBools[counter] = true;
            for (int i = 0; i < 3; i++) 
            {
                if (myBools[i])
                {
                    panelArray[i].setVisible(myBools[i]);                   
                    panelArray[prvPanelCounter].setVisible(myBools[prvPanelCounter]);
                    myBools[i] = false; 
                    prvPanelCounter = i;
                    break;
                }
            }
        }
    };

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Locate Mouse Position");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel0 = new JPanel();
        panel0.setOpaque(true);
        panel0.setBackground(Color.BLUE);       
        JPanel panel1 = new JPanel();
        panel1.setOpaque(true);
        panel1.setBackground(Color.RED);
        JPanel panel2 = new JPanel();
        panel2.setOpaque(true);
        panel2.setBackground(Color.DARK_GRAY);

        panelArray[0] = panel0;
        panelArray[1] = panel1;
        panelArray[2] = panel2;

        JComponent contentPane = (JComponent) frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 1));
        frame.add(panel0);  
        frame.add(panel1);  
        frame.add(panel2);  
        panel0.setVisible(myBools[counter]);
        panel1.setVisible(myBools[counter]);
        panel2.setVisible(myBools[counter]);
        frame.setSize(300, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String\u005B\u005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new MyPanel().createAndDisplayGUI();
            }
        });
    }
}

Upvotes: 6

Conor Pender
Conor Pender

Reputation: 1081

What you want to do can be done, but here's a few points to bear in mind:

  1. Make sure you initialise the JPanels before referencing them.
  2. The statement "panelArray[i].setVisible(true)" needs a semicolon after it.
  3. None of these panels will be visible unless you add them to another component, such as a JFrame.
  4. Rather than state javax.swing.JPanel, you could just import the JPanel at the top of the page and refer to it as simply JPanel.
  5. Your "if" statement is unnecessary. Just do .setVisible(myBools[i]);

Hope these were of some help to you.

Upvotes: 5

Rob I
Rob I

Reputation: 5737

Yes, you can. Did you really not initialize the myBools array with new ?

Upvotes: 1

Related Questions