Results 1 to 3 of 3
  1. #1
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default Removing all but current tab from tabbedpane

    In order to remove all other tabs except the currently selected tab in a JTabbedPane,
    I wrote this code:

    if(actionCommand.equals("Close Other Tabs"))
    {
    int selectedIndex = tabbedPane.getSelectedIndex();

    for(int i = 0; i < tabbedPane.getTabCount(); i++)
    {
    if(selectedIndex != i) // if tab is not 'selected', then remove it
    {
    tabbedPane.removeTabAt(i); }
    }
    }

    The problem is that though some tabs duly get removed, other unselected tabs fail to get removed and remain along with the currently selected tab. Thanks.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose tab #2 isn't selected so you remove it. The original tab #3 becomes tab #2 but you increment your loop counter so the new tab #2 isn't tested and stays in the JTabbedPane forever. The remedy is not to increment your loop counter if you have removed tab #i. Find the selected tab index again after removal of a tab.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Iterate in reverse and remove all except the selected tab.
    Java Code:
    for(int i = tabbedPane.getTabCount() - 1; i >= 0; i--) {
       if(selectedIndex != i) {
          tabbedPane.removeTabAt(i); }
       }
    }
    db

Similar Threads

  1. Working with Tabbedpane
    By blu_fox88 in forum AWT / Swing
    Replies: 5
    Last Post: 03-10-2010, 09:50 AM
  2. How can I add popUp menu on a tab of a tabbedPane?
    By shahid0627 in forum AWT / Swing
    Replies: 1
    Last Post: 08-31-2009, 04:19 AM
  3. Another Color TabbedPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:37 PM
  4. Color TabbedPane Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:36 PM
  5. Help with tabbedpane
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •