In this JRadioButton Tutorial, you will learn:
1. What is JRadioButton Control in Java Swing?
2. How to create JRadioButton control?
3. Simple JRadioButton Example
4. JRadioButton Selection with ActionListener
5. Track JRadioButton Status on Button Click
6. Save and Retrieve JRadioButton Status in Database
In this Swing JRadioButton Tutorial
, you will learn everything about JRadioButton Control with complete and easy programming example.
1. What is JRadioButton
Control in Java Swing?
JRadioButton
is an option chooser control where you have multiple options but you can choose only one option. It can be selected or deselected and when you select one option, the other option in the same panel will be automatically deselected.
2. How to Create JRadioButton?
You can create JRadioButton like this example
1 | JRadioButton radioButton = new JRadioButton("Option 1"); |
You must import javax.swing.JRadioButton;
in your program.
3. Simple JRadioButton Example
This is the simple JRadioButton Example, in which you learn how to define JRadioButton and place into a form. I have created only 3 Radio buttons and combined them all in one button group.
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 39 40 41 | package Test.MainJava.com; import java.awt.FlowLayout; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JRadioButton; public class JRadioButton_Example { JRadioButton option1, option2, option3; JFrame frame; public static void main(String[] args) { JRadioButton_Example j=new JRadioButton_Example(); j.Draw(); } public void Draw() { option1 = new JRadioButton("Option1"); option2 = new JRadioButton("Option2"); option3 = new JRadioButton("Option3"); ButtonGroup group1 = new ButtonGroup(); group1.add(option1); group1.add(option2); group1.add(option3); frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); frame.setLayout(new FlowLayout()); frame.add(option1); frame.add(option2); frame.add(option3); frame.setVisible(true); } } |
4. JRadioButton Selection with ActionListener
This example will teach you, how to write code under JRadioButton click event. Sometimes, you may want to write some logic when JRadioButton selected. You don’t want to wait for final button click and check the option right after their selection. In this example, when you click Option2, you will notice that lblmessage is changed into “You Selected Option 2”.
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 | package Test.MainJava.com; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class JRadioButton_Example { JRadioButton option1, option2, option3; JFrame frame; JLabel lblmessage; public static void main(String[] args) { JRadioButton_Example j=new JRadioButton_Example(); j.Draw(); } public void Draw() { option1 = new JRadioButton("Option1"); option1.setSelected(true); option2 = new JRadioButton("Option2"); option3 = new JRadioButton("Option3"); lblmessage = new JLabel(" Your Option is : "); ButtonGroup group1 = new ButtonGroup(); group1.add(option1); group1.add(option2); group1.add(option3); frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); frame.setLayout(new GridLayout(4,1)); frame.add(option1); frame.add(option2); frame.add(option3); frame.add(lblmessage); frame.setVisible(true); //Adding ActionListener to Option2 Radio Button option2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JRadioButton rbutton = (JRadioButton) e.getSource(); if(rbutton.isSelected()) lblmessage.setText("You Selected: Option 2"); } }); } } |
5. Track JRadioButton Status on Button Click
This programming example explains how to track radio button status on button click. This is very important to track radio button status on button click because when writing programming logic you need to determine which button is selected and according to that you save value into database.
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 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 | package Test.MainJava.com; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class JRadioButton_Example { JRadioButton option1, option2, option3; JFrame frame; JLabel lblmessage; JButton btnGet; public static void main(String[] args) { JRadioButton_Example j=new JRadioButton_Example(); j.Draw(); } public void Draw() { option1 = new JRadioButton("Option1"); option1.setSelected(true); option2 = new JRadioButton("Option2"); option3 = new JRadioButton("Option3"); lblmessage = new JLabel(" Your Option is : "); btnGet = new JButton("Click to Get Radio Button Status"); btnGet.addActionListener(actions); ButtonGroup group1 = new ButtonGroup(); group1.add(option1); group1.add(option2); group1.add(option3); frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); frame.setLayout(new GridLayout(5,1)); frame.add(option1); frame.add(option2); frame.add(option3); frame.add(btnGet); frame.add(lblmessage); frame.setVisible(true); } //Adding ActionListener to Button private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource()== btnGet) { if(option1.isSelected()) lblmessage.setText("You Selected Option 1"); else if(option2.isSelected()) lblmessage.setText("You Selected Option 2"); else lblmessage.setText("You Selected Option 3"); } } }; } |
6. Save and Retrieve JRadioButton Status in Database
In this example, you will learn how to save and retrieve JRadioButton into database.
I have created simple table and add a row.
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.GridLayout; 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.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class JRadioButton_Example { JRadioButton option1, option2, option3; JFrame frame; JLabel lblmessage; JButton btnGet, btnSave; //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; public static void main(String[] args) { JRadioButton_Example j=new JRadioButton_Example(); j.Draw(); } public void Draw() { option1 = new JRadioButton("Option1"); option1.setSelected(true); option2 = new JRadioButton("Option2"); option3 = new JRadioButton("Option3"); lblmessage = new JLabel(" Your Option is : "); btnGet = new JButton("Get Data"); btnGet.addActionListener(actions); btnSave = new JButton("Save Radio Button"); btnSave.addActionListener(actions); ButtonGroup group1 = new ButtonGroup(); group1.add(option1); group1.add(option2); group1.add(option3); frame = new JFrame("Simple JTextField Example"); frame.setSize(400, 200); frame.setLayout(new GridLayout(6,1)); frame.add(option1); frame.add(option2); frame.add(option3); frame.add(btnSave); frame.add(btnGet); frame.add(lblmessage); frame.setVisible(true); } //Adding ActionListener to Button private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource()== btnSave) { String value=""; if(option1.isSelected()) value = "option1"; else if(option2.isSelected()) value = "option2"; else value = "option3"; SaveData(value); } else if(e.getSource() == btnGet) { String str = GetData(); if(str=="option1") { option1.setSelected(true); } else if(str=="option2") { option2.setSelected(true); } else { option3.setSelected(true); } } } }; public void SaveData(String str) { try { con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); String query="UPDATE JRadioButton_Table SET Value = '" + str + "' 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 String GetData() { Connection con = null; Statement stmt = null; String Value = ""; try { con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); String query="SELECT * FROM JRadioButton_Table WHERE ID=1"; ResultSet rset = stmt.executeQuery(query); while(rset.next()) { Value = rset.getString(2); } } 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) {} } return Value; } } |
Summary
In this chapter, you learned Swing JRadioButton with example. You learned how to create and use JRadioButton in Java Swing. In the next tutorial, you will learn JComboBox in Java Swing.