📄 jlist.java
字号:
return new AccessibleJListChild(JList.this, n); } } 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 #getPreferredScrollableViewportSize} property accordingly. The actual * number of displayed rows, when the list is placed in a real {@link * JViewport} 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 vc The new property value */ public void setVisibleRowCount(int vc) { visibleRowCount = vc; 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -