📄 jlist.java
字号:
} /** * Returns the size of this list child. * * @return the size of this list child */ public Dimension getSize() { Rectangle b = getBounds(); return b.getSize(); } /** * Does nothing since the size cannot be set on list children * individually. * * @param dimension not used here */ public void setSize(Dimension dimension) { // Does nothing since the size cannot be set on list children // individually. } /** * Returns <code>null</code> because list children do not have children * themselves * * @return <code>null</code> */ public Accessible getAccessibleAt(Point point) { return null; } /** * Returns <code>true</code> since list children are focus traversable. * * @return true */ public boolean isFocusTraversable() { // TODO: Is this 100% ok? return true; } /** * Requests focus on the parent list. List children cannot request focus * individually. */ public void requestFocus() { // TODO: Is this 100% ok? parent.requestFocus(); } /** * Adds a focus listener to the parent list. List children do not have * their own focus management. * * @param listener the focus listener to add */ public void addFocusListener(FocusListener listener) { // TODO: Is this 100% ok? parent.addFocusListener(listener); } /** * Removes a focus listener from the parent list. List children do not * have their own focus management. * * @param listener the focus listener to remove */ public void removeFocusListener(FocusListener listener) { // TODO: Is this 100% parent.removeFocusListener(listener); } /** * Returns the accessible role of this list item, which is * {@link AccessibleRole#LABEL}. * * @return {@link AccessibleRole#LABEL} */ public AccessibleRole getAccessibleRole() { return AccessibleRole.LABEL; } /** * Returns the accessible state set of this list item. * * @return the accessible state set of this list item */ public AccessibleStateSet getAccessibleStateSet() { AccessibleStateSet states = new AccessibleStateSet(); if (isVisible()) states.add(AccessibleState.VISIBLE); if (isShowing()) states.add(AccessibleState.SHOWING); if (isFocusTraversable()) states.add(AccessibleState.FOCUSABLE); // TODO: How should the active state be handled? The API docs // suggest that this state is set on the activated list child, // that is the one that is drawn with a box. However, I don't know how // to implement this. // TODO: We set the selectable state here because list children are // selectable. Is there a way to disable single children? if (parent.isEnabled()) states.add(AccessibleState.SELECTABLE); if (parent.isSelectedIndex(listIndex)) states.add(AccessibleState.SELECTED); // TODO: Handle more states here? return states; } /** * Returns the index of this list child within it's parent list. * * @return the index of this list child within it's parent list */ public int getAccessibleIndexInParent() { return listIndex; } /** * Returns <code>0</code> since list children don't have children * themselves. * * @return <code>0</code> */ public int getAccessibleChildrenCount() { return 0; } /** * Returns <code>null</code> since list children don't have children * themselves. * * @return <code>null</code> */ public Accessible getAccessibleChild(int i) { return null; } /** * Returns the locale of this component. This call is forwarded to the * parent list since list children don't have a separate locale setting. * * @return the locale of this component */ public Locale getLocale() { return parent.getLocale(); } /** * This method does * nothing, list children are transient accessible objects which means * that they don't fire property change events. * * @param l not used here */ public void addPropertyChangeListener(PropertyChangeListener l) { // Do nothing here. } /** * This method does * nothing, list children are transient accessible objects which means * that they don't fire property change events. * * @param l not used here */ public void removePropertyChangeListener(PropertyChangeListener l) { // Do nothing here. } // TODO: Implement the remaining methods of this class. } /** * Create a new AccessibleJList. */ public AccessibleJList() { // Nothing to do here. } /** * Returns the number of selected accessible children. * * @return the number of selected accessible children */ public int getAccessibleSelectionCount() { return getSelectedIndices().length; } /** * Returns the n-th selected accessible child. * * @param n the index of the selected child to return * * @return the n-th selected accessible child */ public Accessible getAccessibleSelection(int n) { return new AccessibleJListChild(JList.this, getSelectedIndices()[n]); } /** * Returns <code>true</code> if the n-th child is selected, * <code>false</code> otherwise. * * @param n the index of the child of which the selected state is queried * * @return <code>true</code> if the n-th child is selected, * <code>false</code> otherwise */ public boolean isAccessibleChildSelected(int n) { return isSelectedIndex(n); } /** * Adds the accessible item with the specified index to the selected items. * If multiple selections are supported, the item is added to the selection, * otherwise the item replaces the current selection. * * @param i the index of the item to add to the selection */ public void addAccessibleSelection(int i) { addSelectionInterval(i, i); } /** * Removes the accessible item with the specified index to the selection. * * @param i the index of the item to be removed from the selection */ public void removeAccessibleSelection(int i) { removeSelectionInterval(i, i); } /** * Remove all selection items from the selection. */ public void clearAccessibleSelection() { clearSelection(); } /** * Selects all items if multiple selections are supported. * Otherwise do nothing. */ public void selectAllAccessibleSelection() { addSelectionInterval(0, getModel().getSize()); } /** * Receices notification when the list selection is changed. This method * fires two property change events, the first with * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY} and the second * with {@link AccessibleContext#ACCESSIBLE_SELECTION_PROPERTY}. * * @param event the list selection event */ public void valueChanged(ListSelectionEvent event) { firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE, Boolean.TRUE); firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, Boolean.FALSE, Boolean.TRUE); } /** * Receives notification when items have changed in the * <code>JList</code>. This method fires a property change event with * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}. * * @param event the list data event */ public void contentsChanged(ListDataEvent event) { firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE, Boolean.TRUE); } /** * Receives notification when items are inserted into the * <code>JList</code>. This method fires a property change event with * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}. * * @param event the list data event */ public void intervalAdded(ListDataEvent event) { firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE, Boolean.TRUE); } /** * Receives notification when items are removed from the * <code>JList</code>. This method fires a property change event with * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}. * * @param event the list data event */ public void intervalRemoved(ListDataEvent event) { firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE, Boolean.TRUE); } /** * Receives notification about changes of the <code>JList</code>'s * properties. This is used to re-register this object as listener to * the data model and selection model when the data model or selection model * changes. * * @param e the property change event */ public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if (propertyName.equals("model")) { ListModel oldModel = (ListModel) e.getOldValue(); oldModel.removeListDataListener(this); ListModel newModel = (ListModel) e.getNewValue(); newModel.addListDataListener(this); } else if (propertyName.equals("selectionModel")) { ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue(); oldModel.removeListSelectionListener(this); ListSelectionModel newModel = (ListSelectionModel) e.getNewValue(); oldModel.addListSelectionListener(this); } } /** * Return the state set of the <code>JList</code>. * * @return the state set of the <code>JList</code> */ public AccessibleStateSet getAccessibleStateSet() { // TODO: Figure out if there is possibly more state that must be // handled here. AccessibleStateSet s = super.getAccessibleStateSet(); if (getSelectionMode() != ListSelectionModel.SINGLE_SELECTION) s.add(AccessibleState.MULTISELECTABLE); return s; } /** * Returns the accessible role for <code>JList</code>, * {@link AccessibleRole#LIST}. * * @return the accessible role for <code>JList</code> */ public AccessibleRole getAccessibleRole() { return AccessibleRole.LIST; } /** * Returns the accessible child at the visual location <code>p</code> * (relative to the upper left corner of the <code>JList</code>). If there * is no child at that location, this returns <code>null</code>. * * @param p the screen location for which to return the accessible child * * @return the accessible child at the specified location, or * <code>null</code> if there is no child at that location */ public Accessible getAccessibleAt(Point p) { int childIndex = locationToIndex(p); return getAccessibleChild(childIndex); } /** * Returns the number of accessible children in the <code>JList</code>. * * @return the number of accessible children in the <code>JList</code> */ public int getAccessibleChildrenCount() { return getModel().getSize(); } /** * Returns the n-th accessible child of this <code>JList</code>. This will * be an instance of {@link AccessibleJListChild}. If there is no child * at that index, <code>null</code> is returned. * * @param n the index of the child to return * * @return the n-th accessible child of this <code>JList</code> */ public Accessible getAccessibleChild(int n) { if (getModel().getSize() <= n) return null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -