Search code examples
javaswingjcheckboximageiconjmenuitem

Align icon and JCheckbox in JPopupmenu


I have a Problem with some icons and check-boxes in a JPopupMenu. The Check-boxes and Icons are not aligned

The Items are created like:

JMenuItem deleteAttributeMenuItem = new JMenuItem(locale.getString("TREE_AttrDelete"), iconDelete);

JMenuItem primaryKeyAttributeMenuItem = new JCheckBoxMenuItem(locale.getString("TREE_AttrChkBoxPK"));

Please take a look at the picture:

enter image description here

Any tips?


Solution

  • Have a look at this, in order to achieve what you wanted, I did this,

    JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
    cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
    cbmi.setIconTextGap(15);
    cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
    helpMenu.add(cbmi);
    

    And here is the OUTPUT of the said thingy :

    enter image description here