jlist.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,327 行 · 第 1/3 页

JAVA
1,327
字号
/* JList.java --   Copyright (C) 2002, 2003, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Color;import java.awt.Component;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.Point;import java.awt.Rectangle;import java.util.Vector;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.swing.event.ListDataEvent;import javax.swing.event.ListDataListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.plaf.ListUI;/** * <p>This class is a facade over three separate objects: {@link * javax.swing.ListModel}, {@link javax.swing.ListSelectionModel} and * {@link javax.swing.plaf.ListUI}. The facade represents a unified "list" * concept, with independently replacable (possibly client-provided) models * for its contents and its current selection. In addition, each element in * the list is rendered via a strategy class {@link * javax.swing.ListCellRenderer}.</p> * * <p>Lists have many properties, some of which are stored in this class * while others are delegated to the list's model or selection. The * following properties are available:</p> * * <table> * <tr><th>Property                       </th><th>Stored in</th><th>Bound?</th></tr> * <tr><td>accessibleContext              </td><td>list     </td><td>no    </td></tr> * <tr><td>anchorSelectionIndex           </td><td>selection</td><td>no    </td></tr> * <tr><td>cellRenderer                   </td><td>list     </td><td>yes   </td></tr> * <tr><td>dragEnabled                    </td><td>list     </td><td>no    </td></tr> * <tr><td>firstVisibleIndex              </td><td>list     </td><td>no    </td></tr> * <tr><td>fixedCellHeight                </td><td>list     </td><td>yes   </td></tr> * <tr><td>fixedCellWidth                 </td><td>list     </td><td>yes   </td></tr> * <tr><td>lastVisibleIndex               </td><td>list     </td><td>no    </td></tr> * <tr><td>layoutOrientation              </td><td>list     </td><td>yes   </td></tr> * <tr><td>leadSelectionIndex             </td><td>selection</td><td>no    </td></tr> * <tr><td>maxSelectionIndex              </td><td>selection</td><td>no    </td></tr> * <tr><td>minSelectionIndex              </td><td>selection</td><td>no    </td></tr> * <tr><td>model                          </td><td>list     </td><td>yes   </td></tr> * <tr><td>opaque                         </td><td>list     </td><td>no    </td></tr> * <tr><td>preferredScrollableViewportSize</td><td>list     </td><td>no    </td></tr> * <tr><td>prototypeCellValue             </td><td>list     </td><td>yes   </td></tr> * <tr><td>scrollableTracksViewportHeight </td><td>list     </td><td>no    </td></tr> * <tr><td>scrollableTracksViewportWidth  </td><td>list     </td><td>no    </td></tr> * <tr><td>selectedIndex                  </td><td>selection</td><td>no    </td></tr> * <tr><td>selectedIndices                </td><td>selection</td><td>no    </td></tr> * <tr><td>selectedValue                  </td><td>model    </td><td>no    </td></tr> * <tr><td>selectedValues                 </td><td>model    </td><td>no    </td></tr> * <tr><td>selectionBackground            </td><td>list     </td><td>yes   </td></tr> * <tr><td>selectionEmpty                 </td><td>selection</td><td>no    </td></tr> * <tr><td>selectionForeground            </td><td>list     </td><td>yes   </td></tr> * <tr><td>selectionMode                  </td><td>selection</td><td>no    </td></tr> * <tr><td>selectionModel                 </td><td>list     </td><td>yes   </td></tr> * <tr><td>UI                             </td><td>list     </td><td>yes   </td></tr> * <tr><td>UIClassID                      </td><td>list     </td><td>no    </td></tr> * <tr><td>valueIsAdjusting               </td><td>list     </td><td>no    </td></tr> * <tr><td>visibleRowCount                </td><td>list     </td><td>no    </td></tr> * </table>  * * @author Graydon Hoare (graydon@redhat.com) */public class JList extends JComponent implements Accessible, Scrollable{  private static final long serialVersionUID = 4406629526391098046L;  /**    * Constant value used in "layoutOrientation" property. This value means   * that cells are laid out in a single vertical column. This is the default.    */  public static final int VERTICAL = 0;  /**    * Constant value used in "layoutOrientation" property. This value means   * that cells are laid out in multiple columns "newspaper style", filling   * vertically first, then horizontally.    */  public static final int VERTICAL_WRAP = 1;    /**    * Constant value used in "layoutOrientation" property. This value means   * that cells are laid out in multiple columns "newspaper style",   * filling horizontally first, then vertically.    */  public static final int HORIZONTAL_WRAP = 2;  /**   * This property indicates whether "drag and drop" functions are enabled   * on the list.   */  boolean dragEnabled;  /** This property provides a strategy for rendering cells in the list. */  ListCellRenderer cellRenderer;  /**   * This property indicates an fixed width to assign to all cells in the   * list. If its value is <code>-1</code>, no width has been   * assigned. This value can be set explicitly, or implicitly by setting   * the {@link #prototypeCellValue} property.   */  int fixedCellWidth;    /**   * This property indicates an fixed height to assign to all cells in the   * list. If its value is <code>-1</code>, no height has been   * assigned. This value can be set explicitly, or implicitly by setting   * the {@link #prototypeCellValue} property.   */  int fixedCellHeight;  /**    * This property holds the current layout orientation of the list, which   * is one of the integer constants {@link #VERTICAL}, {@link   * #VERTICAL_WRAP}, or {@link #HORIZONTAL_WRAP}.    */  int layoutOrientation;    /** This property holds the data elements displayed by the list. */  ListModel model;  /**   * <p>This property holds a reference to a "prototype" data value --   * typically a String -- which is used to calculate the {@link   * #fixedCellWidth} and {@link #fixedCellHeight} properties, using the   * {@link #cellRenderer} property to acquire a component to render the   * prototype.</p>   *   * <p>It is important that you <em>not</em> set this value to a   * component. It has to be a <em>data value</em> such as the objects you   * would find in the list's model. Setting it to a component will have   * undefined (and undesirable) affects. </p>   */  Object prototypeCellValue;  /**    * This property specifies a foreground color for the selected cells in   * the list. When {@link ListCellRenderer.getListCellRendererComponent}   * is called with a selected cell object, the component returned will   * have its "foreground" set to this color.   */  Color selectionBackground;  /**    * This property specifies a background color for the selected cells in   * the list. When {@link ListCellRenderer.getListCellRendererComponent}   * is called with a selected cell object, the component returned will   * have its "background" property set to this color.   */  Color selectionForeground;  /**    * This property holds a description of which data elements in the {@link   * #model} property should be considered "selected", when displaying and   * interacting with the list.   */  ListSelectionModel selectionModel;  /**   * This property indicates that the list's selection is currently   * "adjusting" -- perhaps due to a user actively dragging the mouse over   * multiple list elements -- and is therefore likely to change again in   * the near future. A {@link ListSelectionListener} might choose to delay   * updating its view of the list's selection until this property is   * false, meaning that the adjustment has completed.   */  boolean valueIsAdjusting;  /**    * This property indicates a <em>preference</em> for the number of rows   * displayed in the list, and will scale the   * {@link #preferredScrollableViewportSize} property accordingly. The actual   * number of displayed rows, when the list is placed in a real {@link   * Viewport} or other component, may be greater or less than this number.   */  int visibleRowCount;  /**   * Fire a {@link ListSelectionEvent} to all the registered ListSelectionListeners.   */  protected void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)   {    ListSelectionEvent evt = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);    ListSelectionListener listeners[] = getListSelectionListeners();    for (int i = 0; i < listeners.length; ++i)      {        listeners[i].valueChanged(evt);      }  }  /**   * This private listener propagates {@link ListSelectionEvent} events   * from the list's "selectionModel" property to the list's {@link   * ListSelectionListener} listeners. It also listens to {@link   * ListDataEvent} events from the list's {@link #model} property. If this   * class receives either type of event, it triggers repainting of the   * list.   */  private class ListListener     implements ListSelectionListener, ListDataListener  {    // ListDataListener events    public void contentsChanged(ListDataEvent event)    {      JList.this.revalidate();      JList.this.repaint();    }    public void intervalAdded(ListDataEvent event)    {      JList.this.revalidate();      JList.this.repaint();    }    public void intervalRemoved(ListDataEvent event)    {      JList.this.revalidate();      JList.this.repaint();    }    // ListSelectionListener events    public void valueChanged(ListSelectionEvent event)    {      JList.this.fireSelectionValueChanged(event.getFirstIndex(),                                           event.getLastIndex(),                                           event.getValueIsAdjusting());      JList.this.repaint();    }  };  /**    * Shared ListListener instance, subscribed to both the current {@link   * #model} and {@link #selectionModel} properties of the list.   */  ListListener listListener;  /**   * Creates a new JList object.   */  public JList()  {    init();  }  /**   * Creates a new JList object.   *   * @param listData Initial data to populate the list with   */  public JList(Object[] listData)  {    init();    setListData(listData);  }  /**   * Creates a new JList object.   *   * @param listData Initial data to populate the list with   */  public JList(Vector listData)  {    init();    setListData(listData);  }  /**   * Creates a new JList object.   *   * @param listData Initial data to populate the list with   */  public JList(ListModel listData)  {    init();    setModel(listData);  }  void init()  {    dragEnabled = false;    fixedCellHeight = -1;    fixedCellWidth = -1;    layoutOrientation = VERTICAL;    opaque = true;    valueIsAdjusting = false;    visibleRowCount = 8;    cellRenderer = new DefaultListCellRenderer();    listListener = new ListListener();    setModel(new DefaultListModel());    setSelectionModel(createSelectionModel());    updateUI();  }  /**   * Creates the default <code>ListSelectionModel</code>.   *   * @return the <code>ListSelectionModel</code>   */  protected ListSelectionModel createSelectionModel()  {    return new DefaultListSelectionModel();  }    /**   * Gets the value of the {@link #fixedCellHeight} property. This property   * may be <code>-1</code> to indicate that no cell height has been   * set. This property is also set implicitly when the   * {@link #prototypeCellValue} property is set.   *   * @return The current value of the property    *    * @see #fixedCellHeight   * @see #setFixedCellHeight   * @see #setPrototypeCellValue   */  public int getFixedCellHeight()  {    return fixedCellHeight;  }  /**   * Sets the value of the {@link #fixedCellHeight} property. This property   * may be <code>-1</code> to indicate that no cell height has been   * set. This property is also set implicitly when the {@link   * #prototypeCellValue} property is set, but setting it explicitly   * overrides the height computed from {@link #prototypeCellValue}.   *   * @see #getFixedCellHeight   * @see #getPrototypeCellValue   */  public void setFixedCellHeight(int h)  {    if (fixedCellHeight == h)      return;    int old = fixedCellHeight;    fixedCellHeight = h;    firePropertyChange("fixedCellWidth", old, h);  }  /**   * Gets the value of the {@link #fixedCellWidth} property. This property   * may be <code>-1</code> to indicate that no cell width has been   * set. This property is also set implicitly when the {@link   * #prototypeCellValue} property is set.   *   * @return The current value of the property    *    * @see #setFixedCellWidth   * @see #setPrototypeCellValue   */  public int getFixedCellWidth()  {    return fixedCellWidth;  }  /**   * Sets the value of the {@link #fixedCellWidth} property. This property   * may be <code>-1</code> to indicate that no cell width has been   * set. This property is also set implicitly when the {@link   * #prototypeCellValue} property is set, but setting it explicitly   * overrides the width computed from {@link #prototypeCellValue}.   *   * @see #getFixedCellHeight   * @see #getPrototypeCellValue   */  public void setFixedCellWidth(int w)  {    if (fixedCellWidth == w)      return;        int old = fixedCellWidth;    fixedCellWidth = w;    firePropertyChange("fixedCellWidth", old, w);  }  /**    * Gets the value of the {@link #visibleRowCount} property.    *   * @return the current value of the property.   */  public int getVisibleRowCount()  {    return visibleRowCount;  }  /**   * Sets the value of the {@link #visibleRowCount} property.    *   * @param visibleRowCount The new property value   */  public void setVisibleRowCount(int vc)  {    visibleRowCount = vc;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?