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