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
source share