change the selected tab in JTabbedPane "automatically"

jtabbedpane get selected tab name
java swing jtabbedpane change tab event
jtabbedpane add panel
jtabbedpane layout
jtabbedpane close button
java jtabbedpane add tab
jtabbedpane setselectedindex
how to use tabs in java

I'm using Java swing for the first time, and I have a problem which I can't solved.

I have a JTabbedPane with 3 tabs and I need to change to next tab "automatically" in the end of a computing, which starts with a click in a JButton (within one of the tabs). I've tried to use setSelectedIndex() but doesn't work. After the JButton's ActionEvent the selected tab (printed in eclipse console) has changed, but in GUI doesn't had effect.

Yes, I've tried the validate(), revalidate() methods, even the repaint() method, but didn't work

That's an example of my code

public class Tab1 extends JPanel {
    //when click on this button the computing starts
    JButton btn = new JButton("Compute...");
    btn.addActionListener(new BtnListener());

An example of a JPanel which I'll add to my JTabbedPane

public class Window() {

    private JFrame frame;

    public Window() {

    private void init() {
        frame = new JFrame();
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        JPanel tab1 = new Tab1();
        JPanel tab2 = new Tab2();
        JPanel tab3 = new Tab3();

        tabbedPane.addTab("tab 1", null, tab1, "tab1");
        tabbedPane.addTab("tab2", null, tab2, "tab2");
        tabbedPane.addTab("tab3", null, tab3, "tab3");

        //add() all components

Example of my Window

public class BtnListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        //make the computing
        //then I need to change to next tab (tab2)

And my Listener

I hope you understand my problem. Please help me, I really don't know what I do to make it works and I need to do this.

When you switch tabs you needn't to use revalidate()/repaint() just setSelectedIndex().

Here is an example of switching to next tab:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class TestFrame extends JFrame {

    public static void main(String... s) {
        new TestFrame();

    private JTabbedPane tabs;

    public TestFrame() {

    private void init() {
        tabs = new JTabbedPane();

        tabs.addTab("1", new Tab(1));
        tabs.addTab("2", new Tab(2));
        tabs.addTab("3", new Tab(3));

    private class Tab extends JPanel {

        public Tab(int i) {
            JButton next = new JButton("next");
            next.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    int selectedIndex = tabs.getSelectedIndex();
                    int nextIndex = selectedIndex == tabs.getTabCount()-1 ? 0 : selectedIndex+1;
            add(new JLabel("tab " + i));


change the selected tab in JTabbedPane "automatically", When you switch tabs you needn't to use revalidate()/repaint() just setSelectedIndex() . Here is an example of switching to next tab: Determining When the Selected Tab Changes in a JTabbedPane Container: 14.51.28. Moving a Tab in a JTabbedPane Container: 14.51.29. Insert a tab after the first tab: 14.51.30. Get the index of the tab by matching the child component: 14.51.31. Getting and Setting the Selected Tab in a JTabbedPane Container: 14.51.32.

Thanks for answers.

I checked my code and I've found an instance problem. I was changing another instance of my Window instead of what was visible. So, when I fixed that, the updates worked.

Thanks again.

Listening for Selected Tab Changes : JTabbedPane « Swing « Java , Listening for Selected Tab Changes import java.awt.BorderLayout; import javax.​swing.JButton; import javax.swing.JFrame; import javax.swing.JTabbedPane  This method is called each time there is a change to either the selected index or the selected tab in the JTabbedPane. Usually, the selected index and selected tab change together. However, there are some cases, such as tab addition, where the selected index changes and the same tab remains selected. There are other cases, such as deleting the selected tab, where the index remains the same, but a new tab moves to that index.

A change listener is similar to a property change listener. A change listener is registered on an object — typically a component, but it could be another object, like a model — and the listener is notified when the object has changed. The big difference from a property change listener is that a change listener is not notified of what has changed, but simply that the source object has changed. Therefore, a change listener is most useful when it is only necessary to know when an object has changed in any way.

Similarly, you need to register a change listener on a button(or toggle button with states) to be informed when the calculation had stopped and the button's value changes.

//use some flag to determine when calculation process is actually finished
volatile boolean isCalculationEnds = true;  

button.addChangeListener((ChangeEvent e) -> {
 if (tabbedPane.getSelectedComponent() instanceof CustomPanel && isCalculationEnds) {
     //do something 

 if (tabbedPane.getSelectedIndex()==1 && isCalculationEnds){
   //do something

How do I detect tab selection changes in JTabbedPane?, In this example you will learn how to detect tab selection changes in a JTabbedPane component. To get notified when a tab is selected you  Listening for Selected Tab Changes : JTabbedPane « Swing « Java Tutorial. Getting and Setting the Selected Tab in a JTabbedPane Container: 14.51.32.

How do I set the colors of JTabbedPane tabs?, The JTabbedPane 's methods that you can use the change foreground and background color are: setForeground(Color color) for changing the  To get notified when a tab is selected you must add a ChangeListener to the JTabbedPane component using the addChangeListener (). This method take an instance of class that implements the ChangeListener interface as it arguments. In this example we implement the ChangeListener interface in the TabbedPaneSelection class.

changing text color on selected JTabbedPane tab (Swing / AWT , I have a JTabbedPane with black tabs and white lettering on the tabs. I am using UIManager.put("TabbedPane.selected", to change a tab color to  To enable or disable tabs in JTabbedPane you can use the JTabbedPane ‘s setEnableAt (int index, boolean enable) method. The index is zero based, this means that the first tab is at index number 0. The enable parameter when set to true will enable the tab while setting to false will disable the tab.

JTabbedPane (Java SE 13 & JDK 13 ), public class JTabbedPane extends JComponent implements Serializable, Accessible, Usually, the selected index and selected tab change together. However  Returns the index of the tab for the specified tab component. void. insertTab ( String title, Icon icon, Component component, String tip, int index) Inserts a new tab for the given component, at the given index, represented by the given title and/or icon, either of which may be null. boolean.

  • Read about multithreadin in swing here:
  • Post MCVE for help.
  • I think, author make huge computing in EDT. This is his/her mistake.
  • @SergiyMedvynskyy may be you are right. But as written in BtnListener listener computes and then switch, this must work with some delay.