1

I have a JTabbedPane with 5 tabs in it. I also got 3 other buttons in same JFrame where my JTabbedPane is added. I want to make the user able move to particular tab when a particular button is clicked. Here is the image example

enter image description here

Now for example if user clicks Button 1 then tab One should be opened and similarly when button 2 is clicked then tab Two should be opened and so for third one.

Here is my code to add these JTabbedPane and buttons.

public class TabsAndButtons
{
    public TabsAndButtons()
    {
       JTabbedPane tabsPane = new JTabbedPane();
       tabsPane.add("One", new JPanel());
       tabsPane.add("Two", new JPanel());
       tabsPane.add("Three", new JPanel());
       tabsPane.add("Four", new JPanel());
       tabsPane.add("Five", new JPanel());

       JPanel Panel = new JPanel();
       Panel.add(tabsPane);

       JButton Button1 = new JButton("Button 1");
       Panel.add(Button1);
       JButton Button2 = new JButton("Button 2");
       Panel.add(Button2);
       JButton Button3 = new JButton("Button 3");
       Panel.add(Button3);

       JFrame MainFrame = new JFrame("JTabbedPane and Buttons");
       MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       MainFrame.getContentPane().add(Panel );
       MainFrame.pack();
       MainFrame.setVisible(true);
    }

    public static void main(String[] args) 
    {
       java.awt.EventQueue.invokeLater(() -> {
           new TabsAndButtons();
       });
    }
}

The actual purpose of such action is very lengthy and has a lot details which will make the question dull so I am asking the main task where I stucked. Thanks for your kind support and time.

  • possible duplicate of [Bringing tab to front in JTabbedPane](http://stackoverflow.com/questions/1235644/bringing-tab-to-front-in-jtabbedpane) – MarsAtomic Oct 04 '14 at 21:55
  • No that's totally different thing –  Oct 04 '14 at 21:57
  • Yeah... you were meant to use the question I referenced as the core of an action listener handler, but I suppose I should have spelled it out. I think you probably realize that it's not "totally different." – MarsAtomic Oct 04 '14 at 23:55

3 Answers3

2

Use the method button.addActionListener() to execute code when a button is clicked. The code that you want to execute is probablytabsPane. setSelectedIndex(i)wherei` is the index of the tab that you want to show.

You might also want to move the JTabbedPane tabsPane into a member variable, or mark it with final, to make sure that it can be accessed from within the action listener.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • Thanks. I got it. I just needed an idea so you gave me. Thanks again. –  Oct 04 '14 at 22:10
2

Add an ActionListener to each of the buttons. Then in the ActionListener you can invoke the setSelected(...) method of the tabbed pane.

Read the section from the Swing tutorial on How to Write an ActionListener for more information and examples.

Also, variable name should NOT start with an upper case character.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Try in the EventListener for the Buttons the Method setSelectedIndex(int index) on your JTabbedPane.
The Referenz: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setSelectedIndex%28int%29