How to switch tabs in jTabbedPane by clicking a Button?

前端 未结 5 555
有刺的猬
有刺的猬 2021-02-05 05:46

I have two JTabbedPanes, JTabbedPane1 & 2 How can I press button in JTabbedPane2 to show JTabbedPane1 ?

Here is the code for JTabbedPane:

public cla         


        
相关标签:
5条回答
  • 2021-02-05 06:22

    Just! With:

    JTabbedPane.setSelectedComponnet(component);
    
    0 讨论(0)
  • 2021-02-05 06:24

    If you make the tabbed pane accessible to ButtonHandler you can do this:

    class ButtonHandler implements ActionListener{
           public void actionPerformed(ActionEvent e){
                  jtp.setSelectedIndex(0);
           }
    }
    

    You can do this by making jtp (ideally with a better name) a private attribute with a getter method or it can be passed in as a constructor argument to ButtonHandler.

    0 讨论(0)
  • 2021-02-05 06:24

    Just like to add that your action listener has to be in the same class as your tabs.

    0 讨论(0)
  • 2021-02-05 06:30

    its very simple: use the code below:

    JTabbedpane.setSelectedIndex(); 
    

    what ever the name is of you J Panel replace it with the above JTabbedpane and for example you want to select the first tabs simply put 0 in bracket and if you want to select second tab then put 1 in bracket eg: my J tabbed pane is called jtabbedpanel and I want the first tab then the line will look as:

    jtabbedpanel.setSelectedIndex(0);
    

    hope this helps!!

    0 讨论(0)
  • 2021-02-05 06:47

    You should use the method JTabbedPane.setSelectedIndex(int index) with the index of the tab you want.

    0 讨论(0)
提交回复
热议问题