Dimitrios K.
Dimitrios K.

Reputation: 1028

jTabbedPane opens different tab on loading

First of all I'm using netbeans as my IDE and I don't know if this is causing it. When I run my program (even if I have build it and run the .jar) I think it selects the tab that was previously selected (before quiting). So if for example I close the app with the third tab selected, it starts up with that selected again. Is there a known solution for this? The selectedIndex property on the jTabbedPane is set to 0. Shouldn't this property be the default onLoad value?

Thx in advance, Jimmy

PS. BTW for some reason it didn't submit my question in Opera (?)

Upvotes: 0

Views: 4218

Answers (6)

Jeff Gordon
Jeff Gordon

Reputation: 1

Same problem. Had to go back to NetBeans 7.0.1 to update a JSR 296 application and Java 7 runs it differently than previous versions did so the last tab created was always the one that had focus. Couldn't get anything to change that in the constructor, but finally found just wrapping the same call (setSelectedIndex()) in a call to invokeLater() solves it.

    SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          tabMain.setSelectedIndex(0);
        }
      }
    );

Upvotes: 0

Fab
Fab

Reputation: 1

I had the same problem and found an easy workaround. In netbean's GUI-builder I set my tabbedpane to not enabled. Later in my program I checked if it was not enabled and in that case called MyTabbedPane.setEnabled(true); and MyTabbedPane.setSelectedIndex(0);

Upvotes: 0

mjoraid
mjoraid

Reputation: 41

tabbedPaneName.setSelectedIndex(0);

just put that line in the place where the tabbed pane would be loaded if a button actuion will load the tabbed pane then put the line there but change tabbedPaneName to YOUR tabbed pane name.

Upvotes: 4

user352848
user352848

Reputation:

Same problem here with Netbeans 6.8 and JTabbedPane. Neither setSelectedIndex() nor setSelectedComponent() makes a difference. The getSelectedIndex() returns the value previously set, but the pane is not selected correctly.

The reason for this is that the SingleFrameApplication saves it's state and restores the saved state on the next restart. This is done in the code generated by the GUI builder. You could see that startup() and configureWindow() methods of the SingleFrameApplication are overridden.

Workarounds:

  1. You could override the shutdown() method as well, then modifications to the configuration will not be saved. Note that the original will still be restored, so ensure that the required configuration is saved.

  2. Modifying the startup() method also helps:

MyView myView = new MyView(this);
myView.getFrame().setVisible(true);
myView.getFrame().pack();

Upvotes: 0

Joe Carnahan
Joe Carnahan

Reputation: 2391

Besides using JTabbedPane.setSelectedIndex(), it's also possible to select a tab by calling JTabbedPane.setSelectedComponent(). Have you searched the code for setSelectedComponent() as well?

Upvotes: 0

camickr
camickr

Reputation: 324088

The only way it can be set to an index other than zero is if the Java code contains:

tabbedPane.setSelectedIndex(...);

So search the source code for that line and fix it.

Upvotes: 0

Related Questions