---
title: "How to work with JCheckBox with Image"  
description: "How to work with JCheckBox with Image"  
author: "Anonymous User"  
published: 2015-02-04  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/12943/how-to-work-with-jcheckbox-with-image  
category: "oops"  
tags: ["java", "swing"]  
reading_time: 3 minutes  

---

# How to work with JCheckBox with Image

How can I have a ceckbox grup with [image](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character)?

I use:

```
for (String testata : listaTestate) {        JCheckBox checkbox = new JCheckBox();        ImageIcon imgTestata = new ImageIcon("src/img/"+testata+".png");        checkbox.setName(testata);        checkbox.setBackground(new Color(194, 169, 221));        checkbox.setSelected(true);        testate.add(checkbox);        JLabel label = new JLabel(imgTestata);        label.setBackground(new Color(194, 169, 221));        label.setPreferredSize(new Dimension(500, 50));        testate.add(label);    }
```

but a lot of [space](https://www.mindstick.com/articles/12954/do-your-electrical-repair-in-your-own-space) between the ImageIcon and the JCheckBox

## Replies

### Reply by Aryan Kumar

To work with a JCheckBox with image in Java, you can use the following steps:

1. Import the necessary libraries.
2. Create a JFrame.
3. Create a JCheckBox with image.
4. Add the JCheckBox to the JFrame.
5. Set the size and location of the JCheckBox.
6. Set the text of the JCheckBox.
7. Set the image of the JCheckBox.
8. Set the action listener for the JCheckBox.
9. Show the JFrame.

Here is an example of how to work with a JCheckBox with image in Java:

Java

```plaintext
import javax.swing.*;
import java.awt.*;

public class JCheckBoxWithImage {

    public static void main(String[] args) {
        JFrame frame = new JFrame("JCheckBox with Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a JCheckBox with image
        JCheckBox checkBox = new JCheckBox();
        checkBox.setIcon(new ImageIcon("check_box.png"));

        // Add the JCheckBox to the JFrame
        frame.add(checkBox);

        // Set the size and location of the JCheckBox
        checkBox.setBounds(100, 100, 100, 100);

        // Set the text of the JCheckBox
        checkBox.setText("Check Box");

        // Set the image of the JCheckBox
        checkBox.setIcon(new ImageIcon("check_box.png"));

        // Set the action listener for the JCheckBox
        checkBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("The checkbox is checked");
            }
        });

        // Show the JFrame
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}
```

This code will create a JFrame with a JCheckBox with image. The JCheckBox will be located at (100, 100) and have a size of 100 x 100. The text of the JCheckBox will be "Check Box" and the image of the JCheckBox will be "check_box.png". When the JCheckBox is checked, the following message will be printed to the console: "The checkbox is checked".

### Reply by Anonymous User

Without knowing the layout manager, it's difficult to be 100%, but if I was doing this, I might use a GridBagLayout...

```
testate.setLayout(new GridBagLayout());GridBagConstraints gbc = new GridBagConstraints();gbc.gridy = 0;gbc.insets = new Insets(4, 4, 4, 4);gbc.anchor = GridBagConstraints.WEST;for (String testata : listaTestate) {    gbc.gridx = 0;    JCheckBox checkbox= new JCheckBox();    ImageIcon imgTestata = new ImageIcon(getClass().getResource("/img/"+testata+".png"));    checkbox.setName(testata);    checkbox.setBackground(new Color(194, 169, 221));    checkbox.setSelected(true);    testate.add(checkbox,gbc);    gbc.gridx++;    JLabel label = new JLabel(imgTestata);    label.setBackground(new Color(194, 169, 221));    testate.add(label,gbc);    gbc.gridy++;}
```


---

Original Source: https://www.mindstick.com/forum/12943/how-to-work-with-jcheckbox-with-image

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
