Frederico Pantuzza
Frederico Pantuzza

Reputation: 327

JLabel with icon in JPopupMenu doesn't follow other JMenuItem alignment

I need to add a label at the top of a JPopupMenu that contains other JMenuItems in it. So far, so good. The problem happens because my label has an icon on its left side, just like a "normal" JMenuItem, but the other items won't follow the labels alignment like they do with a JMenuItem. Like this:

JPopupMenu with JMenuItem and icon

The first item is a JMenuItem with an icon. Notice that all other JMenuItem will follow the text alignment of the one with the wider icon.

Now, if I add a JLabel with an icon to the JPopupMenu, the effect won't be the same:

JPopupMenu with JLabel and icon

Or even worse, if I add a JLabel with no icon to a JPopupMenu that contains other JMenuItems with icons, the label won't follow the alignment of the other and will remain aligned to the left:

JPopupMenu with JLabel and JMenuItem with icon

My question: Is it possible to attach a JLabel alignment (icon and text) to the alignment of the JMenuItems in a JPopupMenu? If yes, how?

Upvotes: 2

Views: 659

Answers (1)

aterai
aterai

Reputation: 9833

  • Maybe can use a disabled JMenuItem instead of a JLabel.
  • EDIT: WindowsLookAndFeel only works fine.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class MenuItemAlignmentTest2 {
  public JComponent makeUI() {
    JTextArea textArea = new JTextArea();
    textArea.setComponentPopupMenu(makeTestPopupMenu(null));

    JTree tree = new JTree();
    tree.setComponentPopupMenu(makeTestPopupMenu(new ColorIcon(Color.RED)));

    JPanel p = new JPanel(new GridLayout(1, 2));
    p.add(new JScrollPane(textArea));
    p.add(new JScrollPane(tree));
    return p;
  }
  private static JPopupMenu makeTestPopupMenu(Icon icon) {
    //UIManager.put("MenuItem.disabledAreNavigable", Boolean.FALSE);
    //UIManager.put("MenuItem.disabledForeground", Color.RED);
    JMenuItem item0 = new JMenuItem("JMenuItem.setEnabled(false);");
    item0.setEnabled(false);

    JLabel item1 = new JLabel("JLabel");

    JPanel item2 = new JPanel(new BorderLayout());
    item2.setOpaque(false); //<----------------------------- add this line
    item2.add(new JMenuItem("JPanel with JMenuItem") {
      @Override public boolean contains(int x, int y) {
        return false; //disable mouse events
      }
    });

    JMenuItem item3 = new JMenuItem(" ");
    item3.setBorder(BorderFactory.createEmptyBorder()); //<- add this line
    item3.setEnabled(false);
    item3.add(new JMenuItem("JMenuItem(disabled) with JMenuItem") {
      @Override public boolean contains(int x, int y) {
        return false; //disable mouse events
      }
    });

    JPopupMenu popup = new JPopupMenu();
    popup.add(makeTestMenuItems("Test0", item0, icon));
    popup.add(makeTestMenuItems("Test1", item1, icon));
    popup.add(makeTestMenuItems("Test2", item2, icon));
    popup.add(makeTestMenuItems("Test3", item3, icon));

    return popup;
  }
  private static JMenu makeTestMenuItems(String title, JComponent item, Icon icon) {
    JMenu menu = new JMenu(title);
    menu.add(item);
    menu.addSeparator();
    menu.add(new AbstractAction("JMenuItem") {
      @Override public void actionPerformed(ActionEvent e) {
        System.out.println("actionPerformed");
      }
    });
    menu.add("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    menu.add(new JMenuItem("bbbbb", icon));
    return menu;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (ClassNotFoundException | InstantiationException
        | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      JMenuBar mb = new JMenuBar();
      mb.add(LookAndFeelUtil.createLookAndFeelMenu());
      f.setJMenuBar(mb);

      f.getContentPane().add(new MenuItemAlignmentTest2().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

final class LookAndFeelUtil {
  private static String lookAndFeel = UIManager.getLookAndFeel().getClass().getName();
  public static JMenu createLookAndFeelMenu() {
    JMenu menu = new JMenu("LookAndFeel");
    ButtonGroup lookAndFeelRadioGroup = new ButtonGroup();
    for (UIManager.LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
      menu.add(createLookAndFeelItem(
          lafInfo.getName(), lafInfo.getClassName(), lookAndFeelRadioGroup));
    }
    return menu;
  }
  private static JRadioButtonMenuItem createLookAndFeelItem(
      String lafName, String lafClassName, ButtonGroup lookAndFeelRadioGroup) {
    JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem();
    lafItem.setSelected(lafClassName.equals(lookAndFeel));
    lafItem.setHideActionText(true);
    lafItem.setAction(new AbstractAction() {
      @Override public void actionPerformed(ActionEvent e) {
        ButtonModel m = lookAndFeelRadioGroup.getSelection();
        try {
          setLookAndFeel(m.getActionCommand());
        } catch (ClassNotFoundException | InstantiationException
                   | IllegalAccessException | UnsupportedLookAndFeelException ex) {
          ex.printStackTrace();
        }
      }
    });
    lafItem.setText(lafName);
    lafItem.setActionCommand(lafClassName);
    lookAndFeelRadioGroup.add(lafItem);
    return lafItem;
  }
  private static void setLookAndFeel(String lookAndFeel)
      throws ClassNotFoundException, InstantiationException,
             IllegalAccessException, UnsupportedLookAndFeelException {
    String oldLookAndFeel = LookAndFeelUtil.lookAndFeel;
    if (!oldLookAndFeel.equals(lookAndFeel)) {
      UIManager.setLookAndFeel(lookAndFeel);
      LookAndFeelUtil.lookAndFeel = lookAndFeel;
      updateLookAndFeel();
      //firePropertyChange("lookAndFeel", oldLookAndFeel, lookAndFeel);
    }
  }
  private static void updateLookAndFeel() {
    for (Window window : Frame.getWindows()) {
      SwingUtilities.updateComponentTreeUI(window);
    }
  }
}

class ColorIcon implements Icon {
  private final Color color;
  protected ColorIcon(Color color) {
    this.color = color;
  }
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.translate(x, y);
    g2.setPaint(color);
    g2.fillRect(1, 1, getIconWidth() - 2, getIconHeight() - 2);
    g2.dispose();
  }
  @Override public int getIconWidth() {
    return 12;
  }
  @Override public int getIconHeight() {
    return 12;
  }
}

Upvotes: 2

Related Questions