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:io.github.jeremgamer.editor.panels.Panels.java

public Panels(final JFrame frame, final PanelsPanel pp) {
    this.setBorder(BorderFactory.createTitledBorder(""));

    JButton add = null;/*from   w  ww  .  jav a 2 s  .co m*/
    try {
        add = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                JOptionPane jop = new JOptionPane();
                @SuppressWarnings("static-access")
                String name = jop.showInputDialog((JFrame) SwingUtilities.windowForComponent(panelList),
                        "Nommez le panneau :", "Crer un panneau", JOptionPane.QUESTION_MESSAGE);

                if (name != null) {
                    for (int i = 0; i < data.getSize(); i++) {
                        if (data.get(i).equals(name)) {
                            name += "1";
                        }
                    }
                    data.addElement(name);
                    new PanelSave(name);
                    PanelsPanel.updateLists();
                    mainPanel.addItem(name);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });

    JButton remove = null;
    try {
        remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    remove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                if (panelList.getSelectedValue() != null) {
                    File file = new File("projects/" + Editor.getProjectName() + "/panels/"
                            + panelList.getSelectedValue() + "/" + panelList.getSelectedValue() + ".rbd");
                    JOptionPane jop = new JOptionPane();
                    @SuppressWarnings("static-access")
                    int option = jop.showConfirmDialog((JFrame) SwingUtilities.windowForComponent(panelList),
                            "tes-vous sr de vouloir supprimer ce panneau?", "Avertissement",
                            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

                    if (option == JOptionPane.OK_OPTION) {
                        if (panelList.getSelectedValue().equals(pp.getFileName())) {
                            pp.setFileName("");
                        }
                        pp.hide();
                        file.delete();
                        File parent = new File(file.getParent());
                        for (File img : FileUtils.listFiles(parent, TrueFileFilter.INSTANCE,
                                TrueFileFilter.INSTANCE)) {
                            img.delete();
                        }
                        parent.delete();
                        mainPanel.removeItem(panelList.getSelectedValue());
                        data.remove(panelList.getSelectedIndex());
                        ActionPanel.updateLists();
                        PanelsPanel.updateLists();
                    }
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }

        }

    });

    JPanel buttons = new JPanel();
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
    buttons.add(add);
    buttons.add(remove);

    mainPanel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            @SuppressWarnings("unchecked")
            JComboBox<String> combo = (JComboBox<String>) event.getSource();
            GeneralSave gs = new GeneralSave();
            try {
                gs.load(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
                gs.set("mainPanel", combo.getSelectedItem());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    });

    updateList();
    panelList.addMouseListener(new MouseAdapter() {
        @SuppressWarnings("unchecked")
        public void mouseClicked(MouseEvent evt) {
            JList<String> list = (JList<String>) evt.getSource();
            if (evt.getClickCount() == 2) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    pp.load(new File("projects/" + Editor.getProjectName() + "/panels/"
                            + list.getModel().getElementAt(index) + "/" + list.getModel().getElementAt(index)
                            + ".rbd"));
                    PanelsPanel.updateLists();
                    pp.show();
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            pp.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            pp.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            pp.load(new File("projects/" + Editor.getProjectName() + "/panels/"
                                    + list.getModel().getElementAt(index) + "/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                            PanelsPanel.updateLists();
                        }
                    } catch (NullPointerException npe) {
                        pp.hide();
                        list.clearSelection();
                    }
                }
            } else if (evt.getClickCount() == 3) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    pp.load(new File("projects/" + Editor.getProjectName() + "/panels/"
                            + list.getModel().getElementAt(index) + "/" + list.getModel().getElementAt(index)
                            + ".rbd"));
                    PanelsPanel.updateLists();
                    pp.show();
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            pp.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            pp.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            pp.load(new File("projects/" + Editor.getProjectName() + "/panels/"
                                    + list.getModel().getElementAt(index) + "/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                            PanelsPanel.updateLists();
                        }
                    } catch (NullPointerException npe) {
                        pp.hide();
                        list.clearSelection();
                    }
                }
            }
        }
    });
    JScrollPane listPane = new JScrollPane(panelList);
    listPane.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED);
    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.add(buttons);
    this.add(listPane);

    JPanel mainPanelInput = new JPanel();
    mainPanelInput.setPreferredSize(new Dimension(280, 35));
    mainPanelInput.setMaximumSize(new Dimension(280, 35));
    mainPanelInput.add(new JLabel("Panneau principal:"));
    mainPanel.setPreferredSize(new Dimension(150, 27));
    mainPanelInput.add(mainPanel);
    this.add(mainPanelInput);
}

From source file:com.raddle.tools.MergeMain.java

private void selectLine(JList list, PropertyLine line) {
    DefaultComboBoxModel model = (DefaultComboBoxModel) list.getModel();
    for (int i = 0; i < model.getSize(); i++) {
        PropertyLine mLine = (PropertyLine) model.getElementAt(i);
        if (mLine.getKey().equals(line.getKey())) {
            list.setSelectedValue(mLine, true);
            return;
        }//from   w  ww. j a  v a 2 s.c  o m
    }
}

From source file:io.github.jeremgamer.editor.panels.Others.java

public Others(final JFrame frame, final OtherPanel op, final PanelSave ps) {
    this.frame = frame;

    this.setBorder(BorderFactory.createTitledBorder(""));
    JButton add = null;/*from w ww  .j  av a  2  s .co m*/
    try {
        add = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("add.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                JOptionPane jop = new JOptionPane();
                @SuppressWarnings("static-access")
                String name = jop.showInputDialog((JFrame) SwingUtilities.windowForComponent(otherList),
                        "Nommez le composant :", "Crer un composant", JOptionPane.QUESTION_MESSAGE);

                if (name != null) {
                    for (int i = 0; i < data.getSize(); i++) {
                        if (data.get(i).equals(name)) {
                            name += "1";
                        }
                    }
                    data.addElement(name);
                    new OtherSave(name);
                    ActionPanel.updateLists();
                    PanelsPanel.updateLists();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    });

    JButton remove = null;
    try {
        remove = new JButton(new ImageIcon(ImageIO.read(ImageGetter.class.getResource("remove.png"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    remove.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                if (otherList.getSelectedValue() != null) {
                    File file = new File("projects/" + Editor.getProjectName() + "/others/"
                            + otherList.getSelectedValue() + ".rbd");
                    JOptionPane jop = new JOptionPane();
                    @SuppressWarnings("static-access")
                    int option = jop.showConfirmDialog((JFrame) SwingUtilities.windowForComponent(otherList),
                            "tes-vous sr de vouloir supprimer ce composant?", "Avertissement",
                            JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

                    if (option == JOptionPane.OK_OPTION) {
                        File dir = new File("projects/" + Editor.getProjectName() + "/panels");
                        for (File f : FileUtils.listFilesAndDirs(dir, TrueFileFilter.INSTANCE,
                                TrueFileFilter.INSTANCE)) {
                            if (!f.isDirectory()) {
                                try {
                                    ps.load(f);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                                OtherSave os = new OtherSave();
                                try {
                                    os.load(file);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                                String type = null;
                                switch (os.getInt("type")) {
                                case 0:
                                    type = "Zone de saisie";
                                    break;
                                case 1:
                                    type = "Zone de saisie de mot de passe";
                                    break;
                                case 2:
                                    type = "Zone de saisie (Grande)";
                                    break;
                                case 3:
                                    type = "Case  cocher";
                                    break;
                                case 4:
                                    type = "Menu droulant";
                                    break;
                                case 5:
                                    type = "Barre de progression";
                                    break;
                                case 6:
                                    type = "Slider";
                                    break;
                                case 7:
                                    type = "Spinner";
                                    break;
                                }
                                for (String section : ps.getSectionsContaining(
                                        otherList.getSelectedValue() + " (" + type + ")")) {
                                    ps.removeSection(section);
                                    try {
                                        ps.save(f);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        if (otherList.getSelectedValue().equals(op.getFileName())) {
                            op.setFileName("");
                        }
                        op.hide();
                        file.delete();
                        data.remove(otherList.getSelectedIndex());
                        ActionPanel.updateLists();
                        OtherPanel.updateLists();
                        PanelsPanel.updateLists();
                    }
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
            }

        }

    });

    JPanel buttons = new JPanel();
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
    buttons.add(add);
    buttons.add(remove);

    updateList();
    otherList.addMouseListener(new MouseAdapter() {
        @SuppressWarnings("unchecked")
        public void mouseClicked(MouseEvent evt) {
            JList<String> list = (JList<String>) evt.getSource();
            if (evt.getClickCount() == 2) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    op.show();
                    op.load(new File("projects/" + Editor.getProjectName() + "/others/"
                            + list.getModel().getElementAt(index) + ".rbd"));
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            op.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            op.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            op.load(new File("projects/" + Editor.getProjectName() + "/others/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                        }
                    } catch (NullPointerException npe) {
                        op.hide();
                        list.clearSelection();
                    }
                }
            } else if (evt.getClickCount() == 3) {
                int index = list.locationToIndex(evt.getPoint());
                if (isOpen == false) {
                    op.show();
                    op.load(new File("projects/" + Editor.getProjectName() + "/others/"
                            + list.getModel().getElementAt(index) + ".rbd"));
                    previousSelection = list.getSelectedValue();
                    isOpen = true;
                } else {
                    try {
                        if (previousSelection.equals(list.getModel().getElementAt(index))) {
                            op.hide();
                            previousSelection = list.getSelectedValue();
                            list.clearSelection();
                            isOpen = false;
                        } else {
                            op.hideThenShow();
                            previousSelection = list.getSelectedValue();
                            op.load(new File("projects/" + Editor.getProjectName() + "/others/"
                                    + list.getModel().getElementAt(index) + ".rbd"));
                        }
                    } catch (NullPointerException npe) {
                        op.hide();
                        list.clearSelection();
                    }
                }
            }
        }
    });
    JScrollPane listPane = new JScrollPane(otherList);
    listPane.getVerticalScrollBar().setUnitIncrement(Editor.SCROLL_SPEED);
    this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    this.add(buttons);
    this.add(listPane);
    OtherPanel.updateLists();
}

From source file:edu.ku.brc.specify.tasks.subpane.VisualQueryPanel.java

/**
 * Moves selected items from one list to the other.
 * @param srcList//from www  .j ava 2 s .  c  o m
 * @param srcHash
 * @param dstList
 * @param dstHash
 */
private void moveItems(final JList srcList, final HashSet<Integer> srcHash, final JList dstList,
        final HashSet<Integer> dstHash) {
    int inx = srcList.getSelectedIndex();
    if (inx > -1) {
        DefaultListModel srcModel = (DefaultListModel) srcList.getModel();
        DefaultListModel dstModel = (DefaultListModel) dstList.getModel();

        int[] indexes = srcList.getSelectedIndices();
        ArrayList<LatLonPoint> llpList = new ArrayList<LatLonPoint>(indexes.length);
        for (int selInx : indexes) {
            LatLonPoint llp = (LatLonPoint) srcModel.get(selInx);
            llpList.add(llp);

            if (!dstHash.contains(llp.getLocId())) {
                dstModel.addElement(llp);
                dstHash.add(llp.getLocId());
            }
        }

        for (LatLonPoint llp : llpList) {
            srcModel.removeElement(llp);
            srcHash.remove(llp.getLocId());
        }
    }
}

From source file:cpsd.ImageGUI.java

private void processActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processActionPerformed
    //rgb2Gray();
    try {/*from   w  w  w  . j  a  v  a  2s .c  o  m*/
        for (int k = 0; k < 7; k++)
            listModel.addElement(str[k]);
    } catch (NullPointerException e) {
        System.err.println("..........Please load a valid image..........");
    }
    JList list = new JList(listModel);
    int i = 0;
    for (int j = listModel.getSize(); j > 0; j--, i++) {
        System.out.println(list.getModel().getElementAt(i).toString());
        switch (list.getModel().getElementAt(i).toString()) {

        case "enhance contrast": {
            enhanceContrast();
            break;
        }
        case "enhance brightness": {
            enhanceBrightness(0.9);
            break;
        }
        case "enhance sharpness": {
            enhanceSharpness(1.5);
            break;
        }
        case "blur": {
            blurImage();
            break;
        }
        case "default":
            break;
        }
    }
    listModel.removeAllElements();
    displayImage();
    // TODO add your handling code here:
}

From source file:net.sourceforge.doddle_owl.ui.InputDocumentSelectionPanel.java

private void addDocuments(JList list, Set fileSet) {
    DefaultListModel model = (DefaultListModel) list.getModel();
    for (Iterator i = fileSet.iterator(); i.hasNext();) {
        File file = (File) i.next();
        Document doc = new Document(file);
        String text = doc.getText();
        if (30 < text.split(" ").length) { // ????????
            doc.setLang("en");
        }/* w w  w.ja  v a 2 s .  c o  m*/
        model.addElement(doc);
    }
}

From source file:com.mirth.connect.client.ui.browsers.message.MessageBrowserAdvancedFilter.java

private void shiftValues(JList source, JList destination) {
    Object[] values = source.getSelectedValues();
    DefaultListModel sourceModel = (DefaultListModel) source.getModel();
    DefaultListModel destinationModel = (DefaultListModel) destination.getModel();

    for (Object value : values) {
        sourceModel.removeElement(value);
        destinationModel.addElement(value);
    }/* w ww  . j a v a  2 s  . c o  m*/
}

From source file:edu.ku.brc.specify.config.init.secwiz.UserPanel.java

/**
 * @param doGainAccess//from  w  ww  .ja  v  a  2  s  . co m
 */
private void changeMasterAccess(final boolean doGainAccess) {
    String msg = UIRegistry.getResourceString(doGainAccess ? "DO_GAIN_ACCESS" : "DO_LOOSE_ACCESS");
    if (UIRegistry.askYesNoLocalized("YES", "NO", msg, "WARNING") == JOptionPane.NO_OPTION) {
        return;
    }

    DBMSUserMgr mgr = DBMSUserMgr.getInstance();

    String dbUserName = properties.getProperty("dbUserName");
    String dbPassword = properties.getProperty("dbPassword");
    String saUserName = properties.getProperty("saUserName");
    String hostName = properties.getProperty("hostName");

    String dbName = doGainAccess ? otherDBName : databaseName;

    if (mgr.getConnection() == null) {
        if (!mgr.connectToDBMS(dbUserName, dbPassword, hostName)) {
            UIRegistry.showError("Unable to login.");
            return;
        }
    }

    ArrayList<String> changedNames = new ArrayList<String>();
    ArrayList<String> noChangeNames = new ArrayList<String>();

    JList list = doGainAccess ? otherDBList : dbList;
    int[] inxs = list.getSelectedIndices();
    for (int inx : inxs) {
        String dbNm = (String) list.getModel().getElementAt(inx);
        if (mgr.setPermissions(saUserName, dbNm,
                doGainAccess ? DBMSUserMgr.PERM_ALL_BASIC : DBMSUserMgr.PERM_NONE)) {
            changedNames.add(dbNm);
        } else {
            noChangeNames.add(dbNm);
        }
    }

    for (String nm : changedNames) {
        if (doGainAccess) {
            ((DefaultListModel) otherDBList.getModel()).removeElement(nm);
            ((DefaultListModel) dbList.getModel()).addElement(nm);
        } else {
            ((DefaultListModel) otherDBList.getModel()).addElement(nm);
            ((DefaultListModel) dbList.getModel()).removeElement(nm);
        }
    }

    if (inxs.length == 1) {
        UIRegistry.showLocalizedMsg(JOptionPane.INFORMATION_MESSAGE, "MSTR_PERM_CHGED_TITLE",
                doGainAccess ? "MSTR_PERM_ADDED" : "MSTR_PERM_DEL", saUserName, dbName);
        final int selInx = inxs[0];
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                dbList.setSelectedIndex(selInx);
            }
        });
    } else {
        UIRegistry.showLocalizedMsg(JOptionPane.INFORMATION_MESSAGE, "MSTR_PERM_CHGED_TITLE",
                doGainAccess ? "MSTR_NUM_PERM_ADDED" : "MSTR_NUM_PERM_DEL", saUserName, changedNames.size());
    }

    mgr.close();
}

From source file:es.emergya.ui.plugins.admin.aux1.SummaryAction.java

private JPanel buildBotones(final Dimension dimension2, final JList left, final JList right) {

    JPanel botones = new JPanel(new GridBagLayout());
    botones.setPreferredSize(dimension2);
    JButton derecha = new JButton(LogicConstants.getIcon("button_right"));
    derecha.setBorderPainted(false);/*from w  w w .j  a v a2  s  . com*/
    derecha.setOpaque(false);
    derecha.setContentAreaFilled(false);
    derecha.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            cambios = true;
            for (Object o : left.getSelectedValues()) {
                ((DefaultListModel) right.getModel()).addElement(o);
                rightItems.add(o);
            }
            for (Object o : left.getSelectedValues()) {
                ((DefaultListModel) left.getModel()).removeElement(o);
                leftItems.remove(o);
            }
        }
    });
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridy = 0;
    botones.add(derecha, gbc);
    izquierda.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            cambios = true;
            for (Object o : right.getSelectedValues()) {
                ((DefaultListModel) left.getModel()).addElement(o);
                leftItems.add(o);
            }
            for (Object o : right.getSelectedValues()) {
                ((DefaultListModel) right.getModel()).removeElement(o);
                rightItems.remove(o);
            }
        }
    });
    gbc.gridy++;
    izquierda.setBorderPainted(false);
    izquierda.setOpaque(false);
    izquierda.setContentAreaFilled(false);
    botones.add(izquierda, gbc);
    botones.setOpaque(false);
    return botones;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.DataImportDialog.java

/**
 * Checks if data that is being imported will encounter issues during import,
 * due to field size constraints in teh data base.  Creates a list of errors to
 * be displayed to the user// w  w w  .ja va 2 s  . c om
 * 
 * @param headers - the column names
 * @param data - the table data
 * @return
 * boolean whether the data contains errors
 */
private boolean checkForErrors(String[] headers, String[][] data) {
    JList listOfErrors = genListOfErrorWhereTableDataDefiesSizeConstraints(headers, data);
    if (listOfErrors == null) {
        return false;
    }
    if (listOfErrors.getModel().getSize() > 0) {
        return true;
    }
    return false;
}