Wednesday 15 April 2015

Filled Under:

Tutorial On Simple JList With Scroll Pane In Java

J List is GUI component in java which is used to display and store elements in a graphical way it store elements as a object so if want to store elements in data types first we want to convert it into desired data type as we done in our next examples.In this tutorial we have add jlist in scroll pane because if we don't  use it size changes according to number of elements in it and  design of program will change.

CONSTRUCTORS OF JLIST IN JAVA:

JList(ListModel)

This constructor creates a jlist with specified elements/Non null elements.

JList(Object[])

This constructor is used to create jlist with a specified array.

JList(Vector)

This constructor can only create a jlist with read only model for the given vector.

JList()

This constructor creates a empty jlist.

CODING:

import javax.swing.*;
import java.awt.*;


class myfram extends JFrame 
{
myfram()
{
setTitle("Simple Jlist");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] names = {"Item 1A","Item 1B","Item 1C","Item 1D","Item 1E","Item 1F","Item 1G"};
JList l1 = new JList(names);
JScrollPane lS = new JScrollPane(l1);
lS.setPreferredSize(new Dimension(300,100));
add(lS);
setSize(700,500);
setVisible(true);
}
}
public class simplelist
{

public static void main(String[] args) {

myfram fr = new myfram();
}
}

OUTPUT:


0 comments:

Post a Comment