0

I am creating a tab pain in a Class called Dashboard which holds 'filler' panels in the tabs. I am wondering if there is a way to create a new Dashboard and change the panels that I stored in the tabs. I'm not sure if this is the right way to explain it, but here is some code.

public class Dashboard{

    public Dashboard(){
         tabPane = new JTabbedPane();
     panel1 = new JPanel();
         panel2 = new JPanel();
         panel3 = new JPanel();
     panel1.add(new JLabel("This is the first panel"));
         panel2.add(new JLabel("This is the second panel"));
         panel3.add(new JLable("This is the third panel"));
         tabPane.add("One", panel1);
         tabPane.add("Two", panel2);
         tabPane.add("Three", panel3);
    }

I now want to make a new class that creates and instance of the Dashboard, but changes what panels show up in the tabs. I was trying something like this:

  public class Changer{
      public Changer(){
         Dashboard d = new Dashboard();
         // assuming I have getters and setters in the above class and that the 
         // panels are fields in Dashboard
         JPanel new = new JPanel();
         d.setPanel1(new);
      }
  }

I'm not sure if this is possible or if there is another way of doing so.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ola
  • 882
  • 2
  • 12
  • 29
  • `CardLayout` as seen in [this e.g.](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Oct 03 '13 at 01:18
  • @AndrewThompson can I add panels to a CardLayout panel? Because the panels I need to change between will have multiple components on each – ola Oct 03 '13 at 01:22
  • 1
    *"can I add panels to a CardLayout panel?"* Of course. One panel with one card layout can hold multiple cards which are themselves panels. Those panels can in turn have any layout, and contain any components that can be added to a panel (including other panels with their own layouts). – Andrew Thompson Oct 03 '13 at 01:36
  • @AndrewThompson thank you this is very helpful information – ola Oct 03 '13 at 01:38

1 Answers1

1

Get the index of tab

int index = tabPane.indexOfTab("One");

Set the component at the specified index

tabPane.setComponentAt(index, new Dashboard());
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366