📄 list.java
字号:
/** * Deletes the item at the specified index. * * @param index The index of the item to delete. * * @exception IllegalArgumentException If the index is not valid */public voiddelItem(int index) throws IllegalArgumentException{ remove(index);}/*************************************************************************//** * Deletes the item at the specified index. * * @param index The index of the item to delete. * * @exception IllegalArgumentException If the index is not valid */public voidremove(int index) throws IllegalArgumentException{ items.removeElementAt (index); if (peer != null) { ListPeer l = (ListPeer) peer; l.delItems (index, index); }}/*************************************************************************//** * Deletes all items in the specified index range. * * @param start The beginning index of the range to delete. * @param end The ending index of the range to delete. * * @exception IllegalArgumentException If the indexes are not valid * * @deprecated This method is deprecated for some unknown reason. */public synchronized voiddelItems(int start, int end) throws IllegalArgumentException{ if ((start < 0) || (start >= items.size())) throw new IllegalArgumentException("Bad list start index value: " + start); if ((start < 0) || (start >= items.size())) throw new IllegalArgumentException("Bad list start index value: " + start); if (start > end) throw new IllegalArgumentException("Start is greater than end!"); // We must run the loop in reverse direction. for (int i = end; i >= start; --i) items.removeElementAt (i); if (peer != null) { ListPeer l = (ListPeer) peer; l.delItems (start, end); }}/*************************************************************************//** * Deletes the first occurrence of the specified item from the list. * * @param item The item to delete. * * @exception IllegalArgumentException If the specified item does not exist. */public synchronized voidremove(String item) throws IllegalArgumentException{ int index = items.indexOf(item); if (index == -1) throw new IllegalArgumentException("List element to delete not found"); remove(index);}/*************************************************************************//** * Deletes all of the items from the list. */public synchronized voidremoveAll(){ items.clear(); if (peer != null) { ListPeer l = (ListPeer) peer; l.removeAll (); }}/*************************************************************************//** * Deletes all of the items from the list. * * @deprecated This method is deprecated in favor of <code>removeAll()</code>. */public voidclear(){ removeAll();}/*************************************************************************//** * Replaces the item at the specified index with the specified item. * * @param item The new item value. * @param index The index of the item to replace. * * @exception IllegalArgumentException If the index is not valid. */public synchronized voidreplaceItem(String item, int index) throws IllegalArgumentException{ remove(index); addItem(item, index);}/*************************************************************************//** * Returns the index of the currently selected item. -1 will be returned * if there are no selected rows or if there are multiple selected rows. * * @return The index of the selected row. */public synchronized intgetSelectedIndex(){ if (peer != null) { ListPeer l = (ListPeer) peer; selected = l.getSelectedIndexes (); } if (selected == null || selected.length > 1) return -1; return selected[0];}/*************************************************************************//** * Returns an array containing the indexes of the rows that are * currently selected. * * @return A list of indexes of selected rows. */public synchronized int[]getSelectedIndexes(){ if (peer != null) { ListPeer l = (ListPeer) peer; selected = l.getSelectedIndexes (); } return selected;}/*************************************************************************//** * Returns the item that is currently selected, or <code>null</code> if there * is no item selected. FIXME: What happens if multiple items selected? * * @return The selected item, or <code>null</code> if there is no * selected item. */public synchronized StringgetSelectedItem(){ int index = getSelectedIndex(); if (index == -1) return(null); return((String)items.elementAt(index));}/*************************************************************************//** * Returns the list of items that are currently selected in this list. * * @return The list of currently selected items. */public synchronized String[]getSelectedItems(){ int[] indexes = getSelectedIndexes(); if (indexes == null) return(new String[0]); String[] retvals = new String[indexes.length]; if (retvals.length > 0) for (int i = 0 ; i < retvals.length; i++) retvals[i] = (String)items.elementAt(indexes[i]); return(retvals);}/*************************************************************************//** * Returns the list of items that are currently selected in this list as * an array of type <code>Object[]</code> instead of <code>String[]</code>. * * @return The list of currently selected items. */public synchronized Object[]getSelectedObjects(){ int[] indexes = getSelectedIndexes(); if (indexes == null) return(new Object[0]); Object[] retvals = new Object[indexes.length]; if (retvals.length > 0) for (int i = 0 ; i < retvals.length; i++) retvals[i] = items.elementAt(indexes[i]); return(retvals);}/*************************************************************************//** * Tests whether or not the specified index is selected. * * @param index The index to test. * * @return <code>true</code> if the index is selected, <code>false</code> * otherwise. */public booleanisIndexSelected(int index){ int[] indexes = getSelectedIndexes(); for (int i = 0; i < indexes.length; i++) if (indexes[i] == index) return(true); return(false);}/*************************************************************************//** * Tests whether or not the specified index is selected. * * @param index The index to test. * * @return <code>true</code> if the index is selected, <code>false</code> * otherwise. * * @deprecated This method is deprecated in favor of * <code>isIndexSelected(int)</code>. */public booleanisSelected(int index){ return(isIndexSelected(index));}/*************************************************************************//** * This method ensures that the item at the specified index is visible. * * @exception IllegalArgumentException If the specified index is out of * range. */public synchronized voidmakeVisible(int index) throws IllegalArgumentException{ if ((index < 0) || (index >= items.size())) throw new IllegalArgumentException("Bad list index: " + index); visibleIndex = index; if (peer != null) { ListPeer l = (ListPeer) peer; l.makeVisible (index); }}/*************************************************************************//** * Returns the index of the last item that was made visible via the * <code>makeVisible()</code> method. * * @return The index of the last item made visible via the * <code>makeVisible()</code> method. */public intgetVisibleIndex(){ return(visibleIndex);}/*************************************************************************//** * Makes the item at the specified index selected. * * @param index The index of the item to select. */public synchronized voidselect(int index){ ListPeer lp = (ListPeer)getPeer(); if (lp != null) lp.select(index);}/*************************************************************************//** * Makes the item at the specified index not selected. * * @param index The index of the item to unselect. */public synchronized voiddeselect(int index){ ListPeer lp = (ListPeer)getPeer(); if (lp != null) lp.deselect(index);}/*************************************************************************//** * Notifies this object to create its native peer. */public voidaddNotify(){ if (peer == null) peer = getToolkit ().createList (this); super.addNotify ();}/*************************************************************************//** * Notifies this object to destroy its native peer. */public voidremoveNotify(){ super.removeNotify();}/*************************************************************************//** * Adds the specified <code>ActionListener</code> to the list of * registered listeners for this object. * * @param listener The listener to add. */public synchronized voidaddActionListener(ActionListener listener){ action_listeners = AWTEventMulticaster.add(action_listeners, listener);}/*************************************************************************//** * Removes the specified <code>ActionListener</code> from the list of * registers listeners for this object. * * @param listener The listener to remove. */public synchronized voidremoveActionListener(ActionListener listener){ action_listeners = AWTEventMulticaster.remove(action_listeners, listener);}/*************************************************************************//** * Adds the specified <code>ItemListener</code> to the list of * registered listeners for this object. * * @param listener The listener to add. */public synchronized voidaddItemListener(ItemListener listener){ item_listeners = AWTEventMulticaster.add(item_listeners, listener);}/*************************************************************************//** * Removes the specified <code>ItemListener</code> from the list of * registers listeners for this object. * * @param listener The listener to remove. */public synchronized voidremoveItemListener(ItemListener listener){ item_listeners = AWTEventMulticaster.remove(item_listeners, listener);}/*************************************************************************//** * Processes the specified event for this object. If the event is an * instance of <code>ActionEvent</code> then the * <code>processActionEvent()</code> method is called. Similarly, if the * even is an instance of <code>ItemEvent</code> then the * <code>processItemEvent()</code> method is called. Otherwise the * superclass method is called to process this event. * * @param event The event to process. */protected voidprocessEvent(AWTEvent event){ if (event instanceof ActionEvent) processActionEvent((ActionEvent)event); else if (event instanceof ItemEvent) processItemEvent((ItemEvent)event); else super.processEvent(event);}/*************************************************************************//** * This method processes the specified event by dispatching it to any * registered listeners. Note that this method will only get called if * action events are enabled. This will happen automatically if any * listeners are added, or it can be done "manually" by calling * the <code>enableEvents()</code> method. * * @param event The event to process. */protected void processActionEvent(ActionEvent event){ if (action_listeners != null) action_listeners.actionPerformed(event);}/*************************************************************************//** * This method processes the specified event by dispatching it to any * registered listeners. Note that this method will only get called if * item events are enabled. This will happen automatically if any * listeners are added, or it can be done "manually" by calling * the <code>enableEvents()</code> method. * * @param event The event to process. */protected void processItemEvent(ItemEvent event){ if (item_listeners != null) item_listeners.itemStateChanged(event);}voiddispatchEventImpl(AWTEvent e){ if (e.id <= ItemEvent.ITEM_LAST && e.id >= ItemEvent.ITEM_FIRST && (item_listeners != null || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0)) processEvent(e); else if (e.id <= ActionEvent.ACTION_LAST && e.id >= ActionEvent.ACTION_FIRST && (action_listeners != null || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0)) processEvent(e); else super.dispatchEventImpl(e);}/*************************************************************************//** * Returns a debugging string for this object. * * @return A debugging string for this object. */protected StringparamString(){ return "multiple=" + multipleMode + ",rows=" + rows + super.paramString();}} // class List
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -