Java Reference
In-Depth Information
JScrollPane scrollPane1 = new JScrollPane(jlist1);
frame.add(scrollPane1, BorderLayout.NORTH);
JList jlist2 = new JList(model);
jlist2.setVisibleRowCount(4);
jlist2.setFixedCellHeight(12);
jlist2.setFixedCellWidth(200);
JScrollPane scrollPane2 = new JScrollPane(jlist2);
frame.add(scrollPane2, BorderLayout.CENTER);
JList jlist3 = new JList(labels);
jlist3.setVisibleRowCount(4);
frame.add(jlist3, BorderLayout.SOUTH);
frame.setSize(300, 350);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
In addition to placing a JList within a JScrollPane , you can also find out which choices
are visible or request that a specific element be made visible. The firstVisibleIndex and
lastVisibleIndex properties allow you to find out which choices are currently visible within
the JScrollPane . Both methods return -1 if nothing is visible; this usually happens where the
data model is empty. To request that a specific element be made visible, use the public void
ensureIndexIsVisible(int index) method. For instance, to programmatically move the list to
the top, use the following:
jlist.ensureIndexIsVisible(0);
Rendering JList Elements
Every element within the JList is called a cell . Every JList has an installed cell renderer that draws
every cell when the list needs to be drawn. The default renderer, DefaultListCellRenderer , is a
subclass of JLabel , which means you can use either text or an icon as the graphical depiction
for the cell. This tends to suit most users' needs, but sometimes the cell's appearance can
benefit from some customization. And, because every JList can have at most one renderer
installed, customization requires that you replace the existing renderer.
ListCellRenderer Interface and DefaultListCellRenderer Class
The JList has an installed renderer. A class that implements the ListCellRenderer interface
provides this renderer.
public interface ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus);
}
Search WWH ::




Custom Search