In this tutorial you will learn:
- What is
JCheckBox
Control in Java Swing? - How to create
JCheckBox
control? - Simple
JCheckBox
Programming JCheckBox
withActionListener
- Track
JCheckBox
Status on Button Click - Save and Retrieve
JCheckBox
to Database
What is JCheckBox
Control in Java Swing?
JCheckBox
provides multiple option chooser panel to the user. It is like a toggle button that gets activated while checked on it and gets deactivated when unchecked it. If user has to choose multiple option then JCheckBox
control is used.
How to Create or Initialize JCheckBox Control?
You can create JCheckBox control in swing like that.
1 2 3 4 5 6 7 8 9 10 11 | JCheckBox checkbox; checkbox = new JcheckBox("String Message"); //Check JCheckBox Status if(checkbox.isSelected()) { //Statement } //Check and Uncheck JCheckBox checkbox.setSelected(true) |
Simple JCheckBox Example
Here, I created simple JCheckBox example which will teach you how to draw JCheckBox on your swing apps. In the next example you will learn JCheckBox with ActionListener.
Programming Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package Test.MainJava.com; import javax.swing.*; public class JCheckBox_Example { JFrame frame; JLabel lbltext; JCheckBox boxjava, boxc,boxcplus; public static void main(String[] args) { JCheckBox_Example j=new JCheckBox_Example(); j.Draw(); } public void Draw() { boxjava = new JCheckBox("Java"); boxc = new JCheckBox("C"); boxcplus = new JCheckBox("C++"); lbltext = new JLabel("Which Language do you know?"); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); BoxLayout boxlayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); frame.setLayout(boxlayout); frame.add(lbltext); frame.add(boxjava); frame.add(boxc); frame.add(boxcplus); frame.setVisible(true); } } |
JCheckBox with ActionListener
This program shows you how can you write code under JCheckBox Checked or Unchecked event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package Test.MainJava.com; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class JCheckBox_Example { JFrame frame; JLabel lbltext, lblmessage; JCheckBox boxjava, boxc,boxcplus; JButton btntest; public static void main(String[] args) { JCheckBox_Example j=new JCheckBox_Example(); j.Draw(); } public void Draw() { boxjava = new JCheckBox("Java"); boxc = new JCheckBox("C"); boxcplus = new JCheckBox("C++"); lbltext = new JLabel("Which Language do you know?"); lblmessage = new JLabel(); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); BoxLayout boxlayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); frame.setLayout(boxlayout); frame.add(lbltext); frame.add(boxjava); frame.add(boxc); frame.add(boxcplus); frame.add(lblmessage); frame.setVisible(true); //Add ActionListener boxjava.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JCheckBox cbox = (JCheckBox) e.getSource(); if(cbox.isSelected()) { lblmessage.setText("You Selected " + cbox.getText()); } else { lblmessage.setText("You have unchecked Java"); } } }); } } |
Track JCheckBox
Status on Button Click
In this example, you will learn how to track JCheckBox status on button click. Most of the time you may want to perform certain task on JCheckBox checked or unchecked status. This programming example helps you to understand this scenario.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package Test.MainJava.com; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class JCheckBox_Example { JFrame frame; JLabel lbltext, lblmessage; JCheckBox boxjava, boxc,boxcplus; JButton btntest; public static void main(String[] args) { JCheckBox_Example j=new JCheckBox_Example(); j.Draw(); } public void Draw() { boxjava = new JCheckBox("Java"); boxc = new JCheckBox("C"); boxcplus = new JCheckBox("C++"); lbltext = new JLabel("Which Language do you know?"); lblmessage = new JLabel(); btntest = new JButton("Checkbox Checker Button"); btntest.addActionListener(actions); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); BoxLayout boxlayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); frame.setLayout(boxlayout); frame.add(lbltext); frame.add(boxjava); frame.add(boxc); frame.add(boxcplus); frame.add(btntest); frame.add(lblmessage); frame.setVisible(true); } private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String msg1,msg2,msg3; if(e.getSource() == btntest) { if(boxjava.isSelected()) { msg1 = "Java, "; } else { msg1=""; } if(boxc.isSelected()) { msg2="C, "; } else { msg2=""; } if(boxcplus.isSelected()) { msg3="C++"; } else { msg3=""; } lblmessage.setText("You have chosen : " + msg1 + msg2 + msg3); } } }; } |
Save and Retrieve JCheckBox
to Database
Saving and Retrieving JCheckBox value in database is very easy. You just need to store 0 or 1 in the table. If JCheckBox is unchecked, store 0 into table and if JCheckBox is checked then store 1 into table. Here, is the complete programming example.
I created a new table JCheckBox_Table in database. I will use this table for storing checked value. You must insert a default row in JCheckBox_Table manually because this program will only update row for storing checkbox value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | package Test.MainJava.com; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.*; public class JCheckBox_Example { //Connection Credential static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String dburl = "jdbc:mysql://localhost/swing_db"; static final String dbuser = "root"; static final String dbpass = "root"; Connection con=null; Statement stmt=null; //Control Initialization JFrame frame; JLabel lbltext, lblmessage; JCheckBox boxjava, boxc,boxcplus; JButton btnSave,btnGet; public static void main(String[] args) { JCheckBox_Example j=new JCheckBox_Example(); j.Draw(); } //Constructing Form Design public void Draw() { boxjava = new JCheckBox("Java"); boxc = new JCheckBox("C"); boxcplus = new JCheckBox("C++"); lbltext = new JLabel("Which Language do you know?"); lblmessage = new JLabel(); btnSave = new JButton("Save CheckBox"); btnSave.addActionListener(actions); btnGet = new JButton("Get CheckBox"); btnGet.addActionListener(actions); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); BoxLayout boxlayout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS); frame.setLayout(boxlayout); frame.add(lbltext); frame.add(boxjava); frame.add(boxc); frame.add(boxcplus); frame.add(btnSave); frame.add(btnGet); frame.add(lblmessage); frame.setVisible(true); } private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == btnSave) { SaveData(); } else if(e.getSource()== btnGet) { GetData(); } } }; public void SaveData() { int val1, val2, val3; if(boxjava.isSelected()) val1=1; else val1=0; if(boxc.isSelected()) val2=1; else val2=0; if(boxcplus.isSelected()) val3=1; else val3=0; try { con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); String query="UPDATE JCheckBox_Table SET Java = "+val1+", C = "+val2+", CPlus = "+val3+" WHERE ID=1"; stmt.executeUpdate(query); lblmessage.setText("Saved Successfully."); } catch (SQLException ex) { System.err.println("Cannot connect ! "); ex.printStackTrace(); } finally { System.out.println("Closing the connection."); if (con != null) try { con.close(); } catch (SQLException ignore) {} } } public void GetData() { Connection con = null; Statement stmt = null; int val1 = 0, val2 = 0, val3 = 0; try { con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); String query="SELECT * FROM JCheckBox_Table WHERE ID=1"; ResultSet rset = stmt.executeQuery(query); while(rset.next()) { val1 = rset.getInt(2); val2 = rset.getInt(3); val3 = rset.getInt(4); } if(val1==0) boxjava.setSelected(false); else boxjava.setSelected(true); if(val2==0) boxc.setSelected(false); else boxc.setSelected(true); if(val3==0) boxcplus.setSelected(false); else boxcplus.setSelected(true); } catch (SQLException ex) { System.err.println("Cannot connect ! "); ex.printStackTrace(); } finally { System.out.println("Closing the connection."); if (con != null) try { con.close(); } catch (SQLException ignore) {} } } } |
Summary:
In this tutorial, you learned how to use JCheckBox in Java Swing with complete programming example. In the next chapter you will learn about Java JRadioButton.