Question

I have a Jtabbedpane which contains 4 tabs (each one a Jpanel).

When I run the application from netbeans 6.8 the selected tab will be the same one as it was selected before closing the application.

Is there a way to select the first tab (index 0) each time I run my application? Here is the Code:

mainTabbedPanel = new javax.swing.JTabbedPane();
mainTabbedPanel.setName("mainTabbedPanel");
mainTabbedPanel.addTab(resourceMap.getString("panel1"), panel1);
mainTabbedPanel.addTab(resourceMap.getString("panel2"), panel2);
mainTabbedPanel.addTab(resourceMap.getString("panel3"), panel3);
mainTabbedPanel.addTab(resourceMap.getString("panel4"), panel4);

Those are in the method private void initComponents() of netbeans and on startup (constructor of my application) initComponents will be called. I try to put mainTabbedPanel.setSelectedIndex(0) after calling initComponents() but didn't work.

Was it helpful?

Solution

By default the first tab will be selected. If something other than this is happening then there must be code somewhere that is using the setSelectedIndex(...) to reset the tab. You need to search the generated code to find out where this is and remove the code.

If you don't know how to remove the code then you can try to reset the index after the default code is executing. This is done by using SwingUtilities.invokeLater after the GUI is visible. Your code would be something like:

SwingUtilities.invokeLater(new Runnable()
{
    public void run()
    {
        tabbedPane.setSelectedIndex(0);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top