Skip to content

Instantly share code, notes, and snippets.

@LindsayBradford
Created May 3, 2014 08:47
Show Gist options
  • Save LindsayBradford/bb6e440186ec92c0fd21 to your computer and use it in GitHub Desktop.
Save LindsayBradford/bb6e440186ec92c0fd21 to your computer and use it in GitHub Desktop.
Reacting to Java Swing JTabbedPane mouse events over a tab.
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JTabbedPane;
import java.util.HashMap;
import java.util.Runnable;
public class MouseReactiveTabbedPane extends JTabbedPane {
private HashMap<Integer,Runnable> runnableMap;
public MouseReactiveTabbedPane() {
super();
runnableMap = new HashMap<Integer, Runnable>();
final MouseReactiveTabbedPane pane = this;
this.addMouseMotionListener(
new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
for (int i = 0; i < pane.getTabCount(); i++){
if (pane.getBoundsAt(i).contains(e.getPoint())) {
if (runnableMap.containsKey(Integer.valueOf(i))) {
Runnable tabRunnable = runnableMap.get(Integer.valueOf(i));
tabRunnable.run();
}
} // if event point is within tab's bounds
} // for all tab pane indices
}
});
}
public void setRunnableForTab(int tabIndex, Runnable runnable) {
runnableMap.put(Integer.valueOf(i), runnable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment