22

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

Here is the code for JTabbedPane:

public class TabbedPane extends JFrame {

    public TabbedPane() {


        setTitle("Tabbed Pane");  
        setSize(300,300); 

        JTabbedPane jtp = new JTabbedPane();

       getContentPane().add(jtp);

       JPanel1 jp1 = new JPanel1();//This will create the first tab

       JPanel jp2 = new JPanel2();//This will create the second tab

       //add panel .........

    //example usage
     public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }

}

here is class JPane1:

...    JLabel label1 = new JLabel();
       label1.setText("This is Tab 1");
       jp1.add(label1);

and class Jpane2 with button on int

JButton test = new JButton("Press"); jp2.add(test);

ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
setVisible(true); 

} so problem is here in ActionListener of button on Jpanel2

class ButtonHandler implements ActionListener{
       public void actionPerformed(ActionEvent e){
              // what i do now ? to call  jpanel 1 show ![alt text][1]
       }
}

alt text

Kym NT
  • 670
  • 9
  • 28
tiendv
  • 2,307
  • 7
  • 23
  • 34

6 Answers6

47

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.

Ventral
  • 1,194
  • 9
  • 5
9

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

Guillaume
  • 5,535
  • 1
  • 24
  • 30
3

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!!

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
sidd
  • 31
  • 1
0

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

JohnnyQ
  • 484
  • 10
  • 23
0

Just! With:

JTabbedPane.setSelectedComponnet(component);
Bugs
  • 4,491
  • 9
  • 32
  • 41
  • 1
    **From review queue:** May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. See also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers). – help-info.de Jun 08 '17 at 18:00
0

here is fixed and full code. after 12 years.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import javax.swing.JPanel;

public class TabbedPane extends JFrame {
    public TabbedPane() {
        JTabbedPane jtp = new JTabbedPane(); 
        JPanel jp1 = new JPanel();//This will create the first tab
        JPanel jp2 = new JPanel();//This will create the second tab
        setTitle("Tabbed Pane");  
        setSize(400,300); 
        setLocationRelativeTo(null);
        
        JLabel label1 = new JLabel();
        label1.setText("This is Tab 1"); 
        jp1.add(label1);
        //jp2.add(new JLabel(new ImageIcon("C:/users/user/downloads/jav/s1/pic1.jpg")));        
        getContentPane().add(jtp);
        
        JButton nextButton = new JButton("Next"); jp1.add(nextButton);
        JButton prevButton = new JButton("Previous"); jp2.add(prevButton);
        //prev
        class ButtonHandlerPrev implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i-1);
            }
        }
        class ButtonHandlerNext implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i+1);
            }
        }
        jtp.addTab("first", jp1);
        jtp.addTab("second", jp2);
        ButtonHandlerPrev prevHandler = new ButtonHandlerPrev();
        ButtonHandlerNext nextHandler = new ButtonHandlerNext();
        prevButton.addActionListener(prevHandler);
        nextButton.addActionListener(nextHandler);
        setVisible(true);
    }
        //example usage
    public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }
}

just run void main(String[] args)