1

I've been playing about with mouse listeners etc on my tabbedpane but cant seem to get anything going. Trying to make a little menu appears when you right-click a tab which will give you the option to close that tab. Could someone point me in the right direction please

tabbedPane.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) 
        {
            if(SwingUtilities.isRightMouseButton(e))
            {
                System.out.print(tabbedPane.getSelectedIndex());
            }
        }
    });
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
horHAY
  • 788
  • 2
  • 10
  • 23
  • Can you provide an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the problem? – Kevin Workman Apr 22 '14 at 14:40
  • Well as you can see in the code, im just trying to return the value in which the right click occurs. just testing that its actually registering the click etc but its not. – horHAY Apr 22 '14 at 14:43
  • Well it's up to you how to proceed. The best way to get help is to provide an MCVE that we can copy and paste into our own IDE to test ourselves. – Kevin Workman Apr 22 '14 at 14:46
  • Duplicate of http://stackoverflow.com/questions/9220889/java-right-clicking-jtabbledpane-tab – Mubin Apr 22 '14 at 14:55

1 Answers1

2
tabbedPane.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) 
    {
        if(SwingUtilities.isRightMouseButton(e))
        {
            JPopupMenu menu = new JPopupMenu();
            JMenuItem closer = new JMenuItem(new AbstractAction("Close") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    tabbedPane.removeTabAt(tabbedPane.getSelectedIndex());
                }
            });
            menu.add(closer);
            menu.show(tabbedPane, e.getX(), e.getY());
        }
    }
});

It might be better to install the menu on the tab component which can be accessed via tabbedPane.getTabComponentAt. The tab component is the component that renders the text tag for the tab. If you wanted to add an X button to the tab, that is where you put it.

Not Saying
  • 194
  • 11