Hi! I have added an action listener for a button. When i click this button it should go to the second tab in the tabbed pane which i have created. I have created two tabs as follows:

tab.addTab("Change Password", panel1);
tab.addTab("Add new User", panel2);

and the action listener for the button as follows:

class newuserListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
optionsGUI tx = new optionsGUI();
}
}

My problem is that when i click the button it goes to the first tab. How can i make it to go to the second tab when i click the button???

Dani AI

Generated

Nice progress , and good pointer from /. The key issues here are:

  • You were constructing a brand-new options window on each click, which always starts on the first tab.
  • You were reaching directly into the tabbed pane from outside the class.

A cleaner, less brittle approach is to keep one OptionsGUI instance and expose an intent-style method for selecting the desired tab. Also prefer selecting by component (or a semantic method) instead of a hard-coded index so your code survives tab reordering.

Example:

// OptionsGUI.java
public final class OptionsGUI extends JFrame {
    private final JTabbedPane tabs = new JTabbedPane();
    private final JPanel changePassword = new JPanel();
    private final JPanel addUser = new JPanel();

    public OptionsGUI() {
        tabs.add("Change Password", changePassword);
        tabs.add("Add new User", addUser);
        setContentPane(tabs);
        pack();
    }

    public void openAddUserTab() {
        tabs.setSelectedComponent(addUser); // more robust than using 1
        if (!isVisible()) setVisible(true);
        toFront();
    }
}
// mainGUI snippet
public final class MainGUI extends JFrame {
    private final OptionsGUI options = new OptionsGUI();

    private void wire() {
        newUserButton.addActionListener(e -> {
            SwingUtilities.invokeLater(options::openAddUserTab);
        });
    }
}

Why this helps:

  • No duplicate windows; state in OptionsGUI is preserved between opens.
  • UI changes happen on the EDT.
  • The tabbed pane stays encapsulated; callers do not poke at fields like tab.
  • If you ever reorder or rename tabs, openAddUserTab() still targets the right one.

If you really must pass the selected tab from MainGUI, you can add options.openTabByName("Add new User"); or an enum. But avoid making the JTabbedPane public or using static accessors.

Recommended Answers

All 7 Replies

in the JTabbedPane class there is a method setSelectedIndex(int index). where 0 is the first tab, 1 is the second, ect. (I believe that is how it works, you can also select the tab with the method, setSelectedComponent(Component c). where c is the component that the tab represents. Hope this helps =)

in the JTabbedPane class there is a method setSelectedIndex(int index). where 0 is the first tab, 1 is the second, ect. (I believe that is how it works, you can also select the tab with the method, setSelectedComponent(Component c). where c is the component that the tab represents. Hope this helps =)

Thank you for the reply, but the problem is that I need to open this tab from another form. It's like this:
I have two forms namely "mainGUI" and "optionsGUI".
There is a button in the "mainGUI" which is coded to go to the "optionsGUI".
the tabbed pane is in the "optionsGUI".
Therefore when i click the button in the "mainGUI", it should go to the "optionsGUI" as well as to the second tab.

I have coded it to go to the "optionsGUI" form, but after that point I dont know how to view the second tab as soon as I click the button in the "mainGUI".

Thank you again for replying.

the problem is that I need to open this tab from another form.

I don't know why that would be a problem. If you are able to see the JTabbedPane, and it sounds like you can, can't you do this in your ActionListener?

tx.setSelectedIndex(1);

Maybe I'm not visualizing things correctly. I think that's what llemes was suggesting.

I don't know why that would be a problem. If you are able to see the JTabbedPane, and it sounds like you can, can't you do this in your ActionListener?

tx.setSelectedIndex(1);

Maybe I'm not visualizing things correctly. I think that's what llemes was suggesting.

I tried it, but its not working and sorry because I'm new to JAVA, so this is I what I could do up to now. I referred many sites and also a book that I have, but coudn't find a solution to this. Given below is the code that I used. Please help. Thank you.

class newuserListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
optionsGUI tx = new optionsGUI();
tx.setSelectedIndex(1);
}
}

Oh, Ok, i think I see.
you need to do something like this:

class newuserListener implements ActionListener{
    public void actionPerformed(ActionEvent ev)
    {
        optionsGUI tx = new optionsGUI();
        tx.JTabbedPane.setSelectedIndex(1);
    }
}

replace JTabbedPane with variable name of method below:

you need to point to the GUIs TabbedPane. if it's private, set it to public or have method:
public static void getTabbedPane(){return "JTabbedPane_variable_name";}


i THINK that's it ;)

Oh, Ok, i think I see.
you need to do something like this:

class newuserListener implements ActionListener{
    public void actionPerformed(ActionEvent ev)
    {
        optionsGUI tx = new optionsGUI();
        tx.JTabbedPane.setSelectedIndex(1);
    }
}

replace JTabbedPane with variable name of method below:

you need to point to the GUIs TabbedPane. if it's private, set it to public or have method:
public static void getTabbedPane(){return "JTabbedPane_variable_name";}


i THINK that's it ;)

Thank you for the reply dude. It worked, but I just modified your code a bit. Given below is what I did. Thank you again for helping me.

class newuserListener implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
optionsGUI tx = new optionsGUI();
tx.tab.setSelectedIndex(1); //"tab" is the variable name for the JTabbedPane that I have created.
}
}

No problem, don't forget to set the thread to solved =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.