Reputation: 1692
I created a array of JPanels which contains a common JLabel
class gui
{
JPanel[] multpanel;
JPanel finalPane = new JPanel();
JLabel InputLabel = new JLabel("Input Files");
gui()
{
InputLabel.setLocation(50,50);
InputLabel.setSize(120,20);
int total_instances=2;
multpanel=new JPanel[total_instances];
for(int instance=0;instance<total_instances;instance++)
{
multpanel[instance]=new JPanel();
multpanel[instance].setLocation(10,0);
multpanel[instance].setSize(500,500);
multpanel[instance].setLayout(null);
multpanel[instance].add(InputLabel);
}
finalPane.add(multpanel[0]);
finalPane.add(multpanel[1]);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getContentPane().add(finalPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}
this a short version of my program, i creating array of panels and at a time only one panel is visible My problem is that it only display last panel in array, in my case 2nd panel of array is displayed and when i try to display first panel it displays nothing
like if i have panel array of size five then only 5th panel is displayed and all other panels display blank
Is this because i am adding a common label in it
Please help
Upvotes: 0
Views: 1051
Reputation: 57381
Use proper LayoutManager e.g. BoxLayout and don't set size and location of the panels.
setLayout(null);
<-- would not recomment to use that.
Upvotes: 2
Reputation: 691715
A given component may only have one ancestor. So, when you add a label to a panel, you're effectively removing it from the previous one. If you want a label in 5 panels, you need 5 labels.
Two additional notes:
Upvotes: 2