简体   繁体   中英

Display the first tab of a JTabbedPane

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.

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM