Example usage for javax.swing JList getSelectedIndex

List of usage examples for javax.swing JList getSelectedIndex

Introduction

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

Prototype

public int getSelectedIndex() 

Source Link

Document

Returns the smallest selected cell index; the selection when only a single item is selected in the list.

Usage

From source file:SplitPaneDemo2.java

public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting())
        return;/*ww  w. j a v a  2 s. c o  m*/

    JList theList = (JList) e.getSource();
    if (theList.isSelectionEmpty()) {
        picture.setIcon(null);
        picture.setText(null);
    } else {
        int index = theList.getSelectedIndex();
        ImageIcon newImage = createImageIcon("images/" + (String) imageNames.elementAt(index));
        picture.setIcon(newImage);
        if (newImage != null) {
            picture.setText(null);
        } else {
            picture.setText("Image not found: " + (String) imageNames.elementAt(index));
        }
    }
}

From source file:ListCutPaste.java

/**
 * When the export is complete, remove the old list entry if the action was a
 * move.//from www . j  a v  a  2s  .  c o m
 */
protected void exportDone(JComponent c, Transferable data, int action) {
    if (action != MOVE) {
        return;
    }
    JList list = (JList) c;
    DefaultListModel model = (DefaultListModel) list.getModel();
    int index = list.getSelectedIndex();
    model.remove(index);
}

From source file:ExtendedDnDDemo.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  w w.  j  a  va 2  s.  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 (int i = 0; i < values.length; i++) {
        listModel.add(index++, values[i]);
    }
}

From source file:fr.eurecom.hybris.demogui.HybrisDemoGui.java

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_DELETE) {

        JList<String> jlist = (JList<String>) e.getComponent();
        if (jlist.getSelectedIndex() >= 0) {

            if (jlist.equals(lstAmazon)) {
                new Thread(cm.new BackgroundWorker(OperationType.DELETE, ClientType.AWS,
                        jlist.getSelectedValue(), null)).start();
                System.out.println("Removed " + jlist.getSelectedValue() + " from Amazon S3.");
                corruptedItems.remove("amazon" + jlist.getSelectedValue());
            } else if (jlist.equals(lstAzure)) {
                new Thread(cm.new BackgroundWorker(OperationType.DELETE, ClientType.AZURE,
                        jlist.getSelectedValue(), null)).start();
                System.out.println("Removed " + jlist.getSelectedValue() + " from Azure.");
                corruptedItems.remove("azure" + jlist.getSelectedValue());
            } else if (jlist.equals(lstGoogle)) {
                new Thread(cm.new BackgroundWorker(OperationType.DELETE, ClientType.GOOGLE,
                        jlist.getSelectedValue(), null)).start();
                System.out.println("Removed " + jlist.getSelectedValue() + " from Google.");
                corruptedItems.remove("google" + jlist.getSelectedValue());
            } else if (jlist.equals(lstRackspace)) {
                new Thread(cm.new BackgroundWorker(OperationType.DELETE, ClientType.RACKSPACE,
                        jlist.getSelectedValue(), null)).start();
                System.out.println("Removed " + jlist.getSelectedValue() + " from Rackspace.");
                corruptedItems.remove("rackspace" + jlist.getSelectedValue());
            } else if (jlist.equals(lstHybris)) {
                new Thread(cm.new BackgroundWorker(OperationType.DELETE, ClientType.HYBRIS,
                        jlist.getSelectedValue(), null)).start();
                System.out.println("Removed " + jlist.getSelectedValue() + " from Hybris.");
            }/*from  ww w  .ja  va 2s .c o  m*/
        }
    } else if (e.getKeyChar() == 'c') {
        JList<String> jlist = (JList<String>) e.getComponent();
        if (jlist.getSelectedIndex() >= 0) {

            byte[] corruptedPayload = "I_AM_THE_BOGUS_PAYLOAD".getBytes();

            if (jlist.equals(lstAmazon)) {
                JOptionPane.showMessageDialog(frame, "Corrupted " + jlist.getSelectedValue() + " on Amazon S3.",
                        "Corruption", JOptionPane.WARNING_MESSAGE);
                System.out.println("Corrupted " + jlist.getSelectedValue() + " on Amazon S3.");
                corruptedItems.add("amazon" + jlist.getSelectedValue());
                new Thread(cm.new BackgroundWorker(OperationType.PUT, ClientType.AWS, jlist.getSelectedValue(),
                        corruptedPayload)).start();
            } else if (jlist.equals(lstAzure)) {
                System.out.println("Corrupted " + jlist.getSelectedValue() + " on Azure.");
                JOptionPane.showMessageDialog(frame, "Corrupted " + jlist.getSelectedValue() + " on Azure.",
                        "Corruption", JOptionPane.WARNING_MESSAGE);
                corruptedItems.add("azure" + jlist.getSelectedValue());
                new Thread(cm.new BackgroundWorker(OperationType.PUT, ClientType.AZURE,
                        jlist.getSelectedValue(), corruptedPayload)).start();
            } else if (jlist.equals(lstGoogle)) {
                JOptionPane.showMessageDialog(frame, "Corrupted " + jlist.getSelectedValue() + " on Google.",
                        "Corruption", JOptionPane.WARNING_MESSAGE);
                System.out.println("Corrupted " + jlist.getSelectedValue() + " on Google.");
                corruptedItems.add("google" + jlist.getSelectedValue());
                new Thread(cm.new BackgroundWorker(OperationType.PUT, ClientType.GOOGLE,
                        jlist.getSelectedValue(), corruptedPayload)).start();
            } else if (jlist.equals(lstRackspace)) {
                JOptionPane.showMessageDialog(frame, "Corrupted " + jlist.getSelectedValue() + " on Rackspace.",
                        "Corruption", JOptionPane.WARNING_MESSAGE);
                System.out.println("Corrupted " + jlist.getSelectedValue() + " on Rackspace.");
                corruptedItems.add("rackspace" + jlist.getSelectedValue());
                new Thread(cm.new BackgroundWorker(OperationType.PUT, ClientType.RACKSPACE,
                        jlist.getSelectedValue(), corruptedPayload)).start();
            }
        }
    }
}

From source file:edu.ku.brc.af.ui.forms.validation.DataChangeNotifier.java

/**
 * Returns a string value for a control to compare to see if the value has changed.
 * @param component the component to get the value fromn
 * @return Returns a string value for a control to compare to see if the value has changed
 *//*from w w w. j  a  va 2  s .co  m*/
public String getValueForControl(Component component) {
    //log.debug("DataChangeNotifier - getValueForControl "+component);

    if (component instanceof JTextComponent) {
        return ((JTextComponent) component).getText();

    } else if (component instanceof JToggleButton) {
        return Boolean.toString(((JToggleButton) component).isSelected());

    } else if (component instanceof JComboBox) {
        return ((JComboBox) component).getSelectedItem().toString();

    } else if (component instanceof JList) {
        JList list = (JList) component;
        int inx = list.getSelectedIndex();
        return inx == -1 ? "" : list.getModel().getElementAt(inx).toString();

    } else if (component instanceof JCheckBox) {
        return ((JCheckBox) component).isSelected() ? "true" : "false";

    } else if (component instanceof GetSetValueIFace) {
        Object value = ((GetSetValueIFace) component).getValue();
        return (value != null) ? value.toString() : null;

    } else if (component instanceof MultiStateIconButon) {
        return String.valueOf(((MultiStateIconButon) component).getState());
    } else {
        throw new RuntimeException("Can't get a value for componentonent: " + component);
    }
}

From source file:net.sf.jabref.gui.openoffice.OOBibBase.java

public static XTextDocument selectComponent(List<XTextDocument> list)
        throws UnknownPropertyException, WrappedTargetException, IndexOutOfBoundsException {
    String[] values = new String[list.size()];
    int ii = 0;/*  w  w w  .  jav a  2 s  .  c  o  m*/
    for (XTextDocument doc : list) {
        values[ii] = String.valueOf(OOUtil.getProperty(doc.getCurrentController().getFrame(), "Title"));
        ii++;
    }
    JList<String> sel = new JList<>(values);
    sel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    sel.setSelectedIndex(0);
    int ans = JOptionPane.showConfirmDialog(null, new JScrollPane(sel), Localization.lang("Select document"),
            JOptionPane.OK_CANCEL_OPTION);
    if (ans == JOptionPane.OK_OPTION) {
        return list.get(sel.getSelectedIndex());
    } else {
        return null;
    }
}

From source file:fr.free.hd.servers.gui.PhonemView.java

@Override
protected JComponent createControl() {
    final JPanel view = new JPanel(new BorderLayout());

    Collection<Phonem> phonesList = phonemsDAO.getPhonems();
    Map<String, Phonem> mapList = new HashMap<String, Phonem>();
    for (Phonem phonem : phonesList) {
        mapList.put(phonem.getPhonem(), phonem);
    }//  w  ww .j  a  v a  2  s .c om

    final StatementListModel model = new StatementListModel(mapList);

    printCommand.setModel(model);
    printCommand.setFace(face);
    copyCommand.setModel(model);
    copyCommand.setFace(face);

    list = new JList(model);
    final JScrollPane sp = new JScrollPane(list);
    final JTextField field = new JTextField();
    field.getDocument().addDocumentListener(new DocumentListener() {

        @Override
        public void changedUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            model.setString(field.getText());
        }

    });

    final PhonemListModel phonemModel = new PhonemListModel((List<Phonem>) phonesList);
    final JList phonemList = new JList(phonemModel);
    final JScrollPane spPhonemList = new JScrollPane(phonemList);
    phonemList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        // private int oldIndex = -1;
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting() == false) {
                Phonem innerPhonem = (Phonem) phonemModel.getElementAt(phonemList.getSelectedIndex());
                field.setText(field.getText() + innerPhonem.getPhonem());
            }
        }
    });
    phonemList.setCellRenderer(new PhonemListRenderer());
    list.setCellRenderer(new StatementPhonemListRenderer(face));
    list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    list.setVisibleRowCount(1);

    view.add(spPhonemList, BorderLayout.WEST);
    view.add(sp, BorderLayout.CENTER);
    view.add(field, BorderLayout.SOUTH);

    field.requestFocus();

    return view;
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void pickListUpAction(final JList list) {
    final DefaultListModel selectedListModel = (DefaultListModel) list.getModel();
    final int selectedIndex = list.getSelectedIndex();
    Object element;//w ww.java  2  s . c om
    if (selectedIndex > 0) {
        element = list.getSelectedValue();
        selectedListModel.remove(selectedIndex);
        selectedListModel.add(selectedIndex - 1, element);
    }
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void pickListDownAction(final JList list) {
    final DefaultListModel selectedListModel = (DefaultListModel) list.getModel();
    final int selectedIndex = list.getSelectedIndex();
    final int listSize = selectedListModel.getSize();
    Object element;/*from www  .j a v  a 2s . co  m*/
    if (selectedIndex < listSize - 1) {
        element = list.getSelectedValue();
        selectedListModel.remove(selectedIndex);
        selectedListModel.add(selectedIndex + 1, element);
    }
}

From source file:br.upe.ecomp.dosa.view.wizard.WizardAction.java

private void pickListAddAction(final JList sourceList, final JList destinationList) {
    if (sourceList.getSelectedValue() != null) {
        final Object element = sourceList.getSelectedValue();
        ((DefaultListModel) sourceList.getModel()).remove(sourceList.getSelectedIndex());
        ((DefaultListModel) destinationList.getModel()).addElement(element);
    }//from   ww  w  . ja  va  2s.co  m
}