Java Swing Tutorial - Java JCheckBox(String text, boolean selected) Constructor








Syntax

JCheckBox(String text, boolean selected) constructor from JCheckBox has the following syntax.

public JCheckBox(String text,   boolean selected)

Example

In the following code shows how to use JCheckBox.JCheckBox(String text, boolean selected) constructor.

//  w  ww. j  ava  2 s.  c o m
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class Main extends JFrame implements ActionListener {

  public Main() {
    JCheckBox checkbox = new JCheckBox("Show Title", true);
    checkbox.addActionListener(this);
    add(checkbox);

    setSize(280, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  public static void main(String[] args) {
    new Main();
  }

  public void actionPerformed(ActionEvent e) {
    if (this.getTitle() == "") {
      this.setTitle("Checkbox example");
    } else {
      this.setTitle("");
    }
  }
}