2

I would like to make a mouse-event that allows me to right-click on a tab, to select and delete it. How do I enable the JOptionPane() after the tab name is right clicked?

I haven't been able to try myself as I have no idea...

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • When you say " haven't been able to try myself" ... why is that? What IDE are you using? – Caffeinated Mar 17 '12 at 20:08
  • 1
    well i have no idea where to start as I have never worked with mousevents but am busy researching now any help would be appreciated I am using netbeans – donthedestroyer Mar 17 '12 at 20:10

1 Answers1

0

I suppose you are talking about a JTabbedPane here. I don't know how to detect a right click on a tab but at least I can show you how to get notified about mouse clicks and tab changes.

To be notified if the used right clicks somewhere in the JTabbedPane you can use:

tabbedPane.addMouseListener(new MouseAdapter()
{
    @Override
    public void mouseClicked(MouseEvent e)
    {
        if (e.getButton() == MouseEvent.BUTTON3)
            JOptionPane.showMessageDialog(
                null, "Clicked with right mouse button somewhere on the tabbed pane");
    }
});

You can also get notified as soon as the user presses or releases the mouse button. See the API documentation for MouseListener.

To be notified on a tab change you can use:

tabbedPane.addChangeListener(new ChangeListener()
{
    @Override
    public void stateChanged(ChangeEvent e)
    {
        JOptionPane.showMessageDialog(null, "Tab changed");
    }
});

Maybe you can combine this somehow. An idea (although not very beautiful) would be:

  1. Register that the used presses the right mouse button
  2. If a stateChanged event occurs before (or shortly after) the mouse button is released, show your message dialog.
siegi
  • 5,646
  • 2
  • 30
  • 42