List of usage examples for javax.swing JList getSelectedIndex
public int getSelectedIndex()
From source file:Main.java
public static void main(String[] args) { String[] items = { "item1", "item2", "item1" }; JList<String> list = new JList<>(items); JTextField output = new JTextField(15); JPanel gui = new JPanel(); gui.add(list);//from w w w. j a v a 2 s .co m gui.add(output); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent lse) { int index = list.getSelectedIndex(); String outputText = "Index: " + index + " Value: " + items[index]; output.setText(outputText); } }); JOptionPane.showMessageDialog(null, gui); }
From source file:Main.java
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Selecting JList"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JList jlist = new JList(labels); JScrollPane scrollPane1 = new JScrollPane(jlist); frame.add(scrollPane1);//from w ww . ja v a 2s .co m ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { System.out.println(jlist.getSelectedIndex()); } }; jlist.addListSelectionListener(listSelectionListener); frame.setSize(350, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); // Get the index of all the selected items int[] selectedIx = list.getSelectedIndices(); // Get all the selected items using the indices for (int i = 0; i < selectedIx.length; i++) { Object sel = list.getModel().getElementAt(selectedIx[i]); }/*from w w w. j av a2s . c o m*/ // Get the index of the first selected item int firstSelIx = list.getSelectedIndex(); }
From source file:Main.java
public static void addDoubleClickEvent(JList list) { list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { JList source = (JList) e.getSource(); if (e.getClickCount() == 2) { ListSelectionListener[] listeners = source.getListSelectionListeners(); for (int i = 0; i < listeners.length; i++) { listeners[i].valueChanged(new ListSelectionEvent(source, source.getSelectedIndex(), source.getSelectedIndex(), false)); }/* ww w .j ava 2 s.c o m*/ } } }); }
From source file:Main.java
@Override public void valueChanged(final ListSelectionEvent e) { JList list = (JList) e.getSource(); list.ensureIndexIsVisible(list.getSelectedIndex()); }
From source file:components.SplitPaneDemo.java
public void valueChanged(ListSelectionEvent e) { JList list = (JList) e.getSource(); updateLabel(imageNames[list.getSelectedIndex()]); }
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;// w w w . j a v a 2 s.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 (String value : values) { listModel.add(index++, value); } }
From source file:SplitPaneDemo2.java
public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return;// ww w .ja v a 2 s . co m JList theList = (JList) e.getSource(); if (theList.isSelectionEmpty()) { label.setText("Nothing selected."); } else { int index = theList.getSelectedIndex(); label.setText("Selected image number " + index); } }
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 w w. j a va2 s . co 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]); } }
From source file:ListCutPaste.java
/** * Bundle up the data for export.//from w w w. j a v a 2s .co m */ protected Transferable createTransferable(JComponent c) { JList list = (JList) c; int index = list.getSelectedIndex(); String value = (String) list.getSelectedValue(); return new StringSelection(value); }