- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we align the JRadioButtons horizontally in Java?
A JRadioButton is a subclass of JToggleButton and it is a two-state button that can either selected or deselected. Unlike checkboxes, the radio buttons are associated with a group and only one radio button in a group can be selected and it can be implemented by using the ButtonGroup class. When the radio button in the group is selected, any other previously selected radio button in the group is deselected. We can align the radio buttons either horizontally or vertically by using BoxLayout.
Example
import java.awt.*; import javax.swing.*; public class HorizontalRadioButtonsTest extends JPanel { public HorizontalRadioButtonsTest(){ JRadioButton jrb1 = new JRadioButton(" RB1"); JRadioButton jrb2 = new JRadioButton(" RB2"); JRadioButton jrb3 = new JRadioButton(" RB3"); JRadioButton jrb4 = new JRadioButton(" RB4"); JRadioButton jrb5 = new JRadioButton(" RB5"); Box box1 = Box.createHorizontalBox(); box1.add(jrb1); box1.add(jrb2); box1.add(jrb3); box1.add(jrb4); box1.add(jrb5); JRadioButton jrb6 = new JRadioButton(" RB6"); JRadioButton jrb7 = new JRadioButton(" RB7"); JRadioButton jrb8 = new JRadioButton(" RB8"); JRadioButton jrb9 = new JRadioButton(" RB9"); JRadioButton jrb10 = new JRadioButton(" RB10"); Box box2 = Box.createHorizontalBox(); box2.add(jrb6); box2.add(jrb7); box2.add(jrb8); box2.add(jrb9); box2.add(jrb10); JRadioButton jrb11 = new JRadioButton(" RB11"); JRadioButton jrb12 = new JRadioButton(" RB12"); JRadioButton jrb13 = new JRadioButton(" RB13"); JRadioButton jrb14 = new JRadioButton(" RB14"); JRadioButton jrb15 = new JRadioButton(" RB15"); Box box3 = Box.createHorizontalBox(); box3.add(jrb11); box3.add(jrb12); box3.add(jrb13); box3.add(jrb14); box3.add(jrb15); JRadioButton jrb16 = new JRadioButton(" RB16"); JRadioButton jrb17 = new JRadioButton(" RB17"); JRadioButton jrb18 = new JRadioButton(" RB18"); JRadioButton jrb19 = new JRadioButton(" RB19"); JRadioButton jrb20 = new JRadioButton(" RB20"); Box box4 = Box.createHorizontalBox(); box4.add(jrb16); box4.add(jrb17); box4.add(jrb18); box4.add(jrb19); box4.add(jrb20); setLayout(new GridLayout(5, 1)); add(box1); add(box2); add(box3); add(box4); } public static void main(String[] args) { JFrame frame = new JFrame("HorizontalRadioButtons Test"); frame.add(new HorizontalRadioButtonsTest()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(375, 250); frame.setVisible(true); } }
Output
- Related Articles
- How to align two divs horizontally in HTML?
- How can we call the invokeLater() method in Java?\n
- How can we sort a JSONObject in Java?\n
- How can we change the JButton text dynamically in Java?\n
- How can we create a login form in Java?\n
- How can we implement auto-complete JComboBox in Java?\n
- How can we initialize a boolean array in Java?\n
- How can we implement an editable JLabel in Java?\n
- How can we implement different borders using the BorderFactory in Java?\n
- How can we get the name of the Enum constant in Java?\n
- Can we override private methods in Java\n
- How can we Implement a Queue using Stack in Java?\n
- How can we set a border to JCheckBox in Java?\n
- How can we create an unmodifiable Map in Java 9?\n
- How can we implement the paintComponent() method of a JPanel in Java?\n

Advertisements