Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Put JLabel into list

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

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


import javax.swing.*;

import java.awt.GridLayout;

public class AddJLabelIntoList
{
public static void main(String[]args)
{
//Create label using JLabel that will be put into list
JLabel label1=new JLabel("Duck");
JLabel label2=new JLabel("Sheep");
JLabel label3=new JLabel("Cow");
JLabel label4=new JLabel("Buffalo");
JLabel label5=new JLabel("Horse");
JLabel label6=new JLabel("Goat");

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

//Add all created label into panel
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(label6);

//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 JLabel into list )
JFrame frame=new JFrame("Add JLabel 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
************************************************************************

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
******************************************************************

Create list using JList

Source code below will show you, how to create list in java using JList.

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


import javax.swing.JList;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class CreateListUsingJList
{
public static void main(String[]args)
{
//Create contents for JList using array
//Using String as array type
String[]listContents={"HI","HA","HU"};

//Create a list using JList
JList list=new JList(listContents);

//Create a JFrame that will be use to put JList into it
JFrame frame=new JFrame("Create list using JList");

//set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//add created JList into JFrame
frame.add(list);

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

//set JFrame size
frame.setSize(400,400);

//make JFrame visible. So we can see it
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

Create list in java

In java, you can create list using JList class.

RELAXING NATURE VIDEO