DefaultListModel: addElement(Object obj) : DefaultListModel « javax.swing « Java by API






DefaultListModel: addElement(Object obj)

 
import java.awt.BorderLayout;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class MainClass {

  public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
      }
    }

    JList jlist2 = new JList(model);
    jlist2.setVisibleRowCount(4);
    jlist2.setFixedCellHeight(12);
    jlist2.setFixedCellWidth(200);
    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

    frame.setSize(300, 350);
    frame.setVisible(true);
  }
}
           
         
  








Related examples in the same category

1.DefaultListModel: add(int index, Object element)
2.DefaultListModel: clear()
3.DefaultListModel: getSize()
4.DefaultListModel: insertElementAt(Object obj, int index)
5.DefaultListModel: remove(int index)
6.DefaultListModel: removeAllElements()
7.DefaultListModel: removeElementAt(int index)
8.DefaultListModel: removeRange(int fromIndex, int toIndex)
9.DefaultListModel: set(int index, Object element)
10.DefaultListModel: setElementAt(Object obj, int index)
11.JList: setFixedCellWidth(int width)
12.DefaultListModel: ensureCapacity(int count)