basiccomboboxui.java
来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 1,878 行 · 第 1/5 页
JAVA
1,878 行
} else if (key == TOGGLE || key == TOGGLE_2) { if (ui != null && (key == TOGGLE || !comboBox.isEditable())) { if ( ui.isTableCellEditor() ) { // Forces the selection of the list item if the // combo box is in a JTable. comboBox.setSelectedIndex(ui.popup.getList(). getSelectedIndex()); } else { comboBox.setPopupVisible(!comboBox.isPopupVisible()); } } } else if (key == UP) { if (ui != null) { if (ui.isPopupVisible(comboBox)) { ui.selectPreviousPossibleValue(); } else if (DefaultLookup.getBoolean(comboBox, ui, "ComboBox.showPopupOnNavigation", false)) { ui.setPopupVisible(comboBox, true); } } } else if (key == UP_2) { // Special case in which pressing the arrow keys will not // make the popup appear - except for editable combo boxes. if (comboBox.isShowing() && ui != null) { if ( comboBox.isEditable() && !comboBox.isPopupVisible()) { comboBox.setPopupVisible(true); } else { ui.selectPreviousPossibleValue(); } } } else if (key == ENTER) { if (comboBox.isPopupVisible()) { // Forces the selection of the list item boolean isEnterSelectablePopup = UIManager.getBoolean("ComboBox.isEnterSelectablePopup"); if (!comboBox.isEditable() || isEnterSelectablePopup || ui.isTableCellEditor) { Object listItem = ui.popup.getList().getSelectedValue(); if (listItem != null) { comboBox.getModel().setSelectedItem(listItem); // Ensure that JComboBox.actionPerformed() // doesn't set editor value as selected item comboBox.getEditor().setItem(listItem); } } comboBox.setPopupVisible(false); } else { // Call the default button binding. // 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(); if (e.getSource() == editor){ // If the border of the editor changes then this can effect // the size of the editor which can cause the combo's size to // become invalid so we need to clear size caches if ("border".equals(propertyName)){ isMinimumSizeDirty = true; isDisplaySizeDirty = true; comboBox.revalidate(); } } else { 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 buttonHeight = height - (insets.top + insets.bottom); int buttonWidth = buttonHeight; if (arrowButton != null) { Insets arrowInsets = arrowButton.getInsets(); buttonWidth = squareButton ? buttonHeight : arrowButton.getPreferredSize().width + arrowInsets.left + arrowInsets.right; } Rectangle cvb; if ( arrowButton != null ) { if(BasicGraphicsUtils.isLeftToRight(cb)) { arrowButton.setBounds( width - (insets.right + buttonWidth), insets.top, buttonWidth, buttonHeight); } else { arrowButton.setBounds( insets.left, insets.top, buttonWidth, buttonHeight); } } if ( editor != null ) { cvb = rectangleForCurrentValue(); editor.setBounds(cvb);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?