How can we set a border to JCheckBox in Java?\\n



In this article, we will learn to set a border for JCheckBox in Java. JCheckBox is a Swing component that is commonly used in user interface selection. Although it includes a default appearance, we can customize its look by setting borders in order to have a good visual effect.

What is a JCheckBox?

A JCheckBox is a component that extends JToggleButton, and an object of JCheckBox represents an option that can be checked or unchecked.

Syntax

The following is the syntax for JCheckBox initialization:

JCheckBox Checkbox_name = new JCheckBox();

If there are two or more options, then any combination of these options can be selected at the same time. A JCheckBox can generate either ItemListener or ActionListener interfaces.

Setting a Border for JCheckBox

We can set a border to the JCheckBox component by using the setBorder() method and make sure that the setBorderPainted() method is set to true.

Step-by-step process for setting a border for a JCheckBox in Java:

Class Declaration and Imports

The javax.swing.* provides Swing components (JCheckBox, JFrame, etc.) while the java.awt.* provides AWT classes (Color, layout managers), and the class extends JFrame to create a window.

import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class BorderedJCheckBoxTest extends JFrame {

Instance Variable & Constructor

Declares jcb as a JCheckBox component. The constructor initializes the frame and its components.

private JCheckBox jcb;
public BorderedJCheckBoxTest() throws Exception {
    }

Frame Setup & CheckBox Configuration

Sets the window title as "JCheckBox Test", uses FlowLayout for automatic component positioning, setBorderPainted(true), enables custom borders, and the setBorder() method applies a red line border using BorderFactory.

setTitle("JCheckBox Test");
setLayout(new FlowLayout());
jcb = new JCheckBox("BorderedJCheckBox Test");
jcb.setBorderPainted(true);
jcb.setBorder(BorderFactory.createLineBorder(Color.red));

Main Method

The main method launches the application by creating an object of BorderedJCheckBoxTest.

public static void main(String args[]) throws Exception {
    new BorderedJCheckBoxTest();
}

Example

Below is an example of setting a border for a JCheckBox in Java using the setBorder() method:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderedJCheckBoxTest extends JFrame {
   private JCheckBox jcb;
   public BorderedJCheckBoxTest() throws Exception {
      setTitle("JCheckBox Test");
      setLayout(new FlowLayout());
      jcb = new JCheckBox("BorderedJCheckBox Test");
      jcb.setBorderPainted(true);
      jcb.setBorder(BorderFactory.createLineBorder(Color.red)); // set the border
      add(jcb);
      setSize(375, 250);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   public static void main(String args[]) throws Exception {
      new BorderedJCheckBoxTest();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-05T18:33:11+05:30

656 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements