Google Tag Manager

2023/08/31

Hide the check icon on the JRadioButtonMenuItem and reduce the text offset

Code

UIManager.put(PRE + "minimumTextOffset", 10);
UIManager.put(PRE + "afterCheckIconGap", 0);
UIManager.put(PRE + "checkIconOffset", 0);
Icon checkIcon = getCheckIcon();
int height = checkIcon == null ? 22 : checkIcon.getIconHeight();
UIManager.put(PRE + "checkIcon", new EmptyIcon());
Dimension d = new Dimension(100, height);
JPopupMenu popup = new JPopupMenu();
ButtonGroup bg = new ButtonGroup();
Arrays.asList(
    makeMenuItem("0.5 pt", .5f, d),
    makeMenuItem("0.75 pt", .75f, d),
    makeMenuItem("1 pt", 1f, d),
    makeMenuItem("1.5 pt", 1.5f, d),
    makeMenuItem("2.25 pt", 2.25f, d),
    makeMenuItem("3 pt", 3f, d)
).forEach(m -> {
  popup.add(m);
  bg.add(m);
});
// ...
private static JMenuItem makeMenuItem(String txt, float width, Dimension d) {
  float px = width * Toolkit.getDefaultToolkit().getScreenResolution() / 72f;
  return new JRadioButtonMenuItem(txt, new LineIcon(new BasicStroke(px), d)) {
    @Override protected void init(String text, Icon icon) {
      super.init(text, icon);
      setHorizontalTextPosition(LEADING);
      setHorizontalAlignment(TRAILING);
    }

    @Override protected void paintComponent(Graphics g) {
      if (isSelected()) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setPaint(new Color(0xAA_64_AA_FF, true));
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.dispose();
      }
      super.paintComponent(g);
    }
  };
}

References

No comments:

Post a Comment