1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gurpaldeep
  • 61
  • 7
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Jun 08 '15 at 06:27
  • .. 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jun 08 '15 at 06:27
  • BTW - why are you adding the icon to a `JLabel` when `JCheckBoxMenuItem` already has a `setIcon()` method? – Andrew Thompson Jun 08 '15 at 06:30
  • Actually when I am using setIcon() method the checkbox and icon overlap each other – Gurpaldeep Jun 08 '15 at 06:35
  • I tried the way it is given in this question's answer http://stackoverflow.com/questions/10465736/align-icon-and-jcheckbox-in-jpopupmenu But i see the icon on both sides – Gurpaldeep Jun 08 '15 at 06:39

2 Answers2

3

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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I am able to align the text to the left side of the icon but the problem is that there is one more icon instead of a check box on the left side of the text that is I see two icons for a JCheckBoxMenuItem but no checkbox. – Gurpaldeep Jun 08 '15 at 07:04
2

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
VILLAIN bryan
  • 701
  • 5
  • 24