Credits

Sunday, February 14, 2010

Java's GUI List

Another interesting component in Java is a list that actually displays n items.  Unlike a combo box, a scroll pane can be integrated with a list and that one or multiple items can be selected. 


To define a list, use JList  class.  It can have any of the following constructors:

JList() - creates an empty list;

JList(Object [])- creates an array object;

JList  (Vector) - creates a vector object in a list.

Use setVisibleRowCount(parameters) to define the number of items to be displayed on the list.


[+/-] show/hide



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

public class ListGUI extends JFrame {
String[] jobs={"Programmer","Analyst","Network Administrator","DB Administrator","System Auditor"};
JList jobList=new JList(jobs);
public ListGUI() {
super("List");
setSize(345, 120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

FlowLayout flo = new FlowLayout();
jobList.setVisibleRowCount(3);
JScrollPane scroller=new JScrollPane(jobList);

setLayout(flo);
add(scroller);
setVisible(true);
}

public static void main(String[] arguments) {
ListGUI app = new ListGUI();
}
}

AddThis