Example usage for javax.swing DefaultListModel addElement

List of usage examples for javax.swing DefaultListModel addElement

Introduction

In this page you can find the example usage for javax.swing DefaultListModel addElement.

Prototype

public void addElement(E element) 

Source Link

Document

Adds the specified component to the end of this list.

Usage

From source file:Main.java

public static void main(String[] args) {

    String[] listValues = { "Click", "To", "Add", "New", "Values" };
    DefaultListModel<String> model = new DefaultListModel<>();
    for (String s : listValues) {
        model.addElement(s);
    }//from   w w  w  .  j  ava2 s . c o  m
    JList<String> list = new JList<>(model);
    MouseListener addListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            model.addElement("aa");
        }
    };
    list.addMouseListener(addListener);
    JOptionPane.showMessageDialog(null, new JScrollPane(list));
}

From source file:ModifyModelSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);
    }/*  ww  w.  j  a v  a  2 s .  c om*/
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);
    }/*from   w w  w .j a  va 2s.  co m*/
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String[] arg) {
    DefaultListModel<String> listModel = new DefaultListModel<String>();
    for (int i = 0; i < 10; i++) {
        listModel.addElement("Item " + (i + 1));
    }/*from   w  w w.  j  av a  2 s . co m*/

    JList<String> list = new JList<String>(listModel);
    list.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isRightMouseButton(me)) {
                list.clearSelection();
            }
        }
    });

    JScrollPane listScrollPane = new JScrollPane(list);
    JFrame f = new JFrame();
    f.getContentPane().add(listScrollPane);

    f.setSize(500, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:SelectionModeDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);//w  ww  .  j  a v  a2 s.  c  o m
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    JList jlist2 = new JList(model);
    jlist2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);//from www  . java  2s.c  o  m
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    JList jlist2 = new JList(model);

    JScrollPane scrollPane2 = new JScrollPane(jlist2);
    frame.add(scrollPane2, BorderLayout.CENTER);

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

    jlist2.ensureIndexIsVisible(50);
}

From source file:Main.java

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

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);//from   w  w w.j  a  va 2s .  c om
    for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
    }
    model.trimToSize();

    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);
}

From source file:Main.java

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

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);//from   w  w  w . j a v a  2 s  . c  o  m
    for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
    }
    System.out.println(model.toArray().length);

    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);
}

From source file:Main.java

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

    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(1000);//from  w  w  w  .j  a  va2  s .  com
    for (int j = 0; j < 5; j++) {
        model.addElement(labels[j]);
    }
    System.out.println(model.toString());

    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);
}

From source file:SizingSamples.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };

    JFrame frame = new JFrame("Sizing Samples");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JList jlist1 = new JList(labels);
    jlist1.setVisibleRowCount(4);//from w  w w  .java 2 s.c  o m
    DefaultListModel model = new DefaultListModel();
    model.ensureCapacity(100);
    for (int i = 0; i < 100; i++) {
        model.addElement(Integer.toString(i));
    }
    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);
}