Created
May 3, 2014 08:47
-
-
Save LindsayBradford/bb6e440186ec92c0fd21 to your computer and use it in GitHub Desktop.
Reacting to Java Swing JTabbedPane mouse events over a tab.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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