Set the item dimensions using a sample value for JList in Java
Description
The following code shows how to set the item dimensions using a sample value for JList.
Example
/*from w w w . ja v a 2 s.co m*/
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Main {
public Main() {
String languages[] = { "Java", "Perl", "java2s.com", "C++", "Basic", "C#" };
JList jlst = new JList(languages);
JFrame jfrm = new JFrame("Use JList");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(200, 160);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlst.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
String protoCellValue = "My Sample Item Value";
jlst.setPrototypeCellValue(protoCellValue);
jfrm.add(new JScrollPane(jlst));
jfrm.setSize(300, 300);
jfrm.setVisible(true);
}
public static void main(String args[]) {
new Main();
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »