Pages

Put JRadioButton into list

Complete source code below will show you, how to put JRadioButton into list. During my try using java i found no way to put JRadioButton into JList. So i try to create it on my own. I hope you enjoy what i share with you.

******************************************************************
COMPLETE SOURCE CODE FOR : AddRadioButtonIntoList.java
******************************************************************


import javax.swing.*;

import java.awt.GridLayout;

import java.awt.Color;

public class AddRadioButtonIntoList
{
public static void main(String[]args)
{
//Create radio button using JRadioButton that will be put into list
JRadioButton radioButton1=new JRadioButton("Duck");
JRadioButton radioButton2=new JRadioButton("Sheep");
JRadioButton radioButton3=new JRadioButton("Cow");
JRadioButton radioButton4=new JRadioButton("Buffalo");
JRadioButton radioButton5=new JRadioButton("Horse");
JRadioButton radioButton6=new JRadioButton("Goat");

//Set all radio button background color to white
radioButton1.setBackground(Color.WHITE);
radioButton2.setBackground(Color.WHITE);
radioButton3.setBackground(Color.WHITE);
radioButton4.setBackground(Color.WHITE);
radioButton5.setBackground(Color.WHITE);
radioButton6.setBackground(Color.WHITE);

//Create button group for JRadioButton
ButtonGroup bg=new ButtonGroup();

//Add all created radio button into button group
bg.add(radioButton1);
bg.add(radioButton2);
bg.add(radioButton3);
bg.add(radioButton4);
bg.add(radioButton5);
bg.add(radioButton6);

//Create panel that will be use to put all radio button into it
JPanel panel=new JPanel(new GridLayout(6,1));

//Add all created radio button into panel
panel.add(radioButton1);
panel.add(radioButton2);
panel.add(radioButton3);
panel.add(radioButton4);
panel.add(radioButton5);
panel.add(radioButton6);

//Create scroll bar for created panel
//Vertical scrollbar always show
//Horizontal scrollbar never show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//Create window using JFrame with title ( Add JRadioButton into list )
JFrame frame=new JFrame("Add JRadioButton into list");

//Add created scroll bar into JFrame
frame.add(scrollBar);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible
frame.setVisible(true);
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************
Related Posts Plugin for WordPress, Blogger...