java类库详细讲解

源代码在线查看: combobox_cbmultikey.html

软件大小: 5593 K
上传用户: add505
关键词: java
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				
				
				Selecting an Item in a JComboBox Component with Multiple Keystrokes
				(Java Developers Almanac Example)
				
				
				
				
				
				
								    BODY CODE  {font-family: Courier, Monospace;				           font-size: 11pt}				    TABLE, BODY				          {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 10pt}				    PRE   {font-family: Courier, Monospace;				           font-size: 10pt}				    H3    {font-family: Verdana, Arial, Helvetica, sans-serif;				           font-size: 11pt}				    A.eglink {text-decoration: none}				    A:hover.eglink {text-decoration: underline}				    -->
				
				
				
				
				
				The Java Developers Almanac 1.4
				
				        Order this book from Amazon.
				    
				
				
				
				
				
				
				
				
				
				
				
				Home
				    >
				    List of Packages
				    >
				    javax.swing
				         [136 examples]
				        
				        >
				        JComboBox
				             [12 examples]
				            
				
				  
				    e764.  
				    Selecting an Item in a JComboBox Component with Multiple Keystrokes
				
				By default, when the user types a keystroke in a read-only combobox and an
				item in the combobox starts with the typed keystroke, the combobox will
				select that item. This behavior is not ideal if there are multiple
				items that start with the same letter. 
				
				 This example demonstrates how to customize a combobox so that
				it will select an item based on multiple keystrokes. More
				specifically, if a keystroke is typed within 300 milliseconds of the
				previous keystroke, the new keystroke is appended to the previous
				keystroke, and the combobox will select an item that starts with both
				keystrokes.
				
				
				
				    // Create a read-only combobox
				    String[] items = {"Ant", "Ape", "Bat", "Boa", "Cat", "Cow"};
				    JComboBox cb = new JComboBox(items);
				    
				    // Install the custom key selection manager
				    cb.setKeySelectionManager(new MyKeySelectionManager());
				    
				    // This key selection manager will handle selections based on multiple keys.
				    class MyKeySelectionManager implements JComboBox.KeySelectionManager {
				        long lastKeyTime = 0;
				        String pattern = "";
				    
				        public int selectionForKey(char aKey, ComboBoxModel model) {
				            // Find index of selected item
				            int selIx = 01;
				            Object sel = model.getSelectedItem();
				            if (sel != null) {
				                for (int i=0; i<model.getSize(); i++) {
				                    if (sel.equals(model.getElementAt(i))) {
				                        selIx = i;
				                        break;
				                    }
				                }
				            }
				    
				            // Get the current time
				            long curTime = System.currentTimeMillis();
				    
				            // If last key was typed less than 300 ms ago, append to current pattern
				            if (curTime - lastKeyTime < 300) {
				                pattern += ("" + aKey).toLowerCase();
				            } else {
				                pattern = ("" + aKey).toLowerCase();
				            }
				    
				            // Save current time
				            lastKeyTime = curTime;
				    
				            // Search forward from current selection
				            for (int i=selIx+1; i<model.getSize(); i++) {
				                String s = model.getElementAt(i).toString().toLowerCase();
				                if (s.startsWith(pattern)) {
				                    return i;
				                }
				            }
				    
				            // Search from top to current selection
				            for (int i=0; i<selIx ; i++) {
				                if (model.getElementAt(i) != null) {
				                    String s = model.getElementAt(i).toString().toLowerCase();
				                    if (s.startsWith(pattern)) {
				                        return i;
				                    }
				                }
				            }
				            return -1;
				        }
				    }
				
				
				
				
				             Related Examples
				        
				
				
				
				
				e760. 
				    Creating a JComboBox Component
				
				
				
				e761. 
				    Getting and Setting the Selected Item in a JComboBox Component
				
				
				
				e762. 
				    Getting the Items in a JComboBox Component
				
				
				
				e763. 
				    Adding and Removing an Item in a JComboBox Component
				
				
				
				e765. 
				    Determining If the Menu of a JComboBox Component Is Visible
				
				
				
				e766. 
				    Displaying the Menu in a JComboBox Component Using a Keystroke
				
				
				
				e767. 
				    Displaying the Menu in a JComboBox Component Using a Keystroke If the Selected Item Is Not Unique
				
				
				
				e768. 
				    Setting the Number of Visible Items in the Menu of a JComboBox Component
				
				
				
				e769. 
				    Listening for Changes to the Selected Item in a JComboBox Component
				
				
				
				e770. 
				    Listening for Action Events from a JComboBox Component
				
				
				
				e771. 
				    Determining When the Menu of a JComboBox Component Is Displayed
				
				
				
				
				
				
				
				
				        See also: 
				
				    Actions
				  
				
				
				    JButton
				  
				
				
				    JCheckBox
				  
				
				
				    JDesktop and JInternalFrame
				  
				
				
				    JFrame, JWindow, JDialog
				  
				
				
				    JLabel
				  
				
				
				    JList
				  
				
				
				    JProgressBar
				  
				
				
				    JRadioButton
				  
				
				
				    JScrollPane
				  
				
				
				    JSlider
				  
				
				
				    JSpinner
				  
				
				
				    JSplitPane
				  
				
				
				    JTabbedPane
				  
				
				
				    JToolBar
				  
				
				
				    Keystrokes and Input Maps
				  
				
				
				    Layout
				  
				
				
				    Look and Feel
				  
				
				
				    Menus
				  
				
				
				    Progress Monitor
				  
				
				
				    The Screen
				  
				
				
				    Tool Tips
				  
				
				
				    UI Default Values
				  
				
				
				
				
				
				
				
				
				
				© 2002 Addison-Wesley.
				
				
				
							

相关资源