Java Reference
In-Depth Information
Figure 13-5. Sizing entries within a JList
The source used to generate the output in Figure 13-5 follows in Listing 13-3. The center
list in the figure contains more than 1,000 fixed-size cells. The top list shows that you can set
the number of visible rows with setVisibleRowCount() . Notice that the bottom list in the figure
also uses setVisibleRowCount() . However, because the list isn't in a JScrollPane , the request to
limit the number of rows is ignored.
Listing 13-3. Sizing the List Cells
import javax.swing.*;
import java.awt.*;
public class SizingSamples {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
String labels[] = {"Chardonnay", "Sauvignon", "Riesling", "Cabernet",
"Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
"Gewürztraminer"};
JFrame frame = new JFrame("Sizing Samples");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist1 = new JList(labels);
jlist1.setVisibleRowCount(4);
DefaultListModel model = new DefaultListModel();
model.ensureCapacity(1000);
for (int i=0;i<100;i++) {
for (int j=0;j<10;j++) {
model.addElement(labels[j]);
}
}
 
Search WWH ::




Custom Search