JTabbedPane


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: JTabbedPane

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    Unhappy JTabbedPane

    How do I attach a close button to my tabbed pane so that I can close whenever I choose to.

  2. #2
    Join Date
    Mar 2006
    Posts
    4
    Help

  3. #3
    Join Date
    Mar 2004
    Posts
    635
    How about giving ppl more than 1 minute to respond before you repost.

    Took about 20seconds to find on yahoo.
    tabbed panes with close icon

  4. #4
    Join Date
    Mar 2006
    Location
    England
    Posts
    10

    Post

    Try extending class JTabbedPane with say, MyTabbedPane. Then, add a MouseListener to watch for right-clicks on the tab. On right-click, show popup menu that allows user to close the tab. Code below does something like this.

    Code:
    public class MyTabbedPane
      extends JTabbedPane
    {
        public MyTabbedPane()
        {
            super(JTabbedPane.TOP);
    
            addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    tabClicked(e);
                }
            });
        }
    
        private void tabClicked(MouseEvent e)
        {
          if (e.getButton() != MouseEvent.BUTTON1 && e.getClickCount() == 1) {   // if is right-click
          
            // create popup with Close menuitem
            JPopupMenu popupMenu = new JPopupMenu();
            JMenuItem closeBtn = new JMenuItem("Close");
            closeBtn.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      closeSelectedTab();
                  }
                });
              }
            });
            popupMenu.add(closeBtn);
            
            // display popup near location of mouse click
            popupMenu.show(e.getComponent(), e.getX(), e.getY() - 10);
          }
        }
        
        private void closeSelectedTab()
        {
           // do cleanup
           remove( getSelectedIndex() );  // remove selected tab
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2005, 04:35 AM
  2. associate a JComboBox to JTabbedPane component
    By ozmanbozman in forum Java
    Replies: 4
    Last Post: 03-27-2005, 12:55 PM
  3. Replies: 1
    Last Post: 11-05-2001, 02:16 PM
  4. JTabbedPane problems
    By Franck in forum Java
    Replies: 1
    Last Post: 06-26-2001, 01:16 PM
  5. JTabbedPane equivalent
    By David in forum Java
    Replies: 3
    Last Post: 06-02-2000, 11:10 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center