Java Tutorial/Swing/JList Selection
Содержание
- 1 Add another selection - the third item
- 2 A single-selection JList.
- 3 Clear all selections
- 4 Deselect the first item
- 5 Determine if there are any selected items
- 6 Get the index of the last selected item
- 7 Getting the Selected Items in a JList Component
- 8 Listening for Changes to the Items in a JList Component
- 9 Listening for Changes to the Selection in a JList Component
- 10 List selection event
- 11 Multiple ranges of selected items are allowed
- 12 Select all the items
- 13 Select the first item
- 14 Setting the Selected Items in a JList Component
- 15 Setting the Selection Mode of a JList Component
- 16 The selected items must be in a contiguous range
Add another selection - the third item
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); int start = 2; int end = 2; list.addSelectionInterval(start, end); }
}</source>
A single-selection JList.
<source lang="java">
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Main {
String languages[] = { "Java", "Perl", "Python", "C++", "Basic", "C#" }; JList jlst = new JList(languages); Main() { JFrame jfrm = new JFrame("Use JList"); jfrm.setLayout(new FlowLayout()); jfrm.setSize(200, 160); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jlst.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent le) { int idx = jlst.getSelectedIndex(); if (idx != -1) System.out.println("Current selection: " + languages[idx]); else System.out.println("Please choose a language."); } }); jfrm.add(new JScrollPane(jlst)); jfrm.setSize(300, 300); jfrm.setVisible(true); } public static void main(String args[]) { new Main(); }
}</source>
Clear all selections
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); list.clearSelection(); }
}</source>
Deselect the first item
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); int start = 0; int end = 0; list.removeSelectionInterval(start, end); }
}</source>
Determine if there are any selected items
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); boolean anySelected = !list.isSelectionEmpty(); }
}</source>
Get the index of the last selected item
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); int lastSelIx = list.getMaxSelectionIndex(); }
}</source>
Getting the Selected Items in a JList Component
<source lang="java">
import javax.swing.JList; public class Main {
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]); } // Get the index of the first selected item int firstSelIx = list.getSelectedIndex(); }
}</source>
Listening for Changes to the Items in a JList Component
<source lang="java">
import javax.swing.DefaultListModel; import javax.swing.JList; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; public class Main {
public static void main(String[] argv) throws Exception { JList list = new JList(); // Register a list data listener DefaultListModel model = (DefaultListModel) list.getModel(); model.addListDataListener(new MyListDataListener()); }
} class MyListDataListener implements ListDataListener {
public void intervalAdded(ListDataEvent evt) { DefaultListModel model = (DefaultListModel) evt.getSource(); int start = evt.getIndex0(); int end = evt.getIndex1(); int count = end - start + 1; for (int i = start; i <= end; i++) { Object item = model.getElementAt(i); } } public void intervalRemoved(ListDataEvent evt) { int start = evt.getIndex0(); int end = evt.getIndex1(); int count = end - start + 1; } public void contentsChanged(ListDataEvent evt) { DefaultListModel model = (DefaultListModel) evt.getSource(); int start = evt.getIndex0(); int end = evt.getIndex1(); int count = end - start + 1; for (int i = start; i <= end; i++) { Object item = model.getElementAt(i); } }
}</source>
Listening for Changes to the Selection in a JList Component
<source lang="java">
import javax.swing.JList; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); list.addListSelectionListener(new MyListSelectionListener()); }
} class MyListSelectionListener implements ListSelectionListener {
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]; } } }
}</source>
List selection event
<source lang="java">
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class SelectionHandler implements ActionListener, ListSelectionListener {
private JLabel direction; private JList source, destination; public SelectionHandler(JLabel d, JList left, JList right) { direction = d; source = left; destination = right; } public void actionPerformed(ActionEvent a) { JComboBox cb = (JComboBox) a.getSource(); String selected = (String) cb.getSelectedItem(); String current = direction.getText(); if (!selected.equals(current)) { direction.setText(selected); JList temp = source; source = destination; destination = temp; source.clearSelection(); destination.clearSelection(); } } public void valueChanged(ListSelectionEvent e) { JList list = (JList) e.getSource(); String item = (String) source.getSelectedValue(); System.out.println(item); if (item != null && !item.equals("")) { removeFromSource(item); addToDestination(item); } } private void removeFromSource(String item) { ListModel model = source.getModel(); Vector listData = new Vector(); for (int i = 0; i < model.getSize(); i++) { listData.addElement(model.getElementAt(i)); } listData.removeElement(item); source.setListData(listData); } private void addToDestination(String item) { ListModel model = destination.getModel(); Vector listData = new Vector(); for (int i = 0; i < model.getSize(); i++) { listData.addElement(model.getElementAt(i)); } listData.addElement(item); destination.setListData(listData); }
}</source>
Multiple ranges of selected items are allowed
<source lang="java">
import javax.swing.DefaultListSelectionModel; import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); list.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION); }
}</source>
Select all the items
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); int start = 0; int end = list.getModel().getSize() - 1; if (end >= 0) { list.setSelectionInterval(start, end); } }
}</source>
Select the first item
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); int start = 0; int end = 0; list.setSelectionInterval(start, end); }
}</source>
Setting the Selected Items in a JList Component
<source lang="java">
import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); // Select the second item int start = 1; int end = 1; list.setSelectionInterval(start, end); }
}</source>
Setting the Selection Mode of a JList Component
<source lang="java">
import javax.swing.DefaultListSelectionModel; import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); // Get the current selection model mode int mode = list.getSelectionMode(); // MULTIPLE_INTERVAL_SELECTION // Only one item can be selected list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); }
}</source>
The selected items must be in a contiguous range
<source lang="java">
import javax.swing.DefaultListSelectionModel; import javax.swing.JList; public class Main {
public static void main(String[] argv) throws Exception { String[] items = { "A", "B", "C", "D" }; JList list = new JList(items); list.setSelectionMode(DefaultListSelectionModel.SINGLE_INTERVAL_SELECTION); }
}</source>