jlist.java

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

JAVA
1,327
字号
    this.model = model;        if (this.model != null)      this.model.addListDataListener(listListener);        firePropertyChange("model", old, model);    revalidate();    repaint();  }  public ListSelectionModel getSelectionModel()  {    return selectionModel;  }  /**   * Sets the value of the {@link #selectionModel} property. The list's   * {@link #listListener} is unsubscribed from the existing selection   * model, if it exists, and re-subscribed to the new selection model.   *   * @param model The new property value   */  public void setSelectionModel(ListSelectionModel model)  {    if (selectionModel == model)      return;        if (selectionModel != null)      selectionModel.removeListSelectionListener(listListener);        ListSelectionModel old = selectionModel;    selectionModel = model;        if (selectionModel != null)      selectionModel.addListSelectionListener(listListener);        firePropertyChange("selectionModel", old, model);    revalidate();    repaint();  }  /**   * Gets the value of the UI property.   *   * @return The current property value   */  public ListUI getUI()  {    return (ListUI) ui;  }  /**   * Sets the value of the UI property.   *   * @param ui The new property value   */  public void setUI(ListUI ui)  {    super.setUI(ui);  }  /**   * Calls {@link #setUI} with the {@link ListUI} subclass   * returned from calling {@link UIManager#getUI}.   */  public void updateUI()  {    setUI((ListUI) UIManager.getUI(this));  }  /**   * Return the class identifier for the list's UI property.  This should   * be the constant string <code>"ListUI"</code>, and map to an   * appropriate UI class in the {@link UIManager}.   *   * @return The class identifier   */  public String getUIClassID()  {    return "ListUI";  }  /**   * Returns the current value of the {@link #prototypeCellValue}   * property. 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.   *   * @return The current prototype cell value   * @see #setPrototypeCellValue   */  public Object getPrototypeCellValue()  {    return prototypeCellValue;  }  /**   * <p>Set the {@link #prototypeCellValue} property. 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>   *   * @param obj The new prototype cell value   * @see #getPrototypeCellValue   */  public void setPrototypeCellValue(Object obj)  {    if (prototypeCellValue == obj)      return;    Object old = prototypeCellValue;    Component comp = getCellRenderer()      .getListCellRendererComponent(this, obj, 0, false, false);     Dimension d = comp.getPreferredSize();    fixedCellWidth = d.width;    fixedCellHeight = d.height;    prototypeCellValue = obj;    firePropertyChange("prototypeCellValue", old, obj);  }  public AccessibleContext getAccessibleContext()  {    return null;  }  /**   * Returns a size indicating how much space this list would like to   * consume, when contained in a scrollable viewport. This is part of the   * {@link Scrollable} interface, which interacts with {@link   * ScrollPaneLayout} and {@link Viewport} to define scrollable objects.   *   * @return The preferred size   */  public Dimension getPreferredScrollableViewportSize()  {    int vis = getVisibleRowCount();    int nrows = getModel() == null ? 0 : getModel().getSize();    // FIXME: this is a somewhat arbitrary default, but.. ?    Dimension single = new Dimension(10, 10);;    Rectangle bounds = null;    if (vis > nrows)      {        if (fixedCellWidth != -1 &&             fixedCellHeight != -1)          {            single = new Dimension(fixedCellWidth, fixedCellHeight);          }        else if (nrows != 0 && getUI() != null)          {            Rectangle tmp = getUI().getCellBounds(this, 0, 0);            if (tmp != null)              single = tmp.getSize();          }      }    else if (getUI() != null)      {        return getUI().getCellBounds(this, 0, vis - 1).getSize();      }    return new Dimension(single.width, single.height * vis);  }  /**   * <p>Return the number of pixels the list must scroll in order to move a   * "unit" of the list into the provided visible rectangle. When the   * provided direction is positive, the call describes a "downwards"   * scroll, which will be exposing a cell at a <em>greater</em> index in   * the list than those elements currently showing. Then the provided   * direction is negative, the call describes an "upwards" scroll, which   * will be exposing a cell at a <em>lesser</em> index in the list than   * those elements currently showing.</p>   *   * <p>If the provided orientation is <code>HORIZONTAL</code>, the above   * comments refer to "rightwards" for positive direction, and "leftwards"   * for negative.</p>   *    *   * @param visibleRect The rectangle to scroll an element into   * @param orientation One of the numeric consants <code>VERTICAL</code>   * or <code>HORIZONTAL</code>   * @param direction An integer indicating the scroll direction: positive means   * forwards (down, right), negative means backwards (up, left)   *   * @return The scrollable unit increment, in pixels   */  public int getScrollableUnitIncrement(Rectangle visibleRect,                                        int orientation, int direction)  {    ListUI lui = this.getUI();    if (orientation == SwingConstants.VERTICAL)      {        if (direction > 0)          {            // Scrolling down            Point bottomLeft = new Point(visibleRect.x,                                         visibleRect.y + visibleRect.height);            int curIdx = lui.locationToIndex(this, bottomLeft);            Rectangle curBounds = lui.getCellBounds(this, curIdx, curIdx);            if (curBounds.y + curBounds.height == bottomLeft.y)              {                // we are at the exact bottom of the current cell, so we                 // are being asked to scroll to the end of the next one                if (curIdx + 1 < model.getSize())                  {                    // there *is* a next item in the list                    Rectangle nxtBounds = lui.getCellBounds(this, curIdx + 1, curIdx + 1);                    return nxtBounds.height;                  }                else                  {                    // no next item, no advance possible                    return 0;                  }              }            else              {                // we are part way through an existing cell, so we are being                // asked to scroll to the bottom of it                return (curBounds.y + curBounds.height) - bottomLeft.y;              }		                }        else          {            // scrolling up            Point topLeft = new Point(visibleRect.x, visibleRect.y);            int curIdx = lui.locationToIndex(this, topLeft);            Rectangle curBounds = lui.getCellBounds(this, curIdx, curIdx);            if (curBounds.y == topLeft.y)              {                // we are at the exact top of the current cell, so we                 // are being asked to scroll to the top of the previous one                if (curIdx > 0)                  {                    // there *is* a previous item in the list                    Rectangle nxtBounds = lui.getCellBounds(this, curIdx - 1, curIdx - 1);                    return -nxtBounds.height;                  }                else                  {                    // no previous item, no advance possible                    return 0;                  }              }            else              {                // we are part way through an existing cell, so we are being                // asked to scroll to the top of it                return curBounds.y - topLeft.y;              }		                }      }    // FIXME: handle horizontal scrolling (also wrapping?)    return 1;  }  /**   * <p>Return the number of pixels the list must scroll in order to move a   * "block" of the list into the provided visible rectangle. When the   * provided direction is positive, the call describes a "downwards"   * scroll, which will be exposing a cell at a <em>greater</em> index in   * the list than those elements currently showing. Then the provided   * direction is negative, the call describes an "upwards" scroll, which   * will be exposing a cell at a <em>lesser</em> index in the list than   * those elements currently showing.</p>   *   * <p>If the provided orientation is <code>HORIZONTAL</code>, the above   * comments refer to "rightwards" for positive direction, and "leftwards"   * for negative.</p>   *    *   * @param visibleRect The rectangle to scroll an element into   * @param orientation One of the numeric consants <code>VERTICAL</code>   * or <code>HORIZONTAL</code>   * @param direction An integer indicating the scroll direction: positive means   * forwards (down, right), negative means backwards (up, left)   *   * @return The scrollable unit increment, in pixels   */  public int getScrollableBlockIncrement(Rectangle visibleRect,                                         int orientation, int direction)  {      if (orientation == VERTICAL)	  return visibleRect.height * direction;      else	  return visibleRect.width * direction;  }  /**   * Gets the value of the {@link #scrollableTracksViewportWidth} property.   *   * @return <code>true</code> if the viewport is larger (horizontally)   * than the list and the list should be expanded to fit the viewport;   * <code>false</code> if the viewport is smaller than the list and the   * list should scroll (horizontally) within the viewport   */  public boolean getScrollableTracksViewportWidth()  {    return false;  }  /**   * Gets the value of the {@link #scrollableTracksViewportWidth} property.   *   * @return <code>true</code> if the viewport is larger (vertically)   * than the list and the list should be expanded to fit the viewport;   * <code>false</code> if the viewport is smaller than the list and the   * list should scroll (vertically) within the viewport   */  public boolean getScrollableTracksViewportHeight()  {    return false;  }  public int getAnchorSelectionIndex()  {    return selectionModel.getAnchorSelectionIndex();  }  public int getLeadSelectionIndex()  {    return selectionModel.getLeadSelectionIndex();  }  public int getMinSelectionIndex()  {    return selectionModel.getMaxSelectionIndex();  }  public int getMaxSelectionIndex()  {    return selectionModel.getMaxSelectionIndex();  }  public void clearSelection()  {    selectionModel.clearSelection();  }  public void setSelectionInterval(int anchor, int lead)  {    selectionModel.setSelectionInterval(anchor, lead);  }  public void addSelectionInterval(int anchor, int lead)  {    selectionModel.addSelectionInterval(anchor, lead);  }  public void removeSelectionInterval(int index0, int index1)  {    selectionModel.removeSelectionInterval(index0, index1);  }  /**   * Returns the value of the <code>valueIsAdjusting</code> property.   *   * @return the value   */  public boolean getValueIsAdjusting()  {    return valueIsAdjusting;  }  /**   * Sets the <code>valueIsAdjusting</code> property.   *   * @param isAdjusting the new value   */  public void setValueIsAdjusting(boolean isAdjusting)  {    valueIsAdjusting = isAdjusting;  }  /**   * Return the value of the <code>dragEnabled</code> property.   *   * @return the value   *    * @since 1.4   */  public boolean getDragEnabled()  {    return dragEnabled;  }  /**   * Set the <code>dragEnabled</code> property.   *   * @param enabled new value   *    * @since 1.4   */  public void setDragEnabled(boolean enabled)  {    dragEnabled = enabled;  }  /**   * Returns the layout orientation.   *   * @return the orientation, one of <code>JList.VERTICAL</code>,   * <code>JList.VERTICAL_WRAP</code> and <code>JList.HORIZONTAL_WRAP</code>   *   * @since 1.4   */  public int getLayoutOrientation()  {    return layoutOrientation;  }  /**   * Sets the layout orientation.   *   * @param orientation the orientation to set, one of <code>JList.VERTICAL</code>,   * <code>JList.VERTICAL_WRAP</code> and <code>JList.HORIZONTAL_WRAP</code>   *   * @since 1.4   */  public void setLayoutOrientation(int orientation)  {    if (layoutOrientation == orientation)      return;    int old = layoutOrientation;    layoutOrientation = orientation;    firePropertyChange("layoutOrientation", old, orientation);  }}

⌨️ 快捷键说明

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