문제

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);
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top