List of usage examples for javax.swing JList getSelectedValues
@Deprecated
@BeanProperty(bound = false)
public Object[] getSelectedValues()
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 first selected item Object firstSel = list.getSelectedValue(); // Get all selected items without using indices Object[] selected = list.getSelectedValues(); }
From source file:Main.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H" }; JFrame frame = new JFrame("Selecting JList"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JList jlist = new JList(labels); JScrollPane scrollPane1 = new JScrollPane(jlist); frame.add(scrollPane1, BorderLayout.CENTER); ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { System.out.println("First index: " + listSelectionEvent.getFirstIndex()); System.out.println(", Last index: " + listSelectionEvent.getLastIndex()); boolean adjust = listSelectionEvent.getValueIsAdjusting(); System.out.println(", Adjusting? " + adjust); if (!adjust) { JList list = (JList) listSelectionEvent.getSource(); int selections[] = list.getSelectedIndices(); Object selectionValues[] = list.getSelectedValues(); for (int i = 0, n = selections.length; i < n; i++) { if (i == 0) { System.out.println(" Selections: "); }/*w w w . ja v a 2 s .com*/ System.out.println(selections[i] + "/" + selectionValues[i] + " "); } } } }; jlist.addListSelectionListener(listSelectionListener); frame.setSize(350, 200); frame.setVisible(true); }
From source file:MainClass.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); JList jlist = new JList(labels); JScrollPane scrollPane1 = new JScrollPane(jlist); frame.add(scrollPane1);/* w ww . j a va 2s. c om*/ ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { System.out.println("First index: " + listSelectionEvent.getFirstIndex()); System.out.println(", Last index: " + listSelectionEvent.getLastIndex()); boolean adjust = listSelectionEvent.getValueIsAdjusting(); System.out.println(", Adjusting? " + adjust); if (!adjust) { JList list = (JList) listSelectionEvent.getSource(); int selections[] = list.getSelectedIndices(); Object selectedValues[] = list.getSelectedValues(); for (int i = 0, n = selections.length; i < n; i++) { if (i == 0) { System.out.println(" Selections: "); } System.out.println(selections[i] + "/" + selectedValues[i] + " "); } } } }; jlist.addListSelectionListener(listSelectionListener); frame.setSize(350, 200); frame.setVisible(true); }
From source file:SelectingJListSample.java
public static void main(String args[]) { String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; JFrame frame = new JFrame("Selecting JList"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JList jlist = new JList(labels); JScrollPane scrollPane1 = new JScrollPane(jlist); contentPane.add(scrollPane1, BorderLayout.WEST); ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { System.out.print("First index: " + listSelectionEvent.getFirstIndex()); System.out.print(", Last index: " + listSelectionEvent.getLastIndex()); boolean adjust = listSelectionEvent.getValueIsAdjusting(); System.out.println(", Adjusting? " + adjust); if (!adjust) { JList list = (JList) listSelectionEvent.getSource(); int selections[] = list.getSelectedIndices(); Object selectionValues[] = list.getSelectedValues(); for (int i = 0, n = selections.length; i < n; i++) { if (i == 0) { System.out.print(" Selections: "); }/* w w w. ja va2 s . c om*/ System.out.print(selections[i] + "/" + selectionValues[i] + " "); } System.out.println(); } } }; jlist.addListSelectionListener(listSelectionListener); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent mouseEvent) { JList theList = (JList) mouseEvent.getSource(); if (mouseEvent.getClickCount() == 2) { int index = theList.locationToIndex(mouseEvent.getPoint()); if (index >= 0) { Object o = theList.getModel().getElementAt(index); System.out.println("Double-clicked on: " + o.toString()); } } } }; jlist.addMouseListener(mouseListener); frame.setSize(350, 200); frame.setVisible(true); }
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 w w . ja v a2 s . co m*/ ListSelectionListener listSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent listSelectionEvent) { Object[] objs = jlist.getSelectedValues(); } }; jlist.addListSelectionListener(listSelectionListener); frame.setSize(350, 200); frame.setVisible(true); }
From source file:com.palantir.ptoss.cinch.swing.JListWiringHarness.java
private static void updateListModel(JList list, List<?> newContents) { if (newContents == null) { newContents = ImmutableList.of(); }// www . j a va2 s .c om ListModel oldModel = list.getModel(); List<Object> old = Lists.newArrayListWithExpectedSize(oldModel.getSize()); for (int i = 0; i < oldModel.getSize(); i++) { old.add(oldModel.getElementAt(i)); } if (old.equals(newContents)) { return; } Object[] selected = list.getSelectedValues(); DefaultListModel listModel = new DefaultListModel(); for (Object obj : newContents) { listModel.addElement(obj); } list.setModel(listModel); List<Integer> newIndices = Lists.newArrayListWithCapacity(selected.length); Set<Object> selectedSet = Sets.newHashSet(selected); for (int i = 0; i < listModel.size(); i++) { if (selectedSet.contains(listModel.elementAt(i))) { newIndices.add(i); } } list.setSelectedIndices(ArrayUtils.toPrimitive(newIndices.toArray(new Integer[0]))); }
From source file:Main.java
public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { JList list = (JList) evt.getSource(); Object[] selected = list.getSelectedValues(); for (int i = 0; i < selected.length; i++) { Object sel = selected[i]; }/*from w ww .j a v a 2 s . c om*/ } }
From source file:ListTest.java
public void valueChanged(ListSelectionEvent evt) { JList source = (JList) evt.getSource(); Object[] values = source.getSelectedValues(); String text = ""; for (int i = 0; i < values.length; i++) { String word = (String) values[i]; text += word + " "; }//from w w w . j a va 2 s . c om label.setText("The " + text + "fox jumps over the lazy dog."); }
From source file:ExtendedDnDDemo.java
protected String exportString(JComponent c) { JList list = (JList) c; indices = list.getSelectedIndices(); Object[] values = list.getSelectedValues(); StringBuffer buff = new StringBuffer(); for (int i = 0; i < values.length; i++) { Object val = values[i]; buff.append(val == null ? "" : val.toString()); if (i != values.length - 1) { buff.append("\n"); }/*from w w w .j a v a 2 s . c o m*/ } return buff.toString(); }
From source file:dnd.BasicDnD.java
public BasicDnD() { super(new BorderLayout()); JPanel leftPanel = createVerticalBoxPanel(); JPanel rightPanel = createVerticalBoxPanel(); //Create a table model. DefaultTableModel tm = new DefaultTableModel(); tm.addColumn("Column 0"); tm.addColumn("Column 1"); tm.addColumn("Column 2"); tm.addColumn("Column 3"); tm.addRow(new String[] { "Table 00", "Table 01", "Table 02", "Table 03" }); tm.addRow(new String[] { "Table 10", "Table 11", "Table 12", "Table 13" }); tm.addRow(new String[] { "Table 20", "Table 21", "Table 22", "Table 23" }); tm.addRow(new String[] { "Table 30", "Table 31", "Table 32", "Table 33" }); //LEFT COLUMN //Use the table model to create a table. table = new JTable(tm); leftPanel.add(createPanelForComponent(table, "JTable")); //Create a color chooser. colorChooser = new JColorChooser(); leftPanel.add(createPanelForComponent(colorChooser, "JColorChooser")); //RIGHT COLUMN //Create a textfield. textField = new JTextField(30); textField.setText("Favorite foods:\nPizza, Moussaka, Pot roast"); rightPanel.add(createPanelForComponent(textField, "JTextField")); //Create a scrolled text area. textArea = new JTextArea(5, 30); textArea.setText("Favorite shows:\nBuffy, Alias, Angel"); JScrollPane scrollPane = new JScrollPane(textArea); rightPanel.add(createPanelForComponent(scrollPane, "JTextArea")); //Create a list model and a list. DefaultListModel listModel = new DefaultListModel(); listModel.addElement("Martha Washington"); listModel.addElement("Abigail Adams"); listModel.addElement("Martha Randolph"); listModel.addElement("Dolley Madison"); listModel.addElement("Elizabeth Monroe"); listModel.addElement("Louisa Adams"); listModel.addElement("Emily Donelson"); list = new JList(listModel); list.setVisibleRowCount(-1);/*w w w. ja v a2 s.c om*/ list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); list.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport info) { // we only import Strings if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); if (dl.getIndex() == -1) { return false; } return true; } public boolean importData(TransferHandler.TransferSupport info) { if (!info.isDrop()) { return false; } // Check for String flavor if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { displayDropLocation("List doesn't accept a drop of this type."); return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); DefaultListModel listModel = (DefaultListModel) list.getModel(); int index = dl.getIndex(); boolean insert = dl.isInsert(); // Get the current string under the drop. String value = (String) listModel.getElementAt(index); // 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; } // Display a dialog with the drop information. String dropValue = "\"" + data + "\" dropped "; if (dl.isInsert()) { if (dl.getIndex() == 0) { displayDropLocation(dropValue + "at beginning of list"); } else if (dl.getIndex() >= list.getModel().getSize()) { displayDropLocation(dropValue + "at end of list"); } else { String value1 = (String) list.getModel().getElementAt(dl.getIndex() - 1); String value2 = (String) list.getModel().getElementAt(dl.getIndex()); displayDropLocation(dropValue + "between \"" + value1 + "\" and \"" + value2 + "\""); } } else { displayDropLocation(dropValue + "on top of " + "\"" + value + "\""); } /** This is commented out for the basicdemo.html tutorial page. ** If you add this code snippet back and delete the ** "return false;" line, the list will accept drops ** of type string. // Perform the actual import. if (insert) { listModel.add(index, data); } else { listModel.set(index, data); } return true; */ return false; } public int getSourceActions(JComponent c) { return COPY; } protected Transferable createTransferable(JComponent c) { JList list = (JList) c; Object[] values = list.getSelectedValues(); StringBuffer buff = new StringBuffer(); for (int i = 0; i < values.length; i++) { Object val = values[i]; buff.append(val == null ? "" : val.toString()); if (i != values.length - 1) { buff.append("\n"); } } return new StringSelection(buff.toString()); } }); list.setDropMode(DropMode.ON_OR_INSERT); JScrollPane listView = new JScrollPane(list); listView.setPreferredSize(new Dimension(300, 100)); rightPanel.add(createPanelForComponent(listView, "JList")); //Create a tree. DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Mia Familia"); DefaultMutableTreeNode sharon = new DefaultMutableTreeNode("Sharon"); rootNode.add(sharon); DefaultMutableTreeNode maya = new DefaultMutableTreeNode("Maya"); sharon.add(maya); DefaultMutableTreeNode anya = new DefaultMutableTreeNode("Anya"); sharon.add(anya); sharon.add(new DefaultMutableTreeNode("Bongo")); maya.add(new DefaultMutableTreeNode("Muffin")); anya.add(new DefaultMutableTreeNode("Winky")); DefaultTreeModel model = new DefaultTreeModel(rootNode); tree = new JTree(model); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); JScrollPane treeView = new JScrollPane(tree); treeView.setPreferredSize(new Dimension(300, 100)); rightPanel.add(createPanelForComponent(treeView, "JTree")); //Create the toggle button. toggleDnD = new JCheckBox("Turn on Drag and Drop"); toggleDnD.setActionCommand("toggleDnD"); toggleDnD.addActionListener(this); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel); splitPane.setOneTouchExpandable(true); add(splitPane, BorderLayout.CENTER); add(toggleDnD, BorderLayout.PAGE_END); setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); }