4

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Andreas Freitag
  • 357
  • 1
  • 7
  • 20
  • 1
    Had you tried using [setIconTextGap(...)](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setIconTextGap(int)), along with [setHorizontalTextPosition(...)](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setHorizontalTextPosition(int)) and [setVerticalTextPosition(...)](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setVerticalTextPosition(int)), and the last one [setHorizontalAlignment(...)](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setHorizontalAlignment(int)) ? – nIcE cOw May 06 '12 at 02:54
  • Thanks for your answer! But these did not work. – Andreas Freitag May 06 '12 at 06:26
  • Your Welcome and Keep Smiling, then please do check your images used by you, might be they have something on the left side of themselves, that's occupying that space, remove that by using any software, like Paint, NeroSnapViewer, Gimp, etc. :-) – nIcE cOw May 06 '12 at 07:34
  • @Andreas Freitag this is possible by set LayoutManager to the JMenuItem, but I never tried if required to layout only to plain JMenuItem or/wiht JCheckBoxMenuItem too, you have to try that, – mKorbel May 06 '12 at 07:44
  • @mKorbel and nIcE cOw Thanks for your answers. No, all icons are 32*32 and resized by java! I have tried out some different layouts, but none of them worked. It looks like the Checkbox is part of the text, so the layoutmanager cant fit it. :( – Andreas Freitag May 06 '12 at 12:31
  • @Andreas Freitag aaaach I'm so lazy today, hmmm must to wait a few minutes, for icons at 32x32 – mKorbel May 06 '12 at 12:33

2 Answers2

3

right now I can't found the right way how to re_layout JCheckBoxMenuItem,

but do you agree with this standars output from Swing by using (default) Metal Look and Feel???, just to avoiding any missunderstand by using another Look and Feel(s), because there are some differencies in the API betweens Swing's Standard Look and Feels too

enter image description here

from tutorials code (modified and removed balasts and noises)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuLookDemo {

    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private JTextArea output;
    private JScrollPane scrollPane;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem menuItem;
    private JRadioButtonMenuItem rbMenuItem;
    private JCheckBoxMenuItem cbMenuItem;

    public JMenuBar createMenuBar() {
        menuBar = new JMenuBar();
        menu = new JMenu("A Menu");
        menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
        menuBar.add(menu);
        menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
        menu.add(menuItem);
        menuItem = new JMenuItem("Both text and icon", errorIcon);
        menu.add(menuItem);
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();
        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        rbMenuItem = new JRadioButtonMenuItem("Another one", infoIcon);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        menu.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("A check box menu item", warnIcon);
        menu.add(cbMenuItem);
        cbMenuItem = new JCheckBoxMenuItem("Another one");
        menu.add(cbMenuItem);
        menu.addSeparator();
        return menuBar;
    }

    public Container createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        return contentPane;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MenuLookDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MenuLookDemo demo = new MenuLookDemo();
        frame.setJMenuBar(demo.createMenuBar());
        frame.setContentPane(demo.createContentPane());
        frame.setSize(450, 260);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

these methods talking about possitions in pixels

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @aterai can you [please to elaborate](http://java-swing-tips.blogspot.com/2009/10/jcheckboxmenuitem-icon.html) – mKorbel May 06 '12 at 12:58
  • Thanks for your Post! I have simply added icons to the checkboxes (like you did). It now looks better, but it is not perfect! It is a good workaround. Maybe we can find a good solution later. But for now I am happy with it. – Andreas Freitag May 06 '12 at 13:35
  • @Andreas Freitag use 1)JMenuItem.setIconTextGap(0); and 2) look if is usefull [Popup Menu Alignment](http://tips4java.wordpress.com/2008/12/03/popup-menu-alignment/), 3) we both hove to waiting for his majesty (@aterai), he is known as great Swing guru – mKorbel May 06 '12 at 13:43
  • +1, this should work, don't know of any other way to achieve this :( – nIcE cOw May 06 '12 at 15:00
  • @nIcE cOw you can get AbstractButton or BasicMenuUI from any of JMenuItems, there is to able to override and force no_standard methods or alignments, – mKorbel May 06 '12 at 15:32
  • @mKorbel : Seems like I had found a simple workaround, so I had posted my answer too, as to how I accomplished that :-) – nIcE cOw May 06 '12 at 16:22
3

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

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143