jlist.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,327 行 · 第 1/3 页
JAVA
1,327 行
revalidate(); repaint(); } /** * Adds a {@link ListSelectionListener} to the listener list for this * list. The listener will be called back with a {@link * ListSelectionEvent} any time the list's {@link #selectionModel} * property changes. The source of such events will be the JList, * not the selection model. * * @param listener The new listener to add */ public void addListSelectionListener(ListSelectionListener listener) { listenerList.add (ListSelectionListener.class, listener); } /** * Removes a {@link ListSelectionListener} from the listener list for * this list. The listener will no longer be called when the list's * {@link #selectionModel} changes. * * @param listener The listener to remove */ public void removeListSelectionListener(ListSelectionListener listener) { listenerList.remove(ListSelectionListener.class, listener); } /** * Returns an array of all ListSelectionListeners subscribed to this * list. * * @return The current subscribed listeners * * @since 1.4 */ public ListSelectionListener[] getListSelectionListeners() { return (ListSelectionListener[]) getListeners(ListSelectionListener.class); } public int getSelectionMode() { return selectionModel.getSelectionMode(); } /** * Sets the list's "selectionMode" property, which simply mirrors the * same property on the list's {@link #selectionModel} property. This * property should be one of the integer constants * <code>SINGLE_SELECTION</code>, <code>SINGLE_INTERVAL_SELECTION</code>, * or <code>MULTIPLE_INTERVAL_SELECTION</code> from the {@link * ListSelectionModel} interface. * * @param a The new selection mode */ public void setSelectionMode(int a) { selectionModel.setSelectionMode(a); } /** * Adds the interval <code>[a,a]</code> to the set of selections managed * by this list's {@link #selectionModel} property. Depending on the * selection mode, this may cause existing selections to become invalid, * or may simply expand the set of selections. * * @param a A number in the half-open range <code>[0, x)</code> where * <code>x = getModel.getSize()</code>, indicating the index of an * element in the list to select. * * @see #setSelectionMode * @see #selectionModel */ public void setSelectedIndex(int a) { selectionModel.setSelectionInterval(a, a); } /** * For each element <code>a[i]</code> of the provided array * <code>a</code>, calls {@link #setSelectedIndex} on <code>a[i]</code>. * * @see #setSelectionMode * @see #selectionModel */ public void setSelectedIndices(int [] a) { for (int i = 0; i < a.length; ++i) setSelectedIndex(a[i]); } /** * Returns the minimum index of an element in the list which is currently * selected. * * @return A number in the half-open range <code>[0, x)</code> where * <code>x = getModel.getSize()</code>, indicating the minimum index of * an element in the list for which the element is selected, or * <code>-1</code> if no elements are selected */ public int getSelectedIndex() { return selectionModel.getMinSelectionIndex(); } /** * Returns <code>true</code> if the model's selection is empty, otherwise * <code>false</code>. * * @return The return value of {@link ListSelectionModel#isSelectionEmpty} */ public boolean isSelectionEmpty() { return selectionModel.isSelectionEmpty(); } /** * Returns the list index of the upper left or upper right corner of the * {@link #visibleRect} property, depending on the {@link * #componentOrientation} property. * * @return The index of the first visible list cell, or <code>-1</code> * if none is visible. */ public int getFirstVisibleIndex() { ComponentOrientation or = getComponentOrientation(); Rectangle r = getVisibleRect(); if (or == ComponentOrientation.RIGHT_TO_LEFT) r.translate((int) r.getWidth(), 0); return getUI().locationToIndex(this, r.getLocation()); } /** * Returns index of the cell to which specified location is closest to * @param location for which to look for in the list * * @return index of the cell to which specified location is closest to. */ public int locationToIndex(Point location) { return getUI().locationToIndex(this, location); } /** * Returns location of the cell located at the specified index in the list. * @param index of the cell for which location will be determined * * @return location of the cell located at the specified index in the list. */ public Point indexToLocation(int index){ //FIXME: Need to implement. return null; } /** * Returns the list index of the lower right or lower left corner of the * {@link #visibleRect} property, depending on the {@link * #componentOrientation} property. * * @return The index of the first visible list cell, or <code>-1</code> * if none is visible. */ public int getLastVisibleIndex() { ComponentOrientation or = getComponentOrientation(); Rectangle r = getVisibleRect(); r.translate(0, (int) r.getHeight()); if (or == ComponentOrientation.LEFT_TO_RIGHT) r.translate((int) r.getWidth(), 0); return getUI().locationToIndex(this, r.getLocation()); } /** * Returns the indices of values in the {@link #model} property which are * selected. * * @return An array of model indices, each of which is selected according * to the {@link #selection} property */ public int[] getSelectedIndices() { int lo, hi, n, i, j; if (selectionModel.isSelectionEmpty()) return new int[0]; lo = selectionModel.getMinSelectionIndex(); hi = selectionModel.getMaxSelectionIndex(); n = 0; for (i = lo; i < hi; ++i) if (selectionModel.isSelectedIndex(i)) n++; int [] v = new int[n]; j = 0; for (i = lo; i < hi; ++i) if (selectionModel.isSelectedIndex(i)) v[j++] = i; return v; } /** * Indicates whether the list element at a given index value is * currently selected. * * @param a The index to check * @return <code>true</code> if <code>a</code> is the index of a selected * list element */ public boolean isSelectedIndex(int a) { return selectionModel.isSelectedIndex(a); } /** * Returns the first value in the list's {@link #model} property which is * selected, according to the list's {@link #selectionModel} property. * This is equivalent to calling * <code>getModel()getElementAt(getSelectedIndex())</code>, with a check * for the special index value of <code>-1</code> which returns null * <code>null</code>. * * @return The first selected element, or <code>null</code> if no element * is selected. * * @see getSelectedValues */ public Object getSelectedValue() { int index = getSelectedIndex(); if (index == -1) return null; return getModel().getElementAt(index); } /** * Returns all the values in the list's {@link #model} property which * are selected, according to the list's {@link #selectionModel} property. * * @return An array containing all the selected values * * @see getSelectedValue */ public Object[] getSelectedValues() { int [] idx = getSelectedIndices(); Object [] v = new Object[idx.length]; for (int i = 0; i < idx.length; ++i) v[i] = getModel().getElementAt(i); return v; } /** * Gets the value of the {@link #selectionBackground} property. * * @return The current value of the property */ public Color getSelectionBackground() { return selectionBackground; } /** * Sets the value of the {@link #selectionBackground} property. * * @param c The new value of the property */ public void setSelectionBackground(Color c) { if (selectionBackground == c) return; Color old = selectionBackground; selectionBackground = c; firePropertyChange("selectionBackground", old, c); repaint(); } /** * Gets the value of the {@link #selectionForeground} property. * * @return The current value of the property */ public Color getSelectionForeground() { return selectionForeground; } /** * Sets the value of the {@link #selectionForeground} property. * * @param c The new value of the property */ public void setSelectionForeground(Color c) { if (selectionForeground == c) return; Color old = selectionForeground; selectionForeground = c; firePropertyChange("selectionForeground", old, c); } /** * Sets the selection to cover only the specified value, if it * exists in the model. * * @param obj The object to select * @param scroll Whether to scroll the list to make the newly selected * value visible * * @see #ensureIndexIsVisible */ public void setSelectedValue(Object obj, boolean scroll) { for (int i = 0; i < model.getSize(); ++i) { if (model.getElementAt(i).equals(obj)) { setSelectedIndex(i); if (scroll) ensureIndexIsVisible(i); break; } } } /** * Scrolls this list to make the specified cell visible. This * only works if the list is contained within a viewport. * * @param i The list index to make visible * * @see JComponent#scrollRectToVisible */ public void ensureIndexIsVisible(int i) { scrollRectToVisible(getUI().getCellBounds(this, i, i)); } /** * Sets the {@link #model} property of the list to a new anonymous * {@link AbstractListModel} subclass which accesses the provided Object * array directly. * * @param listData The object array to build a new list model on * @see #setModel */ public void setListData(final Object[] listData) { setModel(new AbstractListModel() { public int getSize() { return listData.length; } public Object getElementAt(int i) { return listData[i]; } }); } /** * Sets the {@link #model} property of the list to a new anonymous {@link * AbstractListModel} subclass which accesses the provided vector * directly. * * @param listData The object array to build a new list model on * @see #setModel */ public void setListData(final Vector listData) { setModel(new AbstractListModel() { public int getSize() { return listData.size(); } public Object getElementAt(int i) { return listData.elementAt(i); } }); } /** * Gets the value of the {@link #cellRenderer} property. * * @return The current value of the property */ public ListCellRenderer getCellRenderer() { return cellRenderer; } /** * Sets the value of the {@link #celLRenderer} property. * * @param renderer The new property value */ public void setCellRenderer(ListCellRenderer renderer) { if (cellRenderer == renderer) return; ListCellRenderer old = cellRenderer; cellRenderer = renderer; firePropertyChange("cellRenderer", old, renderer); revalidate(); repaint(); } /** * Gets the value of the {@link #model} property. * * @return The current value of the property */ public ListModel getModel() { return model; } /** * Sets the value of the {@link #model} property. The list's {@link * #listListener} is unsubscribed from the existing model, if it exists, * and re-subscribed to the new model. * * @param model The new property value */ public void setModel(ListModel model) { if (this.model == model) return; if (this.model != null) this.model.removeListDataListener(listListener); ListModel old = this.model;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?