📄 jlist.java
字号:
* @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 * visible rectangle of this list, depending on the {@link * Component#getComponentOrientation} 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() - 1, 0); return getUI().locationToIndex(this, r.getLocation()); } /** * Returns index of the cell to which specified location is closest to. If * the location is outside the bounds of the list, then the greatest index * in the list model is returned. If the list model is empty, then * <code>-1</code> is returned. * * @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) { return getUI().indexToLocation(this, index); } /** * Returns the list index of the lower right or lower left corner of the * visible rectangle of this list, depending on the {@link * Component#getComponentOrientation} property. * * @return The index of the last 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() - 1); if (or == ComponentOrientation.LEFT_TO_RIGHT) r.translate((int) r.getWidth() - 1, 0); if (getUI().locationToIndex(this, r.getLocation()) == -1 && indexToLocation(getModel().getSize() - 1).y < r.y) return getModel().getSize() - 1; 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 #getSelectedValues} 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 #setSelectedValue */ 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 #getCellRenderer} 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 model (<code>null</code> not permitted). * * @throws IllegalArgumentException if <code>model</code> is * <code>null</code>. */ public void setModel(ListModel model) { if (model == null) throw new IllegalArgumentException("Null 'model' argument."); if (this.model == model) return; if (this.model != null) this.model.removeListDataListener(listListener); ListModel old = this.model; 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -