简体   繁体   中英

Multiple tabs in JTabbedPane using a single JPanel?

Is it possible to use a single JPanel for multiple tab items in JTabbedPane ?

EG:

JTabbedPanel tabs=new JTabbePanel();

JPanel panel=new JPanel();
JButton but=new JButton("TEXT");
but.addActionlistener(this);
panel.add(but)

tabs.add("First",panel);
tabs.add("Second",panel);

An ActionListener is added to the JTabbedPane to notify the program of tab changes (change cur_tab to tab number)

public void actionPerformed(..)
{ System.out.println("Now in "+cur_tab); }

The same component cannot be used for several tabs

taken from here

Sounds like you need a subclass of JPanel . Just create an abstract class that handles the complex layout and have the subclasses create the necessary GUI elements that are required.

If you are just inserting a single text box, button, etc, you may not even need subclasses per tab. Just create multiple instances of the base class and add the component you need.

New panels and other GUI items are relatively cheap. Performance issues in a Swing GUI are more likely to come from event handling or firing too many events rather than how complex or how many components it has. Make things easier to maintain and understand, then worry about performance.

I had a panel with memory-intensive components on it which I only wanted to create one instance of, but change the behaviour of it using the attractive JTabbedPane.

I did it by creating empty panels for each tab, and a third panel that contains my (single) complicated components. On the StateChange event for the JTabbedPane, I remove the third panel from whichever of the first two it was in, and add it to whichever one is newly selected.

Bit hacky, but it works fine.

if you'll create class that returns JPanel then yes that's possible

but Notice:

there are long time Bug that two Tabs can't contains same component schemas, with schemas I means for example one Tab contains JPanel + JTextField + JButton , but then second JPanel must contains another Numbers or Type of JComponents

unfortunatelly (nothing special) BugsDatabase isn't accesible in this moment

You are wrong. Just set a panel on the first tab in function initComponents() like that:

 p.add("1", MainPanel);

Then use:

p.add("2", p.getTabComponentAt(0));

Using this metode you will have the same component on 2 tabs.

You can use StateChanged Event to change actions in this tabs. For example:

JTabbedPane p = (JTabbedPane)Tabbar;
int idx = p.getSelectedIndex();
   if(idx==0){
     Do something...
    }
   if(idx==1){
     Do something different...
    }

The following will allow you to add the same component with different titles to a JTabbedPane:

JTabbedPane tabbedPane = new JTabbedPane()
    {
      boolean adding = false;
      @Override
      public void removeTabAt(int index)
      {
        if(!adding)
        {
          super.removeTabAt(index);
        }
      }

      @Override
      public void insertTab(String title, Icon icon, Component component, String tip, int index)
      {
        adding = true;
        super.insertTab(title, icon, component, tip, index);
        adding = false;
      }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM