private void zAddKeyListenersToTextField ( ) { timeTextField . addKeyListener ( new KeyAdapter ( ) { /** * upPressed, This indicates whether or not the up arrow has already been pressed. This * is used to make sure that we do not "react" to the up arrow multiple times when the * keyboard auto repeat function begins. We will only react to the up arrow once, until * the up arrow is released. */ boolean upPressed = false ; /** * downPressed, This indicates whether or not the down arrow has already been pressed. * This is used to make sure that we do not "react" to the down arrow multiple times * when the keyboard auto repeat function begins. We will only react to the down arrow * once, until the down arrow is released. */ boolean downPressed = false ; /** * keyPressed, This is called when a key is pressed inside the text field. */ @ Override public void keyPressed ( KeyEvent e ) { // Handled the right arrow key, which opens the pop-up menu. if ( enableArrowKeys && e . isActionKey ( ) && e . getKeyCode ( ) == KeyEvent . VK_RIGHT ) { e . consume ( ) ; openPopup ( ) ; if ( popup != null ) { timeMenuPanel . selectFirstEntry ( ) ; } } // Handled the up arrow key, which activates the spinner function to increase // the time value. if ( enableArrowKeys && e . isActionKey ( ) && e . getKeyCode ( ) == KeyEvent . VK_UP ) { e . consume ( ) ; if ( upPressed || ! isEnabled ( ) ) { return ; } upPressed = true ; if ( getTime ( ) == null ) { setTime ( LocalTime . NOON ) ; } zInternalTryChangeTimeByIncrement ( 1 ) ; increaseTimer . start ( ) ; } // Handled the down arrow key, which activates the spinner function to decrease // the time value. if ( enableArrowKeys && e . isActionKey ( ) && e . getKeyCode ( ) == KeyEvent . VK_DOWN ) { e . consume ( ) ; if ( downPressed || ! isEnabled ( ) ) { return ; } downPressed = true ; if ( getTime ( ) == null ) { setTime ( LocalTime . NOON ) ; } zInternalTryChangeTimeByIncrement ( - 1 ) ; decreaseTimer . start ( ) ; } } @ Override public void keyReleased ( KeyEvent e ) { if ( e . isActionKey ( ) && e . getKeyCode ( ) == KeyEvent . VK_UP ) { e . consume ( ) ; upPressed = false ; increaseTimer . stop ( ) ; } if ( e . isActionKey ( ) && e . getKeyCode ( ) == KeyEvent . VK_DOWN ) { e . consume ( ) ; decreaseTimer . stop ( ) ; downPressed = false ; } } } ) ; }
zAddKeyListenersToTextField This adds key listeners to the text field . The right arrow key will open the drop - down menu . The up and down arrow keys will activate the spinner abilities of the time picker and increase or decrease the time .