how to set JCheckBox to have a check mark, or not within a program
Asked Answered
S

3

9

I am using a single JCheckBox as an un-editable indicator for when something happens in my program. Right now I have this:

public void update(Observable o, Object arg) {
    try {

        if (Controller.c.turn.equals(this)) {
            tp.turnCheckBox.setBorderPainted(true);
        }
        else {
            tp.turnCheckBox.setBorderPainted(false);
        }
    } catch (Exception e) {
    }

Basically, instead of painting the turnCheckBox border... I want to have a checkmark in it. It seems like it would be a simple pre-made method, maybe I am missing something but I can't get it to happen.

Skipton answered 7/12, 2011 at 20:4 Comment(0)
O
28

Using tp.turnCheckBox.setSelected (boolean isSelected) will check (or uncheck) the checkbox.

Ortego answered 7/12, 2011 at 20:7 Comment(4)
Thanks, I guess I didn't see that method!Skipton
Using the poster's sample code: tp.turnCheckBox.setSelected(Controller.c.turn.equals(this));Joshuajoshuah
Marcelo raises a good point in his answer, I suggest you take note of it too :o)Ortego
The OP also wants the checkbox to be not editable (readonly).Intuit
I
8

To use the checkBox in "readonly" mode, use:

tp.turnCheckBox.setEnabled(false);

To make the checkBox appear checked or not checked, use:

tp.turnCheckBox.setSelected(isSelected);

where isSelected is a boolean expression which indicates if the control is checked or not checked.

Intuit answered 7/12, 2011 at 20:10 Comment(0)
S
5

JCheckBox has a method from its super class setSelected(). For your future knowledge and searching, in the Api with these components there is typically a chain of super classes you can go up in search for methods such as these.

JCheckBox has to go up 2 levels of classes to AbstractButton to find the setSelected() method.

Sometimes the fastest way to find something like this is simply with an IDE's autocomplete.

Supple answered 7/12, 2011 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.