How To Use RadioButton In Java Using NetBeans
In this Java Tutorial we will see How To Add And Use Radio Button With Button Group
In Java NetBeans .
Project Source Code:
import java.awt.Color;
import java.awt.Font;
import javax.swing.ButtonGroup;
/**
*
* @author 1BestCsharp
*/
public class Java_RadioButton extends javax.swing.JFrame {
/**
* Creates new form Java_RadioButton
*/
public Java_RadioButton() {
initComponents();
ButtonGroup BtnGr = new ButtonGroup();
BtnGr.add(RDBTN1);
BtnGr.add(RDBTN2);
BtnGr.add(RDBTN3);
BtnGr.add(RDBTN4);
RDBTN1.setSelected(true);
RDBTN1ActionPerformed(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
RDBTN2 = new javax.swing.JRadioButton();
RDBTN1 = new javax.swing.JRadioButton();
RDBTN3 = new javax.swing.JRadioButton();
RDBTN4 = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
RDBTN2.setText("Green");
RDBTN2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RDBTN2ActionPerformed(evt);
}
});
RDBTN1.setText("Red");
RDBTN1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RDBTN1ActionPerformed(evt);
}
});
RDBTN3.setText("Blue");
RDBTN3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RDBTN3ActionPerformed(evt);
}
});
RDBTN4.setText("Orange");
RDBTN4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RDBTN4ActionPerformed(evt);
}
});
jLabel1.setText("//////////////////");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(RDBTN3)
.addComponent(RDBTN1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(RDBTN4, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(RDBTN2, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(62, 62, 62))
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(108, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RDBTN2)
.addComponent(RDBTN1))
.addGap(47, 47, 47)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RDBTN3)
.addComponent(RDBTN4))
.addGap(43, 43, 43))
);
pack();
}// </editor-fold>
private void RDBTN1ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.red);
jLabel1.setFont(new Font("Tahoma",Font.BOLD,15));
}
private void RDBTN2ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.GREEN);
jLabel1.setFont(new Font("Tahoma",Font.ITALIC,20));
}
private void RDBTN3ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.BLUE);
jLabel1.setFont(new Font("Tahoma",Font.BOLD,25));
}
private void RDBTN4ActionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setForeground(Color.ORANGE);
jLabel1.setFont(new Font("Tahoma",Font.ITALIC,30));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Java_RadioButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Java_RadioButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Java_RadioButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Java_RadioButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Java_RadioButton().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JRadioButton RDBTN1;
private javax.swing.JRadioButton RDBTN2;
private javax.swing.JRadioButton RDBTN3;
private javax.swing.JRadioButton RDBTN4;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
// OUTPUT :