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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cheesegraterr
  • 517
  • 3
  • 14
  • 26

3 Answers3

26

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

Laf
  • 7,965
  • 4
  • 37
  • 52
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.

Marcelo
  • 11,218
  • 1
  • 37
  • 51
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.

BVSmallman
  • 601
  • 3
  • 9