JAVA - How To Use JCheckBox In Java Swing

How To Use JCheckBox In Java NetBeans

_______________________________________________________________________________

package JavaDB_001;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Project extends JFrame {

    //declare JCheckBox Object
    JCheckBox ch;

public Project(){
     ch = new JCheckBox("CheckBox");
    //add Action to ch
    ch.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //if JCheckBox is selected
          if(ch.isSelected()){
              JOptionPane.showMessageDialog(null, "Checked");
          }
         // if JCheckBox is not selected
          else
              JOptionPane.showMessageDialog(null, "UnChecked");
        }
    });
    ch.setBounds(160, 30, 90, 20);
    add(ch);
    setLayout(null);
    setSize(400, 120);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.decode("#bdb76b"));
    setVisible(true);
}
public static void main(String[] args) throws IOException{
  new Project();
}
}

JCheckBox In Java
JCheckBox In JFrame




Share this

Related Posts

Previous
Next Post »