import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/** JList example illustrating
*
* - The creation of a JList by creating a DefaultListModel,
* adding the values there, then passing that to the JList
* constructor.
*
- Adding new values at run-time, the key thing that
* DefaultListModel lets you do that you can't do with
* a JList where you supply values directly.
*
* 1998-99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class DefaultListModelExample extends JFrame {
public static void main(String[] args) {
new DefaultListModelExample();
}
JList sampleJList;
private DefaultListModel sampleModel;
public DefaultListModelExample() {
super("Creating a Simple JList");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
String[] entries = { "Entry 1", "Entry 2", "Entry 3",
"Entry 4", "Entry 5", "Entry 6" };
sampleModel = new DefaultListModel();
for(int i=0; ibefore trying to scroll
* to make the index visible.
*/
public void actionPerformed(ActionEvent event) {
int index = sampleModel.getSize();
sampleModel.addElement("Entry " + (index+1));
getContentPane().invalidate();
getContentPane().validate();
sampleJList.setSelectedIndex(index);
sampleJList.ensureIndexIsVisible(index);
}
}
}