⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basiccomboboxui.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    // This is a pretty messy way of passing an event through                    // to the root pane.                    JRootPane root = SwingUtilities.getRootPane(comboBox);                    if (root != null) {                        InputMap im = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);                        ActionMap am = root.getActionMap();                        if (im != null && am != null) {                            Object obj = im.get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));                            if (obj != null) {                                Action action = am.get(obj);                                if (action != null) {                                    action.actionPerformed(new ActionEvent(                                     root, e.getID(), e.getActionCommand(),                                     e.getWhen(), e.getModifiers()));                                }                            }                        }                    }                }            }        }        private int getNextIndex(JComboBox comboBox, String key) {            if (key == PAGE_UP) {                int listHeight = comboBox.getMaximumRowCount();                int index = comboBox.getSelectedIndex() - listHeight;                return (index < 0 ? 0: index);            }            else if (key == PAGE_DOWN) {                int listHeight = comboBox.getMaximumRowCount();                int index = comboBox.getSelectedIndex() + listHeight;                int max = comboBox.getItemCount();                return (index < max ? index: max-1);            }            else if (key == HOME) {                return 0;            }            else if (key == END) {                return comboBox.getItemCount() - 1;            }            return comboBox.getSelectedIndex();	}	public boolean isEnabled(Object c) {            if (getName() == HIDE) {                return (c != null && ((JComboBox)c).isPopupVisible());            }            return true;	}    }    //    // end Keyboard Action Management    //===============================    //    // Shared Handler, implements all listeners    //    private class Handler implements ActionListener, FocusListener,                                     KeyListener, LayoutManager,                                     ListDataListener, PropertyChangeListener {        //        // PropertyChangeListener        //        public void propertyChange(PropertyChangeEvent e) {            String propertyName = e.getPropertyName();	    JComboBox comboBox = (JComboBox)e.getSource();            if ( propertyName == "model" ) {                ComboBoxModel newModel = (ComboBoxModel)e.getNewValue();                ComboBoxModel oldModel = (ComboBoxModel)e.getOldValue();                if ( oldModel != null && listDataListener != null ) {                    oldModel.removeListDataListener( listDataListener );                }                if ( newModel != null && listDataListener != null ) {                    newModel.addListDataListener( listDataListener );                }                if ( editor != null ) {                    comboBox.configureEditor( comboBox.getEditor(), comboBox.getSelectedItem() );                 }                isMinimumSizeDirty = true;                isDisplaySizeDirty = true;                comboBox.revalidate();		comboBox.repaint();            }            else if ( propertyName == "editor" && comboBox.isEditable() ) {                addEditor();                comboBox.revalidate();            }            else if ( propertyName == "editable" ) {                if ( comboBox.isEditable() ) {                    comboBox.setRequestFocusEnabled( false );                    addEditor();                } else {                    comboBox.setRequestFocusEnabled( true );                    removeEditor();                }                updateToolTipTextForChildren();                comboBox.revalidate();            }            else if ( propertyName == "enabled" ) {                boolean enabled = comboBox.isEnabled();                if ( editor != null )                    editor.setEnabled(enabled);                if ( arrowButton != null )                    arrowButton.setEnabled(enabled);                comboBox.repaint();            }            else if ( propertyName == "focusable" ) {                boolean focusable = comboBox.isFocusable();                if ( editor != null )                    editor.setFocusable(focusable);                if ( arrowButton != null )                    arrowButton.setFocusable(focusable);                comboBox.repaint();            }            else if ( propertyName == "maximumRowCount" ) {                if ( isPopupVisible( comboBox ) ) {                    setPopupVisible(comboBox, false);                    setPopupVisible(comboBox, true);                }            }            else if ( propertyName == "font" ) {                listBox.setFont( comboBox.getFont() );                if ( editor != null ) {                    editor.setFont( comboBox.getFont() );                }                isMinimumSizeDirty = true;                comboBox.validate();            }            else if ( propertyName == JComponent.TOOL_TIP_TEXT_KEY ) {                updateToolTipTextForChildren();	    }            else if ( propertyName == BasicComboBoxUI.IS_TABLE_CELL_EDITOR ) {                Boolean inTable = (Boolean)e.getNewValue();		isTableCellEditor = inTable.equals(Boolean.TRUE) ? true : false;            } 	    else if (propertyName == "prototypeDisplayValue") {                isMinimumSizeDirty = true;                isDisplaySizeDirty = true;                comboBox.revalidate();            } 	    else if (propertyName == "renderer") {                isMinimumSizeDirty = true;                isDisplaySizeDirty = true;                comboBox.revalidate();            }        }        //        // KeyListener        //        // This listener checks to see if the key event isn't a navigation        // key.  If it finds a key event that wasn't a navigation key it        // dispatches it to JComboBox.selectWithKeyChar() so that it can do        // type-ahead.        public void keyPressed( KeyEvent e ) { 	    if ( isNavigationKey(e.getKeyCode(), e.getModifiers()) ) { 		lastTime = 0L; 	    } else if ( comboBox.isEnabled() && comboBox.getModel().getSize()!=0 &&			isTypeAheadKey( e ) && e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {		time = e.getWhen();		                if ( comboBox.selectWithKeyChar(e.getKeyChar()) ) {                    e.consume();                }            }        }         public void keyTyped(KeyEvent e) {        }        public void keyReleased(KeyEvent e) {        }        private boolean isTypeAheadKey( KeyEvent e ) {            return !e.isAltDown() && !e.isControlDown() && !e.isMetaDown();        }        //        // FocusListener        //        // NOTE: The class is added to both the Editor and ComboBox.        // The combo box listener hides the popup when the focus is lost.        // It also repaints when focus is gained or lost.        public void focusGained( FocusEvent e ) {            ComboBoxEditor comboBoxEditor = comboBox.getEditor();               if ( (comboBoxEditor != null) &&                  (e.getSource() == comboBoxEditor.getEditorComponent()) ) {                 return;            }            hasFocus = true;            comboBox.repaint();	    	    if (comboBox.isEditable() && editor != null) {		editor.requestFocus();	    }        }        public void focusLost( FocusEvent e ) {            ComboBoxEditor editor = comboBox.getEditor();            if ( (editor != null) &&                 (e.getSource() == editor.getEditorComponent()) ) {                Object item = editor.getItem();                Object selectedItem = comboBox.getSelectedItem();                if (!e.isTemporary() && item != null &&                     !item.equals((selectedItem == null) ? "" : selectedItem )) {                    comboBox.actionPerformed                        (new ActionEvent(editor, 0, "",                                      EventQueue.getMostRecentEventTime(), 0));                }            }            hasFocus = false;            if (!e.isTemporary()) {                setPopupVisible(comboBox, false);            }            comboBox.repaint();        }        //        // ListDataListener        //        // This listener watches for changes in the ComboBoxModel        public void contentsChanged( ListDataEvent e ) {	    if ( !(e.getIndex0() == -1 && e.getIndex1() == -1) ) {		isMinimumSizeDirty = true;		comboBox.revalidate();	    }	    // set the editor with the selected item since this	    // is the event handler for a selected item change.	    if (comboBox.isEditable() && editor != null) {		comboBox.configureEditor( comboBox.getEditor(), 					  comboBox.getSelectedItem() );	    }            isDisplaySizeDirty = true;	    comboBox.repaint();	}        public void intervalAdded( ListDataEvent e ) {	    contentsChanged( e );        }        public void intervalRemoved( ListDataEvent e ) {            contentsChanged( e );        }        //        // LayoutManager        //        // This layout manager handles the 'standard' layout of combo boxes.        // It puts the arrow button to the right and the editor to the left.        // If there is no editor it still keeps the arrow button to the right.        public void addLayoutComponent(String name, Component comp) {}        public void removeLayoutComponent(Component comp) {}        public Dimension preferredLayoutSize(Container parent) {            return parent.getPreferredSize();        }        public Dimension minimumLayoutSize(Container parent) {            return parent.getMinimumSize();        }        public void layoutContainer(Container parent) {            JComboBox cb = (JComboBox)parent;            int width = cb.getWidth();            int height = cb.getHeight();                        Insets insets = getInsets();            int buttonSize = height - (insets.top + insets.bottom);            Rectangle cvb;            if ( arrowButton != null ) {	        if(BasicGraphicsUtils.isLeftToRight(cb)) {		    arrowButton.setBounds( width - (insets.right + buttonSize),					   insets.top,					   buttonSize, buttonSize);		}		else {		    arrowButton.setBounds( insets.left, insets.top,					   buttonSize, buttonSize);		}            }            if ( editor != null ) {                cvb = rectangleForCurrentValue();                editor.setBounds(cvb);            }        }        //        // ActionListener        //	// Fix for 4515752: Forward the Enter pressed on the	// editable combo box to the default button 	// Note: This could depend on event ordering. The first ActionEvent	// from the editor may be handled by the JComboBox in which case, the	// enterPressed action will always be invoked.	public void actionPerformed(ActionEvent evt) {	    Object item = comboBox.getEditor().getItem();	    if (item != null) {             if(!comboBox.isPopupVisible() && !item.equals(comboBox.getSelectedItem())) {               comboBox.setSelectedItem(comboBox.getEditor().getItem());             }             ActionMap am = comboBox.getActionMap();             if (am != null) {                Action action = am.get("enterPressed");                if (action != null) {                    action.actionPerformed(new ActionEvent(comboBox, evt.getID(),                                            evt.getActionCommand(),                                           evt.getModifiers()));                }                    }       }   }  }    class DefaultKeySelectionManager implements JComboBox.KeySelectionManager, UIResource { 	private String prefix = ""; 	private String typedString = "";        public int selectionForKey(char aKey,ComboBoxModel aModel) {	    if (lastTime == 0L) {		prefix = "";		typedString = "";	    } 	    boolean startingFromSelection = true; 	    int startIndex = comboBox.getSelectedIndex(); 	    if (time - lastTime < timeFactor) { 		typedString += aKey; 		if((prefix.length() == 1) && (aKey == prefix.charAt(0))) { 		    // Subsequent same key presses move the keyboard focus to the next 		    // object that starts with the same letter. 		    startIndex++; 		} else { 		    prefix = typedString; 		} 	    } else { 		startIndex++; 		typedString = "" + aKey; 		prefix = typedString; 	    } 	    lastTime = time;  	    if (startIndex < 0 || startIndex >= aModel.getSize()) { 		startingFromSelection = false; 		startIndex = 0; 	    } 	    int index = listBox.getNextMatch(prefix, startIndex, 					     Position.Bias.Forward); 	    if (index < 0 && startingFromSelection) { // wrap 		index = listBox.getNextMatch(prefix, 0, 					     Position.Bias.Forward); 	    }	    return index;	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -