Google Tag Manager

2014/10/29

JMenuItem accelerator text alignment

Code

RightAcc

public static void paintAccText(
  Graphics g, MenuItemLayoutHelper lh, MenuItemLayoutHelper.LayoutResult lr,
  Color disabledForeground, Color acceleratorForeground,
  Color acceleratorSelectionForeground) {
  if (!lh.getAccText().equals("")) {
    ButtonModel model = lh.getMenuItem().getModel();
    g.setFont(lh.getAccFontMetrics().getFont());
    if (!model.isEnabled()) {
      // *** paint the accText disabled
      if (disabledForeground != null) {
        g.setColor(disabledForeground);
        SwingUtilities2.drawString(
          lh.getMenuItem(), g, lh.getAccText(),
          lr.getAccRect().x,
          lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
      } else {
        g.setColor(lh.getMenuItem().getBackground().brighter());
        SwingUtilities2.drawString(
          lh.getMenuItem(), g, lh.getAccText(),
          lr.getAccRect().x,
          lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
        g.setColor(lh.getMenuItem().getBackground().darker());
        SwingUtilities2.drawString(
          lh.getMenuItem(), g, lh.getAccText(),
          lr.getAccRect().x - 1,
          lr.getAccRect().y + lh.getFontMetrics().getAscent() - 1);
      }
    } else {
      // *** paint the accText normally
      if (model.isArmed() ||
          (lh.getMenuItem() instanceof JMenu && model.isSelected())) {
        g.setColor(acceleratorSelectionForeground);
      } else {
        g.setColor(acceleratorForeground);
      }
      SwingUtilities2.drawString(
        lh.getMenuItem(), g, lh.getAccText(),
        //lr.getAccRect().x, //++++++++++++++++++++
        lh.getViewRect().x + lh.getViewRect().width
        - lh.getMenuItem().getIconTextGap() - lr.getAccRect().width,
        //++++++++++++++++++++
        lr.getAccRect().y + lh.getAccFontMetrics().getAscent());
    }
  }
}

EastAcc

public static void paintAccText(
private static JMenuItem makeMenuItem2(JMenuItem mi) {
  final JLabel label = new JLabel(MenuItemUIHelper.getAccText(mi, "+"));
  label.setOpaque(true);
  JMenuItem item = new JMenuItem(mi.getText()) {
    @Override public void updateUI() {
      super.updateUI();
      if (getUI() instanceof WindowsMenuItemUI) {
        setUI(new WindowsMenuItemUI() {
          @Override protected void installDefaults() {
            super.installDefaults();
            acceleratorForeground = UIManager.getColor("MenuItem.background");
            acceleratorSelectionForeground = acceleratorForeground;
          }
        });
      }
    }
  };

  GridBagConstraints c = new GridBagConstraints();
  item.setLayout(new GridBagLayout());
  c.gridheight = 1;
  c.gridwidth  = 1;
  c.gridy = 0;
  c.gridx = 0;
  c.insets = new Insets(0, 0, 0, 4);

  c.weightx = 1d;
  c.fill = GridBagConstraints.HORIZONTAL;
  item.add(Box.createHorizontalGlue(), c);
  c.gridx = 1;
  c.fill = GridBagConstraints.NONE;
  c.weightx = 0d;
  c.anchor = GridBagConstraints.EAST;
  item.add(label, c);

  item.setMnemonic(mi.getMnemonic());
  item.setAccelerator(mi.getAccelerator());
  return item;
}

References

1 comment:

  1. Well, Thanks for your article.
    Its impressive to know something about your note on JAVA Applications. Please do share your articles like this your articles for our awareness. Mostly we do also provide JAVA Courses Online Training . For more info Contact-Us at@ Tectist.com.

    ReplyDelete