Code
class LineFocusTabbedPane extends JTabbedPane {
private transient ChangeListener listener;
protected LineFocusTabbedPane() {
super();
}
@Override public void updateUI() {
removeChangeListener(listener);
UIManager.put("TabbedPane.tabInsets", new InsetsUIResource(1, 4, 0, 4));
UIManager.put("TabbedPane.selectedTabPadInsets", new InsetsUIResource(1, 1, 1, 1));
UIManager.put("TabbedPane.tabAreaInsets", new InsetsUIResource(3, 2, 0, 2));
UIManager.put("TabbedPane.selectedLabelShift", 0);
UIManager.put("TabbedPane.labelShift", 0);
UIManager.put("TabbedPane.focus", new ColorUIResource(new Color(0x0, true)));
super.updateUI();
listener = new TabSelectionListener();
addChangeListener(listener);
setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
@Override public void insertTab(String title, Icon icon, Component c, String tip, int index) {
super.insertTab(title, icon, c, tip, index);
JLabel label = new JLabel(title, icon, SwingConstants.CENTER);
setTabComponentAt(index, label);
}
}
class TabSelectionListener implements ChangeListener {
private static final Color ALPHA_ZERO = new Color(0x0, true);
private static final Color SELECTION_COLOR = new Color(0x00_AA_FF);
private static final Color PREV_COLOR = new Color(0x48_00_AA_FF, true);
private int prev = -1;
@Override public void stateChanged(ChangeEvent e) {
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
if (tabbedPane.getTabCount() <= 0) {
return;
}
int idx = tabbedPane.getSelectedIndex();
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component tab = tabbedPane.getTabComponentAt(i);
if (tab instanceof JComponent) {
Color color;
if (i == idx) {
color = SELECTION_COLOR;
} else if (i == prev) {
color = PREV_COLOR;
} else {
color = ALPHA_ZERO;
}
((JComponent) tab).setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0, color));
}
}
prev = idx;
}
}
References
No comments:
Post a Comment