-4

I been searching all the websites and also in youtube to find out how to use to use one jcheckbox in my project.

I want to function this checkbox as enable and disable, also i want to use to inter single data into database table

James Z
  • 12,209
  • 10
  • 24
  • 44
  • No idea what you are asking for. Turn to the tutorials from oracle for swing, and start working with all the example code. Then write some code, and when you are still stuck, put together a [mcve] and ask for help with that. This question is simply not answerable. – GhostCat Dec 31 '18 at 12:04
  • I have a jFrame an created for user i added jcheckbox to make the user account activate or deactivate < for example if I have user which been working for me after 1 year that person left the place so now i need to unchecked the box so this user will be blocked in instead of deleting the username and all the record which been proceed under that person name get deleted> and also i want to use this another check box for giving a value of extra bed for example in my system – Hussain AlBastaki Dec 31 '18 at 13:07
  • Nothing you just said changes anything what I said. If you have not working code then add that into your question. We can't help you finding a bug in code we can't see. Again: read [mcve] and enhance your question accordingly. – GhostCat Dec 31 '18 at 13:53

1 Answers1

0

JCheckBox is quite a simple component.

You can use one of its constructors to create a JCheckBox object.
You can use isSelected() method to check whether it is ticked or not.
You can use setSelected(boolean) method to tick or untick it programmatically.
You can use addActionListener() method to register a listener to get notifications when user tick it or untick it. (There are few other listeners as well.)

Simple sample program:

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

public class SimpleCheckBox
{
  public static void main(String[] args)
  {
    JCheckBox checkBox = new JCheckBox("Active");
    checkBox.addActionListener(e -> System.out.println("User clicked the check box"));

    JButton print = new JButton("Print status");
    print.addActionListener(e -> System.out.println("Selected: " + checkBox.isSelected()));

    JButton select = new JButton("Select");
    select.addActionListener(e -> checkBox.setSelected(true));

    JButton deselect = new JButton("Deselect");
    deselect.addActionListener(e -> checkBox.setSelected(false));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setLayout(new GridLayout(4, 1));
    f.getContentPane().add(checkBox);
    f.getContentPane().add(print);
    f.getContentPane().add(select);
    f.getContentPane().add(deselect);
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16