import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
public class Main {
public static void main(String[] args) {
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");
}
});
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();
jFrame.add(jTabbedPane);
jFrame.setSize(400, 500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}