Example usage for javax.swing DefaultListModel DefaultListModel

List of usage examples for javax.swing DefaultListModel DefaultListModel

Introduction

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

Prototype

DefaultListModel

Source Link

Usage

From source file:Main.java

public Main() {
    JPanel panel = new JPanel();
    panel.setLayout(null);// w ww.  j a v a 2  s .co  m

    model = new DefaultListModel();
    list = new JList(model);
    list.setBounds(150, 30, 220, 150);

    JButton okButton = new JButton("Ok");
    okButton.setBounds(30, 35, 80, 25);

    okButton.addActionListener(this);

    panel.add(okButton);
    panel.add(list);
    add(panel);

    setSize(420, 250);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;//from  w w  w.  ja v  a  2 s. c o  m
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:ListModelExample.java

public ListModelExample() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    for (int i = 0; i < 15; i++)
        model.addElement("Element " + i);

    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addElement("Element " + counter);
            counter++;/*from  w w  w. j a v  a 2 s. c  o m*/
        }
    });
    removeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (model.getSize() > 0)
                model.removeElementAt(0);
        }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
}

From source file:DragDropList.java

public DragDropList() {
    super(new DefaultListModel());
    model = (DefaultListModel) getModel();
    setDragEnabled(true);//from ww  w.j  av a  2  s  . c  om
    setDropMode(DropMode.INSERT);

    setTransferHandler(new MyListDropHandler(this));

    new MyDragListener(this);

    model.addElement("a");
    model.addElement("b");
    model.addElement("c");
}

From source file:net.sf.maltcms.chromaui.charts.GradientPaintScale.java

/**
 *
 * @param args//from w  w w.j  a v a 2s .  co  m
 */
public static void main(String[] args) {
    double[] st = ImageTools.createSampleTable(256);
    Logger.getLogger(GradientPaintScale.class.getName()).info(Arrays.toString(st));
    double min = 564.648;
    double max = 24334.234;
    GradientPaintScale gps = new GradientPaintScale(st, min, max,
            new Color[] { Color.BLACK, Color.RED, Color.orange, Color.yellow, Color.white });
    double val = min;
    double incr = (max - min) / (st.length - 1);
    Logger.getLogger(GradientPaintScale.class.getName()).log(Level.INFO, "Increment: {0}", incr);
    for (int i = 0; i < st.length; i++) {
        Logger.getLogger(GradientPaintScale.class.getName()).log(Level.INFO, "Value: {0}", val);
        gps.getPaint(val);
        val += incr;
    }
    Logger.getLogger(GradientPaintScale.class.getName()).info("Printing min and max values");
    Logger.getLogger(GradientPaintScale.class.getName()).log(Level.INFO, "Min: {0} gps min: {1}",
            new Object[] { min, gps.getPaint(min) });
    Logger.getLogger(GradientPaintScale.class.getName()).log(Level.INFO, "Max: {0} gps max: {1}",
            new Object[] { max, gps.getPaint(max) });
    JList jl = new JList();
    DefaultListModel dlm = new DefaultListModel();
    jl.setModel(dlm);
    jl.setCellRenderer(new ListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            if (value instanceof JLabel) {
                // Border b =
                // BorderFactory.createCompoundBorder(BorderFactory
                // .createEmptyBorder(0, 0, 5, 0), BorderFactory
                // .createLineBorder(Color.BLACK, 1));
                // ((JLabel) value).setBorder(b);
                return (Component) value;
            }
            return new JLabel(value.toString());
        }
    });
    JFrame jf = new JFrame();
    jf.add(new JScrollPane(jl));
    jf.setVisible(true);
    jf.setSize(200, 400);
    for (int alpha = -10; alpha <= 10; alpha++) {
        for (int beta = 1; beta <= 20; beta++) {
            gps.setAlphaBeta(alpha, beta);
            // System.out.println(Arrays.toString(gps.st));
            // System.out.println(Arrays.toString(gps.sampleTable));
            BufferedImage bi = gps.getLookupImage();
            ImageIcon ii = new ImageIcon(bi);
            dlm.addElement(new JLabel(ii));
        }
    }

}

From source file:filesscanner.MainWindow.java

/**
 * Creates new form MainWindow//from w w w.java 2s . c  o m
 */
public MainWindow() {
    initComponents();
    modelDirectories = new DefaultListModel<>();
    modelFiles = new DefaultListModel<>();

    jDirectoriesList.setModel(modelDirectories);
    jFileList.setModel(modelFiles);
    setTitle("ULTIMATE FILE UPLOAD");
}

From source file:com.a544jh.kanamemory.ui.ProfileChooserPanel.java

public void populateList() {
    listmodel = new DefaultListModel<>();
    ArrayList<String> names;
    try {/*w  w  w. ja va  2s  .c o  m*/
        names = JsonFileReader.ProfilesList("profiles");
    } catch (FileNotFoundException ex) {
        System.out.println("Profiles file not found. A new one will be created.");
        names = new ArrayList<>();
    } catch (JSONException ex) {
        JOptionPane.showMessageDialog(this, ex.getMessage() + "\nFix or delete the profiles file.",
                "Profiles file malformed", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
        names = new ArrayList<>();
    }
    for (String string : names) {
        listmodel.addElement(string);
    }
    profilesList.setModel(listmodel);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());

    listModel = new DefaultListModel();
    listModel.addElement("Debbie Scott");
    listModel.addElement("Scott Hommel");
    listModel.addElement("Sharon Zakhour");

    // Create the list and put it in a scroll pane.
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);//w  w w.  j  a  v a2s  .co m
    list.addListSelectionListener(this);
    list.setVisibleRowCount(5);
    JScrollPane listScrollPane = new JScrollPane(list);

    JButton hireButton = new JButton(hireString);
    HireListener hireListener = new HireListener(hireButton);
    hireButton.setActionCommand(hireString);
    hireButton.addActionListener(hireListener);
    hireButton.setEnabled(false);

    fireButton = new JButton(fireString);
    fireButton.setActionCommand(fireString);
    fireButton.addActionListener(new FireListener());

    employeeName = new JTextField(10);
    employeeName.addActionListener(hireListener);
    employeeName.getDocument().addDocumentListener(hireListener);
    String name = listModel.getElementAt(list.getSelectedIndex()).toString();

    // Create a panel that uses BoxLayout.
    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
    buttonPane.add(fireButton);
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
    buttonPane.add(Box.createHorizontalStrut(5));
    buttonPane.add(employeeName);
    buttonPane.add(hireButton);
    buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    add(listScrollPane, BorderLayout.CENTER);
    add(buttonPane, BorderLayout.PAGE_END);
}

From source file:brainflow.core.ImageBrowser.java

private void initSourceView() {
    sourceView = new JList();
    final DefaultListModel model = new DefaultListModel();
    for (IImageSource source : sourceList.sourceList) {
        model.addElement(source);//from w  w  w.ja va 2  s.co m
    }

    sourceView.setModel(model);
    sourceView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    ButtonPanel panel = new ButtonPanel(SwingConstants.CENTER);

    JButton nextButton = new JButton("Next");
    ImageIcon icon = new ImageIcon(getClass().getClassLoader().getResource("icons/control_play_blue.png"));
    nextButton.setIcon(icon);

    JButton prevButton = new JButton("Previous");
    icon = new ImageIcon(getClass().getClassLoader().getResource("icons/control_rev_blue.png"));
    prevButton.setIcon(icon);
    panel.addButton(prevButton);
    panel.addButton(nextButton);
    panel.setSizeConstraint(ButtonPanel.SAME_SIZE);

    JPanel container = new JPanel(new BorderLayout());
    container.setBorder(new TitledBorder("Image List"));
    container.add(new JScrollPane(sourceView), BorderLayout.CENTER);
    container.add(panel, BorderLayout.SOUTH);
    add(container, BorderLayout.WEST);

    nextButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int index = sourceView.getSelectedIndex();
            if (index == (sourceList.size() - 1)) {
                index = 0;
            } else {
                index++;
            }

            updateView(index);

        }
    });

    sourceView.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int index = sourceView.getSelectedIndex();
            if (currentModel.getSelectedLayer().getDataSource() != sourceView.getSelectedValue()) {
                System.out.println("updating view");
                updateView(index);
            } else {
                System.out.println("not updating view ");
            }

        }
    });
}

From source file:interfaceTisseoWS.ST4.java

public final void init() throws ParseException, IOException, URISyntaxException {
    RequestJCDecaux r = new RequestJCDecaux();
    JSONParser parser = new JSONParser();

    r.setPathURIB("/vls/v1/stations");

    Object obj = parser.parse(r.request());
    JSONArray array = (JSONArray) obj;//from ww  w .j  a v a 2 s  .  co  m

    int nbStations = array.size();
    DefaultListModel lm = new DefaultListModel();
    for (int i = 0; i < nbStations; i++) {
        stations.add(new Station((JSONObject) array.get(i)));
        lm.addElement(stations.get(i).getName());
    }
    listeStations.setModel(lm);
}