The component of the tab that consumes the mouse, so the tabs will not change

I have a problem: when I add a mouse listener to the component that is used as a tab, I cannot switch tabs.

This demonstrates the problem:

import javax.swing.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; public class JTabBug { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JTabbedPane jTabbedPane = new JTabbedPane(); jTabbedPane.addTab("Red", new JLabel("Roses")); jTabbedPane.addTab("Blue", new JLabel("Skies")); jTabbedPane.addTab("Green", new JLabel("Grass")); for (int i = 0; i < jTabbedPane.getTabCount(); i++) { JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i)); tabComponent.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { System.out.println("dragging"); } }); jTabbedPane.setTabComponentAt(i, tabComponent); } JFrame jFrame = new JFrame("Testing"); jFrame.add(jTabbedPane); jFrame.setSize(400, 500); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } 

the drag and drop prints as expected, but you cannot change the tabs.

+4
source share
5 answers

This seems to work: Note that I get the JLabel that was added, and not creating a new one that needs to be added again.

 import javax.swing.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; public class JTabBug { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JTabbedPane jTabbedPane = new JTabbedPane(); jTabbedPane.addTab("Red", new JLabel("Roses")); jTabbedPane.addTab("Blue", new JLabel("Skies")); jTabbedPane.addTab("Green", new JLabel("Grass")); for (int i = 0; i < jTabbedPane.getTabCount(); i++) { JLabel tabComponent = (JLabel)jTabbedPane.getComponent(i); tabComponent.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { System.out.println("dragging"); } }); } JFrame jFrame = new JFrame("Testing"); jFrame.add(jTabbedPane); jFrame.setSize(400, 500); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } 
0
source

I do not think this is a mistake, because it does what I expect. You create a new component for the tab (JLabel), add a motion listener to it, and then set it as a tab. You are not adding a mouse listener to the shortcut, which will lead to tab changes, so I did not expect it to be there. The source component of the tab handles this mouse click event, so if you can access this component, try copying it if you can (or just access this component and add a mouse motion adapter). If this is not possible, simply handle the click event yourself.

0
source

Mouse events don't seem to change the tab selection if the new tab component has a different listener. Not sure why this is happening because the new shortcut tab component works without a mouse movement listener. If you add another mouse listener to change the selection:

  final int index = i; tabComponent.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { jTabbedPane.setSelectedIndex(index); } }); 

You will get the desired result, but it looks like it would be a weird way.

0
source

Cancel the contains method of your tab component (JLabel in your case) to return false.

  public boolean contains(int x, int y) { return false; } 
0
source

My solution is a little more detailed than jzd's, I did not know that this can be done so cleanly. I like your decision, it seemed to me something new. Thanks jzd for this.

 public class JTabBug { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JTabbedPane jTabbedPane = new JTabbedPane(); jTabbedPane.addTab("Red", new JLabel("Roses")); jTabbedPane.addTab("Blue", new JLabel("Skies")); jTabbedPane.addTab("Green", new JLabel("Grass")); for(int i = 0; i < jTabbedPane.getTabCount(); i++) { final JLabel tabComponent = new JLabel(jTabbedPane.getTitleAt(i)); tabComponent.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { System.out.println("tabComponent dragging"); } }); jTabbedPane.setTabPlacement(JTabbedPane.LEFT); tabComponent.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { int x = tabComponent.getLocationOnScreen().x - jTabbedPane.getLocationOnScreen().x; int y = tabComponent.getLocationOnScreen().y - jTabbedPane.getLocationOnScreen().y; MouseEvent me = new MouseEvent( (JLabel)e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), x, y, e.getLocationOnScreen(). x, e.getLocationOnScreen().y, e.getClickCount(), e.isPopupTrigger(), e.getButton()); jTabbedPane.getMouseListeners()[0].mousePressed(me); System.out.println("tabComponent mousePressed e="+e); } }); jTabbedPane.setTabComponentAt(i, tabComponent); } JFrame jFrame = new JFrame("Testing"); jFrame.add(jTabbedPane); jFrame.setSize(400, 500); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } 

Enjoy boro

0
source

All Articles