A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes.
In addition to lists, the following Swing components present multiple selectable items to the user: combo boxes, menus, tables, and groups of check boxes or radio buttons. To display hierarchical data, use a tree.
ListTest.java
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import javax.swing.ButtonGroup; import javax.swing.JList; import javax.swing.JRadioButton; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; /* This program demonstrates a simple fixed list of strings. */ public class ListTest { public static void main(String[] args) { JFrame frame = new ListFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } /** This frame contains a word list and a label that shows a sentence made up from the chosen words. Note that you can select multiple words with Ctrl+click and Shift+click. */ class ListFrame extends JFrame { public ListFrame() { setTitle("ListTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); String[] words = { "quick","brown","hungry","wild","silent", "huge","private","abstract","static","final" }; wordList = new JList(words); // To create List box. /* Alternatively, you can use an anonymous array: JList wordList = new JList(new String[] {"quick", "brown", "hungry", "wild","silent","huge","private","abstract","static","final" }); */ /* By default, the list component displays eight items; use the setVisibleRowCount method to change that value: wordList.setVisibleRowCount(4); // display 4 items You can also restrict the user to a more limited selection mode with the setSelectionMode method: wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // select one item at a time wordList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); // select one item or one range of items */ wordList.setVisibleRowCount(4); //List boxes do not scroll automatically. To make a list box scroll, you must insert it into a scroll pane: JScrollPane scrollPane = new JScrollPane(wordList); listPanel = new JPanel(); listPanel.add(scrollPane); wordList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { /* The getSelectedValues method returns an array of objects containing all selected items. Cast each array element to a string. Object[] values = list.getSelectedValues(); //returns the selected values or an empty array if the selection is empty. for (Object value : values) do something with (String) value; ....................................................... If your list does not allow multiple selections, you can call the convenience method getSelectedValue. It returns the first selected value (which you know to be the only value if multiple selections are disallowed). String value = (String) list.getSelectedValue(); */ Object[] values = wordList.getSelectedValues(); StringBuilder text = new StringBuilder(prefix); // for (int i = 0; i < values.length; i++) for (Object value : values) { //String word = values[i]; String word = (String) value; text.append(word); text.append(" "); } text.append(suffix); label.setText(text.toString()); } }); buttonPanel = new JPanel(); group = new ButtonGroup(); /* You can set the layout orientation to one of three values: JList.VERTICAL (the default): arrange all items vertically JList.VERTICAL_WRAP: start new columns if there are more items than the visible row count JList.HORIZONTAL_WRAP: start new columns if there are more items than the visible row count, but fill them horizontally. */ makeButton("Vertical", JList.VERTICAL); makeButton("Vertical Wrap", JList.VERTICAL_WRAP); makeButton("Horizontal Wrap", JList.HORIZONTAL_WRAP); add(listPanel, BorderLayout.NORTH); label = new JLabel(prefix + suffix); add(label, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); } /** Makes a radio button to set the layout orientation. @param label the button label @param orientation the orientation for the list */ private void makeButton(String label, final int orientation) { JRadioButton button = new JRadioButton(label); buttonPanel.add(button); if (group.getButtonCount() == 0) button.setSelected(true); group.add(button); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent event) { wordList.setLayoutOrientation(orientation); listPanel.revalidate(); } }); } private static final int DEFAULT_WIDTH = 400; private static final int DEFAULT_HEIGHT = 300; private JPanel listPanel; private JList wordList; private JLabel label; private JPanel buttonPanel; private ButtonGroup group; private String prefix = "The "; private String suffix = "fox jumps over the lazy dog."; } /* List components do not react to double clicks from a mouse. As envisioned by the designers of Swing, you use a list to select an item, and then you click a button to make something happen. However, some user interfaces allow a user to double-click on a list item as a shortcut for item selection and acceptance of a default action. If you want to implement this behavior, you have to add a mouse listener to the list box, then trap the mouse event as follows: public void mouseClicked(MouseEvent evt) { if (evt.getClickCount() == 2) { JList source = (JList) evt.getSource(); Object[] selection = source.getSelectedValues(); doAction(selection); } } */OUTPUT:-
No comments:
Post a Comment