I have a JTabbedPane with a custom tab component. I want to be able to right click anywhere on the tab and show a JPopupMenu. The problem I'm having is that there is dead space on each tab where the JPopupMenu does not appear with a right click. I believe it is because I'm attaching the listener to a JPanel that is acting as the Tab Component, but the JPanel isn't "filling" the whole tab.
Is there a way to attach a mouse listener to the whole tab?
Here is an example to illustrate what I'm seeing. In the yellow area of the tab, I can right click and get a popup menu, but in the gray area of the tab the right click is not intercepted.
public class TabExample {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 1024, 768);
JTabbedPane pane = new JTabbedPane();
for (int i = 0; i < 15; i++) {
JPanel panel = new JPanel();
JLabel label = new JLabel("Panel " + i);
panel.add(label);
pane.addTab("", panel);
final JPanel tabComponentPanel = new JPanel(new BorderLayout());
final JLabel tabComponentLabel = new JLabel("My Tab " + i);
final JLabel tabComponentImageLabel = new JLabel();
ImageIcon icon = new ImageIcon(getImage());
tabComponentImageLabel.setHorizontalAlignment(JLabel.CENTER);
tabComponentImageLabel.setIcon(icon);
tabComponentPanel.add(tabComponentImageLabel,BorderLayout.CENTER);
tabComponentPanel.add(tabComponentLabel,BorderLayout.SOUTH);
tabComponentPanel.setBackground(Color.YELLOW);
tabComponentPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
pane.setSelectedComponent(panel);
} else if (e.getButton() == MouseEvent.BUTTON3) {
JPopupMenu jPopupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Menu Item");
jPopupMenu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked ");
}
});
jPopupMenu.show(tabComponentPanel, e.getX(),
e.getY());
}
}
});
pane.setTabComponentAt(pane.indexOfComponent(panel),
tabComponentPanel);
}
frame.add(pane);
frame.setVisible(true);
}
});
}
private static BufferedImage getImage() {
BufferedImage bimage = new BufferedImage(16, 16,
BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g2d = bimage.createGraphics();
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 16, 16));
g2d.dispose();
return bimage;
}
}
Is there a way to attach a mouse listener to the whole tab?
You could add the MouseListener to the JTabbedPane.
Then on a MouseEvent you can use the getUI()
method to get the BasicTabbedPaneUI
class. This class has a getTabBounds(...)
method.
So you can iterate through all the tabs to see if the bounds of any tab matches the mouse point.
Edit:
BasicTabbedPaneUI
has a tabForCoordinate(...)
method, which removes the need for the iteration logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With