JComponent component = tip.getComponent(); if (component instanceof JMenuItem) { JMenuItem item = (JMenuItem) component; KeyStroke acc = item.getAccelerator(); String tipText = item.getToolTipText(); int mne = item.getMnemonic();  if (acc == null && mne > 0) acc = KeyStroke.getKeyStroke(Character.toUpperCase((char) mne), InputEvent.ALT_MASK, false); if (tipText != null && acc != null) { UIDefaults defaults = UIManager.getLookAndFeelDefaults(); accFore = defaults.getColor("MenuItem.acceleratorForeground"); accDeliminator = defaults.getString("MenuItem.acceleratorDelimiter"); accFont = defaults.getFont("MenuItem.acceleratorFont"); accText = getAcceleratorText(acc); text = tipText + "  " + accText; } else { accFore = null; accDeliminator = null; accFont = null; accText = null; } }
  public Dimension getPreferredSize(JComponent c)  {    JToolTip tip = (JToolTip) c;    FontMetrics fm;    Toolkit g = tip.getToolkit();    text = tip.getTipText();        // accelerator    JComponent component = tip.getComponent();    if (component instanceof JMenuItem)      {        JMenuItem item = (JMenuItem) component;        KeyStroke acc = item.getAccelerator();        String tipText = item.getToolTipText();        int mne = item.getMnemonic();        if (acc == null && mne > 0)          acc = KeyStroke.getKeyStroke(Character.toUpperCase((char) mne),                                        InputEvent.ALT_MASK, false);        if (tipText != null && acc != null)          {            UIDefaults defaults = UIManager.getLookAndFeelDefaults();            accFore = defaults.getColor("MenuItem.acceleratorForeground");            accDeliminator = defaults.getString("MenuItem.acceleratorDelimiter");            accFont = defaults.getFont("MenuItem.acceleratorFont");            accText = getAcceleratorText(acc);            text = tipText + "  " + accText;          }        else          {            accFore = null;            accDeliminator = null;            accFont = null;            accText = null;          }      }        Rectangle vr = new Rectangle();    Rectangle ir = new Rectangle();    Rectangle tr = new Rectangle();    Insets insets = tip.getInsets();    fm = g.getFontMetrics(tip.getFont());    SwingUtilities.layoutCompoundLabel(tip, fm, text, null,                                       SwingConstants.CENTER,                                       SwingConstants.CENTER,                                       SwingConstants.CENTER,                                       SwingConstants.CENTER, vr, ir, tr, 0);    return new Dimension(insets.left + tr.width + insets.right,                         insets.top + tr.height + insets.bottom);  }