Example usage for javax.swing JList getModel

List of usage examples for javax.swing JList getModel

Introduction

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

Prototype

public ListModel<E> getModel() 

Source Link

Document

Returns the data model that holds the list of items displayed by the JList component.

Usage

From source file:com.adobe.aem.demomachine.gui.AemDemoUtils.java

public static int getSelectedIndex(@SuppressWarnings("rawtypes") JList list, Properties defaultProperties,
        Properties personalProperties, String propertyString) {

    int index = 0;
    String defaultProperty = defaultProperties.getProperty(propertyString);
    String personalProperty = personalProperties.getProperty(propertyString);
    String actualProperty = (personalProperty != null) ? personalProperty : defaultProperty;
    for (int i = 0; i < list.getModel().getSize(); i++) {

        if (list.getModel().getElementAt(i) instanceof AemDemoProperty) {

            AemDemoProperty aemProperty = (AemDemoProperty) list.getModel().getElementAt(i);
            if (aemProperty.getValue() != null && actualProperty != null
                    && aemProperty.getValue().equals(actualProperty)) {
                return i;
            }/*from w ww .  j  a  va  2s .  c  o  m*/

        }

    }

    return index;

}

From source file:fr.duminy.jbackup.swing.SourceListTypeMapper.java

private DefaultListModel<Source> getModel(ListPanel<Source, JList<Source>> listPanel) {
    JList<Source> list = ((SourceListPanel) listPanel).list;
    return (DefaultListModel<Source>) list.getModel();
}

From source file:Main.java

@Override
public boolean importData(TransferHandler.TransferSupport support) {
    if (!this.canImport(support)) {
        return false;
    }//from w  ww  .ja v  a 2  s  .  co  m
    Transferable t = support.getTransferable();
    String data = null;
    try {
        data = (String) t.getTransferData(DataFlavor.stringFlavor);
        if (data == null) {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    JList.DropLocation dropLocation = (JList.DropLocation) support.getDropLocation();
    int dropIndex = dropLocation.getIndex();
    JList<String> targetList = (JList<String>) support.getComponent();
    DefaultListModel<String> listModel = (DefaultListModel<String>) targetList.getModel();
    if (dropLocation.isInsert()) {
        listModel.add(dropIndex, data);
    } else {
        listModel.set(dropIndex, data);
    }
    return true;
}

From source file:dnd.ListTransferHandler.java

public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
        return false;
    }/* w  ww.ja  v a  2s . c  o  m*/

    JList list = (JList) info.getComponent();
    DefaultListModel listModel = (DefaultListModel) list.getModel();
    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    int index = dl.getIndex();
    boolean insert = dl.isInsert();

    // Get the string that is being dropped.
    Transferable t = info.getTransferable();
    String data;
    try {
        data = (String) t.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
        return false;
    }

    // Perform the actual import.  
    if (insert) {
        listModel.add(index, data);
    } else {
        listModel.set(index, data);
    }
    return true;
}

From source file:io.neocdtv.simpleplayer.ui.PlaylistTransferHandler.java

protected void importString(JComponent c, String str) {
    JList target = (JList) c;
    DefaultListModel listModel = (DefaultListModel) target.getModel();
    int index = target.getSelectedIndex();

    //Prevent the user from dropping data back on itself.
    //For example, if the user is moving items #4,#5,#6 and #7 and
    //attempts to insert the items after item #5, this would
    //be problematic when removing the original items.
    //So this is not allowed.
    if (indices != null && index >= indices[0] - 1 && index <= indices[indices.length - 1]) {
        indices = null;/*from  ww  w.  j  a  v  a2s  . c  om*/
        return;
    }

    int max = listModel.getSize();
    if (index < 0) {
        index = max;
    } else {
        index++;
        if (index > max) {
            index = max;
        }
    }
    addIndex = index;
    String[] values = str.split("\n");
    addCount = values.length;
    for (String value : values) {
        listModel.add(index++, value);
    }
}

From source file:io.neocdtv.simpleplayer.ui.PlaylistTransferHandler.java

@Override
public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
        return false;
    }//from   w w  w .  j a  v  a2  s. co  m

    JList list = (JList) info.getComponent();
    DefaultListModel<PlaylistEntry> listModel = (DefaultListModel) list.getModel();
    JList.DropLocation dropLocation = (JList.DropLocation) info.getDropLocation();

    // Get the string that is being dropped.
    Transferable transferable = info.getTransferable();
    List<File> data;
    try {
        data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
        for (File file : data) {
            if (file.isDirectory()) {
                final boolean recursive = true;
                final String[] fileExtensionFilter = null;
                final List<File> listFiles = Arrays.asList(FileUtils.convertFileCollectionToFileArray(
                        FileUtils.listFiles(file, fileExtensionFilter, recursive)));
                for (File o : listFiles) {
                    listModel.addElement(buildEntry(o));
                }
            } else {
                listModel.addElement(buildEntry(file));
            }
        }
    } catch (UnsupportedFlavorException | IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
        return false;
    }
    return true;
}

From source file:de.dakror.virtualhub.client.dialog.ChooseCatalogDialog.java

public static void show(ClientFrame frame, final JSONArray data) {
    final JDialog dialog = new JDialog(frame, "Katalog whlen", true);
    dialog.setSize(400, 300);//from   w  ww. j a v  a  2 s. c  o  m
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    dialog.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            Client.currentClient.disconnect();
            System.exit(0);
        }
    });

    JPanel contentPane = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
    dialog.setContentPane(contentPane);
    DefaultListModel dlm = new DefaultListModel();
    for (int i = 0; i < data.length(); i++) {
        try {
            dlm.addElement(data.getJSONObject(i).getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    final JList catalogs = new JList(dlm);
    catalogs.setDragEnabled(false);
    catalogs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane jsp = new JScrollPane(catalogs, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jsp.setPreferredSize(new Dimension(396, 200));
    contentPane.add(jsp);

    JPanel mods = new JPanel(new GridLayout(1, 2));
    mods.setPreferredSize(new Dimension(50, 22));
    mods.add(new JButton(new AbstractAction("+") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = JOptionPane.showInputDialog(dialog,
                    "Bitte geben Sie den Namen des neuen Katalogs ein.", "Katalog hinzufgen",
                    JOptionPane.PLAIN_MESSAGE);
            if (name != null && name.length() > 0) {
                DefaultListModel dlm = (DefaultListModel) catalogs.getModel();
                for (int i = 0; i < dlm.getSize(); i++) {
                    if (dlm.get(i).toString().equals(name)) {
                        JOptionPane.showMessageDialog(dialog,
                                "Es existert bereits ein Katalog mit diesem Namen!",
                                "Katalog bereits vorhanden!", JOptionPane.ERROR_MESSAGE);
                        actionPerformed(e);
                        return;
                    }
                }

                try {
                    dlm.addElement(name);
                    JSONObject o = new JSONObject();
                    o.put("name", name);
                    o.put("sources", new JSONArray());
                    o.put("tags", new JSONArray());
                    data.put(o);
                    Client.currentClient.sendPacket(new Packet0Catalogs(data));
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }));
    mods.add(new JButton(new AbstractAction("-") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (catalogs.getSelectedIndex() != -1) {
                if (JOptionPane.showConfirmDialog(dialog,
                        "Sind Sie sicher, dass Sie diesen\r\nKatalog unwiderruflich lschen wollen?",
                        "Katalog lschen", JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                    DefaultListModel dlm = (DefaultListModel) catalogs.getModel();
                    data.remove(catalogs.getSelectedIndex());
                    dlm.remove(catalogs.getSelectedIndex());
                    try {
                        Client.currentClient.sendPacket(new Packet0Catalogs(data));
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }));

    contentPane.add(mods);

    JLabel l = new JLabel("");
    l.setPreferredSize(new Dimension(396, 14));
    contentPane.add(l);

    JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
    sep.setPreferredSize(new Dimension(396, 10));
    contentPane.add(sep);

    JPanel buttons = new JPanel(new GridLayout(1, 2));
    buttons.setPreferredSize(new Dimension(396, 22));
    buttons.add(new JButton(new AbstractAction("Abbrechen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            Client.currentClient.disconnect();
            System.exit(0);
        }
    }));
    buttons.add(new JButton(new AbstractAction("Katalog whlen") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (catalogs.getSelectedIndex() != -1) {
                try {
                    Client.currentClient
                            .setCatalog(new Catalog(data.getJSONObject(catalogs.getSelectedIndex())));
                    Client.currentClient.frame.setTitle("- " + Client.currentClient.getCatalog().getName());
                    dialog.dispose();
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }));

    dialog.add(buttons);

    dialog.setLocationRelativeTo(frame);
    dialog.setResizable(false);
    dialog.setVisible(true);
}

From source file:DropDemo.java

protected void cleanup(JComponent c, boolean remove) {
    if (remove && indices != null) {
        JList source = (JList) c;
        DefaultListModel model = (DefaultListModel) source.getModel();
        // If we are moving items around in the same list, we
        // need to adjust the indices accordingly, since those
        // after the insertion point have moved.
        if (addCount > 0) {
            for (int i = 0; i < indices.length; i++) {
                if (indices[i] > addIndex) {
                    indices[i] += addCount;
                }/*from   ww w.j  ava2s  . c o m*/
            }
        }
        for (int i = indices.length - 1; i >= 0; i--) {
            model.remove(indices[i]);
        }
    }
    indices = null;
    addCount = 0;
    addIndex = -1;
}

From source file:Main.java

@Override
protected void exportDone(JComponent source, Transferable data, int action) {
    @SuppressWarnings("unchecked")
    JList<String> sourceList = (JList<String>) source;
    String movedItem = sourceList.getSelectedValue();
    if (action == TransferHandler.MOVE) {
        DefaultListModel<String> listModel = (DefaultListModel<String>) sourceList.getModel();
        listModel.removeElement(movedItem);
    }//from  w ww  .  j  ava  2s . c o m
}

From source file:DropDemo.java

protected void importString(JComponent c, String str) {
    JList target = (JList) c;
    DefaultListModel listModel = (DefaultListModel) target.getModel();
    int index = target.getSelectedIndex();

    // Prevent the user from dropping data back on itself.
    // For example, if the user is moving items #4,#5,#6 and #7 and
    // attempts to insert the items after item #5, this would
    // be problematic when removing the original items.
    // So this is not allowed.
    if (indices != null && index >= indices[0] - 1 && index <= indices[indices.length - 1]) {
        indices = null;/*from   w ww. ja  va 2s.c  o m*/
        return;
    }

    int max = listModel.getSize();
    if (index < 0) {
        index = max;
    } else {
        index++;
        if (index > max) {
            index = max;
        }
    }
    addIndex = index;
    String[] values = str.split("\n");
    addCount = values.length;
    for (int i = 0; i < values.length; i++) {
        listModel.add(index++, values[i]);
    }
}