Code
class UnderlineFocusTabbedPane extends JTabbedPane {
private static final Border DEFAULT_BORDER =
BorderFactory.createMatteBorder(0, 0, 3, 0, new Color(0x0, true));
private static final Border SELECTED_BORDER =
BorderFactory.createMatteBorder(0, 0, 3, 0, new Color(0x00_AA_FF));
protected UnderlineFocusTabbedPane() {
super();
}
@Override public void updateUI() {
super.updateUI();
// setFocusable(false);
if (getUI() instanceof WindowsTabbedPaneUI) {
setUI(new WindowsTabbedPaneUI() {
@Override protected void paintFocusIndicator(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect, boolean isSelected) {
super.paintFocusIndicator(g, tabPlacement, rects, tabIndex, iconRect, textRect, false);
}
});
} else {
setUI(new BasicTabbedPaneUI() {
@Override protected void paintFocusIndicator(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect, boolean isSelected) {
super.paintFocusIndicator(g, tabPlacement, rects, tabIndex, iconRect, textRect, false);
}
});
}
addChangeListener(e -> {
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
if (tabbedPane.getTabCount() <= 0) {
return;
}
int idx = tabbedPane.getSelectedIndex();
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
Component c = tabbedPane.getTabComponentAt(i);
if (c instanceof JComponent) {
((JComponent) c).setBorder(i == idx ? SELECTED_BORDER : DEFAULT_BORDER);
}
}
});
}
@Override public void insertTab(
String title, Icon icon, Component component, String tip, int index) {
super.insertTab(title, icon, component, tip, index);
JLabel label = new JLabel(title, icon, SwingConstants.CENTER);
setTabComponentAt(index, label);
}
}
References
No comments:
Post a Comment