Google Tag Manager

2022/01/31

Fix the JToolTip display of JList cells to correspond to after mouse wheel rotation

Code

JList<String> list1 = new JList<String>(model) {
  @Override public void updateUI() {
    super.updateUI();
    setCellRenderer(new TooltipListCellRenderer<>());
  }
};
JScrollPane scroll1 = new JScrollPane(list1);
scroll1.addMouseWheelListener(e -> {
  JScrollPane scrollPane = (JScrollPane) e.getComponent();
  Component view = scrollPane.getViewport().getView();
  MouseEvent event = SwingUtilities.convertMouseEvent(scrollPane, e, view);
  ToolTipManager.sharedInstance().mouseMoved(event);
});

References

2022/01/01

Change the selection focus Border of JTabbedPane to underline

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