📄 choice.java
字号:
insert(String item, int index){ if (index < 0) throw new IllegalArgumentException ("index may not be less then 0"); if (index > getItemCount ()) index = getItemCount (); pItems.insertElementAt(item, index); if (peer != null) { ChoicePeer cp = (ChoicePeer) peer; cp.add (item, index); } else if (selectedIndex == -1 || selectedIndex >= index) select(0);}/*************************************************************************//** * Removes the specified item from the choice box. * * @param item The item to remove. * * @exception IllegalArgumentException If the specified item doesn't exist. */public synchronized voidremove(String item){ int index = pItems.indexOf(item); if (index == -1) throw new IllegalArgumentException ("item \"" + item + "\" not found in Choice"); remove(index);}/*************************************************************************//** * Removes the item at the specified index from the choice box. * * @param index The index of the item to remove. * * @exception IndexOutOfBoundsException If the index is not valid. */public synchronized voidremove(int index){ if ((index < 0) || (index > getItemCount())) throw new IllegalArgumentException("Bad index: " + index); pItems.removeElementAt(index); if (peer != null) { ChoicePeer cp = (ChoicePeer) peer; cp.remove (index); } else { if (getItemCount() == 0) selectedIndex = -1; else if (index == selectedIndex) select(0); } if (selectedIndex > index) --selectedIndex;}/*************************************************************************//** * Removes all of the objects from this choice box. */public synchronized voidremoveAll(){ if (getItemCount() <= 0) return; pItems.removeAllElements (); if (peer != null) { ChoicePeer cp = (ChoicePeer) peer; cp.removeAll (); } selectedIndex = -1;}/*************************************************************************//** * Returns the currently selected item, or null if no item is * selected. * * @return The currently selected item. */public synchronized StringgetSelectedItem(){ return (selectedIndex == -1 ? null : ((String)pItems.elementAt(selectedIndex)));}/*************************************************************************//** * Returns an array with one row containing the selected item. * * @return An array containing the selected item. */public synchronized Object[]getSelectedObjects(){ if (selectedIndex == -1) return null; Object[] objs = new Object[1]; objs[0] = pItems.elementAt(selectedIndex); return(objs);}/*************************************************************************//** * Returns the index of the selected item. * * @return The index of the selected item. */public intgetSelectedIndex(){ return(selectedIndex);}/*************************************************************************//** * Forces the item at the specified index to be selected. * * @param index The index of the row to make selected. * * @exception IllegalArgumentException If the specified index is invalid. */public synchronized voidselect(int index){ if ((index < 0) || (index >= getItemCount())) throw new IllegalArgumentException("Bad index: " + index); if (pItems.size() > 0) { selectedIndex = index; ChoicePeer cp = (ChoicePeer) peer; if (cp != null) { cp.select(index); } }}/*************************************************************************//** * Forces the named item to be selected. * * @param item The item to be selected. * * @exception IllegalArgumentException If the specified item does not exist. */public synchronized voidselect(String item){ int index = pItems.indexOf(item); if (index >= 0) select(index);}/*************************************************************************//** * Creates the native peer for this object. */public voidaddNotify(){ if (peer == null) peer = getToolkit ().createChoice (this); super.addNotify ();}/*************************************************************************//** * Adds the specified listener 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 listener from the list of registered listeners for * this object. * * @param listener The listener to remove. */public synchronized voidremoveItemListener(ItemListener listener){ item_listeners = AWTEventMulticaster.remove(item_listeners, listener);}/*************************************************************************//** * Processes this event by invoking <code>processItemEvent()</code> if the * event is an instance of <code>ItemEvent</code>, otherwise the event * is passed to the superclass. * * @param event The event to process. */protected voidprocessEvent(AWTEvent event){ if (event instanceof ItemEvent) processItemEvent((ItemEvent)event); else super.processEvent(event);}/*************************************************************************//** * Processes item event by dispatching to any registered listeners. * * @param event The event to process. */protected voidprocessItemEvent(ItemEvent event){ int index = pItems.indexOf((String) event.getItem()); // Don't call back into the peers when selecting index here if (event.getStateChange() == ItemEvent.SELECTED) this.selectedIndex = index; if (item_listeners != null) item_listeners.itemStateChanged(event);}/*************************************************************************//** * Returns a debugging string for this object. * * @return A debugging string for this object. */protected StringparamString(){ return ("selectedIndex=" + selectedIndex + "," + super.paramString());} /** * Returns an array of all the objects currently registered as FooListeners * upon this Choice. FooListeners are registered using the addFooListener * method. * * @exception ClassCastException If listenerType doesn't specify a class or * interface that implements java.util.EventListener. * * @since 1.3 */ public EventListener[] getListeners (Class listenerType) { if (listenerType == ItemListener.class) return AWTEventMulticaster.getListeners (item_listeners, listenerType); return super.getListeners (listenerType); } /** * Returns all registered item listeners. * * @since 1.4 */ public ItemListener[] getItemListeners () { return (ItemListener[]) getListeners (ItemListener.class); } /** * Gets the AccessibleContext associated with this <code>Choice</code>. * The context is created, if necessary. * * @return the associated context */ public AccessibleContext getAccessibleContext() { /* Create the context if this is the first request */ if (accessibleContext == null) accessibleContext = new AccessibleAWTChoice(); return accessibleContext; }} // class Choice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -