<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FSwing_Components%2FItem_Chooser</id>
		<title>Java/Swing Components/Item Chooser - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FSwing_Components%2FItem_Chooser"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Swing_Components/Item_Chooser&amp;action=history"/>
		<updated>2026-04-19T00:34:24Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Swing_Components/Item_Chooser&amp;diff=8225&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Swing_Components/Item_Chooser&amp;diff=8225&amp;oldid=prev"/>
				<updated>2010-06-01T06:56:42Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:56, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Swing_Components/Item_Chooser&amp;diff=8224&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Swing_Components/Item_Chooser&amp;diff=8224&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:46Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== ItemChooser ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2000 David Flanagan.  All rights reserved.&lt;br /&gt;
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.&lt;br /&gt;
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.&lt;br /&gt;
 * You may study, use, and modify it for any non-commercial purpose.&lt;br /&gt;
 * You may distribute it non-commercially as long as you retain this notice.&lt;br /&gt;
 * For a commercial use license, or to purchase the book (recommended),&lt;br /&gt;
 * visit http://www.davidflanagan.ru/javaexamples2.&lt;br /&gt;
 */&lt;br /&gt;
import java.awt.BorderLayout;&lt;br /&gt;
import java.awt.Container;&lt;br /&gt;
import java.awt.event.ActionEvent;&lt;br /&gt;
import java.awt.event.ActionListener;&lt;br /&gt;
import java.awt.event.ItemEvent;&lt;br /&gt;
import java.awt.event.ItemListener;&lt;br /&gt;
import java.awt.event.WindowAdapter;&lt;br /&gt;
import java.awt.event.WindowEvent;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import javax.swing.BoxLayout;&lt;br /&gt;
import javax.swing.ButtonGroup;&lt;br /&gt;
import javax.swing.JButton;&lt;br /&gt;
import javax.swing.JComboBox;&lt;br /&gt;
import javax.swing.JFrame;&lt;br /&gt;
import javax.swing.JLabel;&lt;br /&gt;
import javax.swing.JList;&lt;br /&gt;
import javax.swing.JOptionPane;&lt;br /&gt;
import javax.swing.JPanel;&lt;br /&gt;
import javax.swing.JRadioButton;&lt;br /&gt;
import javax.swing.JScrollPane;&lt;br /&gt;
import javax.swing.border.EtchedBorder;&lt;br /&gt;
import javax.swing.border.TitledBorder;&lt;br /&gt;
import javax.swing.event.ChangeEvent;&lt;br /&gt;
import javax.swing.event.ChangeListener;&lt;br /&gt;
import javax.swing.event.ListSelectionEvent;&lt;br /&gt;
import javax.swing.event.ListSelectionListener;&lt;br /&gt;
/**&lt;br /&gt;
 * This class is a Swing component that presents a choice to the user. It allows&lt;br /&gt;
 * the choice to be presented in a JList, in a JComboBox, or with a bordered&lt;br /&gt;
 * group of JRadioButton components. Additionally, it displays the name of the&lt;br /&gt;
 * choice with a JLabel. It allows an arbitrary value to be associated with each&lt;br /&gt;
 * possible choice. Note that this component only allows one item to be selected&lt;br /&gt;
 * at a time. Multiple selections are not supported.&lt;br /&gt;
 */&lt;br /&gt;
public class ItemChooser extends JPanel {&lt;br /&gt;
  // These fields hold property values for this component&lt;br /&gt;
  String name; // The overall name of the choice&lt;br /&gt;
  String[] labels; // The text for each choice option&lt;br /&gt;
  Object[] values; // Arbitrary values associated with each option&lt;br /&gt;
  int selection; // The selected choice&lt;br /&gt;
  int presentation; // How the choice is presented&lt;br /&gt;
  // These are the legal values for the presentation field&lt;br /&gt;
  public static final int LIST = 1;&lt;br /&gt;
  public static final int COMBOBOX = 2;&lt;br /&gt;
  public static final int RADIOBUTTONS = 3;&lt;br /&gt;
  // These components are used for each of the 3 possible presentations&lt;br /&gt;
  JList list; // One type of presentation&lt;br /&gt;
  JComboBox combobox; // Another type of presentation&lt;br /&gt;
  JRadioButton[] radiobuttons; // Yet another type&lt;br /&gt;
  // The list of objects that are interested in our state&lt;br /&gt;
  ArrayList listeners = new ArrayList();&lt;br /&gt;
  // The constructor method sets everything up&lt;br /&gt;
  public ItemChooser(String name, String[] labels, Object[] values,&lt;br /&gt;
      int defaultSelection, int presentation) {&lt;br /&gt;
    // Copy the constructor arguments to instance fields&lt;br /&gt;
    this.name = name;&lt;br /&gt;
    this.labels = labels;&lt;br /&gt;
    this.values = values;&lt;br /&gt;
    this.selection = defaultSelection;&lt;br /&gt;
    this.presentation = presentation;&lt;br /&gt;
    // If no values were supplied, use the labels&lt;br /&gt;
    if (values == null)&lt;br /&gt;
      this.values = labels;&lt;br /&gt;
    // Now create content and event handlers based on presentation type&lt;br /&gt;
    switch (presentation) {&lt;br /&gt;
    case LIST:&lt;br /&gt;
      initList();&lt;br /&gt;
      break;&lt;br /&gt;
    case COMBOBOX:&lt;br /&gt;
      initComboBox();&lt;br /&gt;
      break;&lt;br /&gt;
    case RADIOBUTTONS:&lt;br /&gt;
      initRadioButtons();&lt;br /&gt;
      break;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // Initialization for JList presentation&lt;br /&gt;
  void initList() {&lt;br /&gt;
    list = new JList(labels); // Create the list&lt;br /&gt;
    list.setSelectedIndex(selection); // Set initial state&lt;br /&gt;
    // Handle state changes&lt;br /&gt;
    list.addListSelectionListener(new ListSelectionListener() {&lt;br /&gt;
      public void valueChanged(ListSelectionEvent e) {&lt;br /&gt;
        ItemChooser.this.select(list.getSelectedIndex());&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // Lay out list and name label vertically&lt;br /&gt;
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // vertical&lt;br /&gt;
    this.add(new JLabel(name)); // Display choice name&lt;br /&gt;
    this.add(new JScrollPane(list)); // Add the JList&lt;br /&gt;
  }&lt;br /&gt;
  // Initialization for JComboBox presentation&lt;br /&gt;
  void initComboBox() {&lt;br /&gt;
    combobox = new JComboBox(labels); // Create the combo box&lt;br /&gt;
//    combobox.setSelectedIndex(selection); // Set initial state&lt;br /&gt;
    // Handle changes to the state&lt;br /&gt;
    combobox.addItemListener(new ItemListener() {&lt;br /&gt;
      public void itemStateChanged(ItemEvent e) {&lt;br /&gt;
        ItemChooser.this.select(combobox.getSelectedIndex());&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // Lay out combo box and name label horizontally&lt;br /&gt;
    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));&lt;br /&gt;
    this.add(new JLabel(name));&lt;br /&gt;
    this.add(combobox);&lt;br /&gt;
  }&lt;br /&gt;
  // Initialization for JRadioButton presentation&lt;br /&gt;
  void initRadioButtons() {&lt;br /&gt;
    // Create an array of mutually exclusive radio buttons&lt;br /&gt;
    radiobuttons = new JRadioButton[labels.length]; // the array&lt;br /&gt;
    ButtonGroup radioButtonGroup = new ButtonGroup(); // used for exclusion&lt;br /&gt;
    ChangeListener listener = new ChangeListener() { // A shared listener&lt;br /&gt;
      public void stateChanged(ChangeEvent e) {&lt;br /&gt;
        JRadioButton b = (JRadioButton) e.getSource();&lt;br /&gt;
        if (b.isSelected()) {&lt;br /&gt;
          // If we received this event because a button was&lt;br /&gt;
          // selected, then loop through the list of buttons to&lt;br /&gt;
          // figure out the index of the selected one.&lt;br /&gt;
          for (int i = 0; i &amp;lt; radiobuttons.length; i++) {&lt;br /&gt;
            if (radiobuttons[i] == b) {&lt;br /&gt;
              ItemChooser.this.select(i);&lt;br /&gt;
              return;&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    // Display the choice name in a border around the buttons&lt;br /&gt;
    this.setBorder(new TitledBorder(new EtchedBorder(), name));&lt;br /&gt;
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));&lt;br /&gt;
    // Create the buttons, add them to the button group, and specify&lt;br /&gt;
    // the event listener for each one.&lt;br /&gt;
    for (int i = 0; i &amp;lt; labels.length; i++) {&lt;br /&gt;
      radiobuttons[i] = new JRadioButton(labels[i]);&lt;br /&gt;
      if (i == selection)&lt;br /&gt;
        radiobuttons[i].setSelected(true);&lt;br /&gt;
      radiobuttons[i].addChangeListener(listener);&lt;br /&gt;
      radioButtonGroup.add(radiobuttons[i]);&lt;br /&gt;
      this.add(radiobuttons[i]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // These simple property accessor methods just return field values&lt;br /&gt;
  // These are read-only properties. The values are set by the constructor&lt;br /&gt;
  // and may not be changed.&lt;br /&gt;
  public String getName() {&lt;br /&gt;
    return name;&lt;br /&gt;
  }&lt;br /&gt;
  public int getPresentation() {&lt;br /&gt;
    return presentation;&lt;br /&gt;
  }&lt;br /&gt;
  public String[] getLabels() {&lt;br /&gt;
    return labels;&lt;br /&gt;
  }&lt;br /&gt;
  public Object[] getValues() {&lt;br /&gt;
    return values;&lt;br /&gt;
  }&lt;br /&gt;
  /** Return the index of the selected item */&lt;br /&gt;
  public int getSelectedIndex() {&lt;br /&gt;
    return selection;&lt;br /&gt;
  }&lt;br /&gt;
  /** Return the object associated with the selected item */&lt;br /&gt;
  public Object getSelectedValue() {&lt;br /&gt;
    return values[selection];&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Set the selected item by specifying its index. Calling this method&lt;br /&gt;
   * changes the on-screen display but does not generate events.&lt;br /&gt;
   */&lt;br /&gt;
  public void setSelectedIndex(int selection) {&lt;br /&gt;
    switch (presentation) {&lt;br /&gt;
    case LIST:&lt;br /&gt;
      list.setSelectedIndex(selection);&lt;br /&gt;
      break;&lt;br /&gt;
    case COMBOBOX:&lt;br /&gt;
      combobox.setSelectedIndex(selection);&lt;br /&gt;
      break;&lt;br /&gt;
    case RADIOBUTTONS:&lt;br /&gt;
      radiobuttons[selection].setSelected(true);&lt;br /&gt;
      break;&lt;br /&gt;
    }&lt;br /&gt;
    this.selection = selection;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This internal method is called when the selection changes. It stores the&lt;br /&gt;
   * new selected index, and fires events to any registered listeners. The&lt;br /&gt;
   * event listeners registered on the JList, JComboBox, or JRadioButtons all&lt;br /&gt;
   * call this method.&lt;br /&gt;
   */&lt;br /&gt;
  protected void select(int selection) {&lt;br /&gt;
    this.selection = selection; // Store the new selected index&lt;br /&gt;
    if (!listeners.isEmpty()) { // If there are any listeners registered&lt;br /&gt;
      // Create an event object to describe the selection&lt;br /&gt;
      ItemChooser.Event e = new ItemChooser.Event(this, selection,&lt;br /&gt;
          values[selection]);&lt;br /&gt;
      // Loop through the listeners using an Iterator&lt;br /&gt;
      for (Iterator i = listeners.iterator(); i.hasNext();) {&lt;br /&gt;
        ItemChooser.Listener l = (ItemChooser.Listener) i.next();&lt;br /&gt;
        l.itemChosen(e); // Notify each listener of the selection&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  // These methods are for event listener registration and deregistration&lt;br /&gt;
  public void addItemChooserListener(ItemChooser.Listener l) {&lt;br /&gt;
    listeners.add(l);&lt;br /&gt;
  }&lt;br /&gt;
  public void removeItemChooserListener(ItemChooser.Listener l) {&lt;br /&gt;
    listeners.remove(l);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This inner class defines the event type generated by ItemChooser objects&lt;br /&gt;
   * The inner class name is Event, so the full name is ItemChooser.Event&lt;br /&gt;
   */&lt;br /&gt;
  public static class Event extends java.util.EventObject {&lt;br /&gt;
    int selectedIndex; // index of the selected item&lt;br /&gt;
    Object selectedValue; // the value associated with it&lt;br /&gt;
    public Event(ItemChooser source, int selectedIndex, Object selectedValue) {&lt;br /&gt;
      super(source);&lt;br /&gt;
      this.selectedIndex = selectedIndex;&lt;br /&gt;
      this.selectedValue = selectedValue;&lt;br /&gt;
    }&lt;br /&gt;
    public ItemChooser getItemChooser() {&lt;br /&gt;
      return (ItemChooser) getSource();&lt;br /&gt;
    }&lt;br /&gt;
    public int getSelectedIndex() {&lt;br /&gt;
      return selectedIndex;&lt;br /&gt;
    }&lt;br /&gt;
    public Object getSelectedValue() {&lt;br /&gt;
      return selectedValue;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * This inner interface must be implemented by any object that wants to be&lt;br /&gt;
   * notified when the current selection in a ItemChooser component changes.&lt;br /&gt;
   */&lt;br /&gt;
  public interface Listener extends java.util.EventListener {&lt;br /&gt;
    public void itemChosen(ItemChooser.Event e);&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    // Create a window, arrange to handle close requests&lt;br /&gt;
    final JFrame frame = new JFrame(&amp;quot;ItemChooser Demo&amp;quot;);&lt;br /&gt;
    frame.addWindowListener(new WindowAdapter() {&lt;br /&gt;
      public void windowClosing(WindowEvent e) {&lt;br /&gt;
        System.exit(0);&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // A &amp;quot;message line&amp;quot; to display results in&lt;br /&gt;
    final JLabel msgline = new JLabel(&amp;quot; &amp;quot;);&lt;br /&gt;
    args = new String[]{&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;};&lt;br /&gt;
    // Create a panel holding three ItemChooser components&lt;br /&gt;
    JPanel chooserPanel = new JPanel();&lt;br /&gt;
    final ItemChooser c1 = new ItemChooser(&amp;quot;Choice #1&amp;quot;, args, null, 0,&lt;br /&gt;
        ItemChooser.LIST);&lt;br /&gt;
    final ItemChooser c2 = new ItemChooser(&amp;quot;Choice #2&amp;quot;, args, null, 0,&lt;br /&gt;
        ItemChooser.ruBOBOX);&lt;br /&gt;
    final ItemChooser c3 = new ItemChooser(&amp;quot;Choice #3&amp;quot;, args, null, 0,&lt;br /&gt;
        ItemChooser.RADIOBUTTONS);&lt;br /&gt;
    // An event listener that displays changes on the message line&lt;br /&gt;
    ItemChooser.Listener l = new ItemChooser.Listener() {&lt;br /&gt;
      public void itemChosen(ItemChooser.Event e) {&lt;br /&gt;
        msgline.setText(e.getItemChooser().getName() + &amp;quot;: &amp;quot;&lt;br /&gt;
            + e.getSelectedIndex() + &amp;quot;: &amp;quot; + e.getSelectedValue());&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    c1.addItemChooserListener(l);&lt;br /&gt;
    c2.addItemChooserListener(l);&lt;br /&gt;
    c3.addItemChooserListener(l);&lt;br /&gt;
    // Instead of tracking every change with a ItemChooser.Listener,&lt;br /&gt;
    // applications can also just query the current state when&lt;br /&gt;
    // they need it. Here&amp;quot;s a button that does that.&lt;br /&gt;
    JButton report = new JButton(&amp;quot;Report&amp;quot;);&lt;br /&gt;
    report.addActionListener(new ActionListener() {&lt;br /&gt;
      public void actionPerformed(ActionEvent e) {&lt;br /&gt;
        // Note the use of multi-line italic HTML text&lt;br /&gt;
        // with the JOptionPane message dialog box.&lt;br /&gt;
        String msg = &amp;quot;&amp;lt;html&amp;gt;&amp;lt;i&amp;gt;&amp;quot; + c1.getName() + &amp;quot;: &amp;quot;&lt;br /&gt;
            + c1.getSelectedValue() + &amp;quot;&amp;lt;br&amp;gt;&amp;quot; + c2.getName() + &amp;quot;: &amp;quot;&lt;br /&gt;
            + c2.getSelectedValue() + &amp;quot;&amp;lt;br&amp;gt;&amp;quot; + c3.getName() + &amp;quot;: &amp;quot;&lt;br /&gt;
            + c3.getSelectedValue() + &amp;quot;&amp;lt;/i&amp;gt;&amp;quot;;&lt;br /&gt;
        JOptionPane.showMessageDialog(frame, msg);&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // Add the 3 ItemChooser objects, and the Button to the panel&lt;br /&gt;
    chooserPanel.add(c1);&lt;br /&gt;
    chooserPanel.add(c2);&lt;br /&gt;
    chooserPanel.add(c3);&lt;br /&gt;
    chooserPanel.add(report);&lt;br /&gt;
    // Add the panel and the message line to the window&lt;br /&gt;
    Container contentPane = frame.getContentPane();&lt;br /&gt;
    contentPane.add(chooserPanel, BorderLayout.CENTER);&lt;br /&gt;
    contentPane.add(msgline, BorderLayout.SOUTH);&lt;br /&gt;
    // Set the window size and pop it up.&lt;br /&gt;
    frame.pack();&lt;br /&gt;
    frame.show();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>