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

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.