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...
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...
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:
stateChanged
event occurs before (or shortly after) the mouse button is released, show your message dialog.