I have a tabbed view in my program and under each tab I have several panels which I rotate between with buttons. I’ve decided to implement a CardLayout for each one of these tabs, and given that I have about 7 tabs I decided to write a class to make things a bit neater. The class is called PanelSystem and it takes in JPanels which have already been created and adds them to a CardLayout. I will also implement a switchPanel method to move between panels. So far I have:
public class PanelSystem {
JPanel cards;
CardLayout cl;
public PanelSystem(JPanel...panels) {
// Create Panel with card layout
cards = new JPanel(new CardLayout());
// Add all the panels to the card system
for (JPanel p : panels) cards.add(p);
// Gains access to the card layout?
cl = (CardLayout)(cards.getLayout());
// Show starting card
cl.show(cards, *UNIQUE IDENTIFIER*);
}
}
Since there are different numbers of JPanels for each tab I had to implement the JPanels…panels line. I’m not sure if this works correctly yet, but the problem comes at the end of the constructor where I’m trying to show the first card since it doesn’t have a unique identifier because of the way I added them. Any thoughts on how I could fix this?? Thanks in advance guys!
You can access the varargs as if it were an array (which it is, anyway). How about if you loop through it with a classical for loop
then at the end
Edit: I’m rusty on Swing layouts, can’t remember exactly whether you need your identifier to be a String or just any Object, but you should be able to figure it out from here.