Add image to JCheckBoxMenuItem

627 views Asked by At

I have been given an assignment in which i need to a use JCheckBoxMenuItem and add an image to it on the right side

enter image description here

I've used the setIcon() method.

Created a custom panel and added image to it and then adding the panel to checkbox.

Tried to add panel like below.

   JCheckBoxMenuItem item = new JCheckBoxMenuItem();
    item.setText("Option1");
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JLabel label = new JLabel(new ImageIcon(
                        "C:\\Users\\abcd\\Desktop\\facebook.jpg"));
    panel.add(label);
    item.add(panel);

The above seemed like working but only image on the right side was visible and the checkbox and text were missing.

2

There are 2 answers

1
Andrew Thompson On BEST ANSWER

This can be done with a standard check box menu item, simply by adjusting the horizontal text position.

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CheckBoxMenuItemIconPosition {

    private JComponent ui = null;
    private JMenuBar mb = null;

    CheckBoxMenuItemIconPosition() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(40,160,40,160));
    }

    public JComponent getUI() {
        return ui;
    }

    public JMenuBar getMenuBar() {
        if (mb != null) return mb;

        mb = new JMenuBar();

        JMenu fileMenu = new JMenu("File");
        mb.add(fileMenu);

        BufferedImage bi = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
        JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem(
                "Text", new ImageIcon(bi));
        checkBoxMenuItem.setHorizontalTextPosition(SwingConstants.LEFT);
        fileMenu.add(checkBoxMenuItem);

        return mb;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                CheckBoxMenuItemIconPosition o = new CheckBoxMenuItemIconPosition();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.setJMenuBar(o.getMenuBar());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
0
VILLAIN bryan On

See http://www.java2s.com/Code/Java/Swing-JFC/Aquickdemonstrationofcheckboxmenuitems.htm

Take a minute to read through this code.

TLDR:

JMenuToolbar jmt = new JMenuToolBar(); // ignore for now, will be added to JFrame

JMenu menu = new JMenu("File") // create a new JMenu that can be 'dropped down'
JCheckBoxMenuItem open = new JCheckBoxMenuItem("Open",new ImageIcon("open_img.gif")); // add a JCheckBoxMenuItem to add to JMenu

menu.add(open); // add to menu
jmt.add(menu);  // add to JMenuToolBar

// in main or wherever, add the JMenuToolBar
JFrame frame = new JFrame("Window");
frame.add(jmt); // add to main Frame