Java Swing Key Action handleSliderAdjustmentViaKey(KeyEvent e)

Here you can find the source of handleSliderAdjustmentViaKey(KeyEvent e)

Description

Checks if a slider has been adjusted by a keystroke.

License

Mozilla Public License

Parameter

Parameter Description
e KeyEvent that has been triggered by the keystroke

Declaration

public static void handleSliderAdjustmentViaKey(KeyEvent e) 

Method Source Code

//package com.java2s;
/*/*w w w . j ava  2 s  .  c  om*/
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.awt.event.KeyEvent;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
    /**
     * Checks if a slider has been adjusted by a keystroke.
     * In this case the changeListener is invoked and valueIsAdjusting is set accordingly.
     * In this project this is necessary because slider changes are only handled if
     * getValueIsAdjusting() returns true.
     * 
     * @param e KeyEvent that has been triggered by the keystroke
     */
    public static void handleSliderAdjustmentViaKey(KeyEvent e) {
        if (e.getSource() instanceof JSlider) {
            if (KeyEvent.VK_LEFT == e.getKeyCode()
                    || KeyEvent.VK_RIGHT == e.getKeyCode()
                    || KeyEvent.VK_UP == e.getKeyCode()
                    || KeyEvent.VK_DOWN == e.getKeyCode()
                    || KeyEvent.VK_PAGE_UP == e.getKeyCode()
                    || KeyEvent.VK_PAGE_DOWN == e.getKeyCode()
                    || KeyEvent.VK_HOME == e.getKeyCode()
                    || KeyEvent.VK_END == e.getKeyCode()) {
                JSlider slider = (JSlider) e.getSource();
                for (ChangeListener listener : slider.getChangeListeners()) {
                    slider.setValueIsAdjusting(true);
                    listener.stateChanged(new ChangeEvent(slider));
                    slider.setValueIsAdjusting(false);
                }
                e.consume();
            }
        }
    }
}

Related

  1. getKeyStrokeRepresentation(KeyStroke ks)
  2. getKeyStrokeText(KeyStroke ks)
  3. getPrettyStringFor(KeyStroke keyStroke)
  4. getSystemHelpKey()
  5. getTypePanel(final ActionListener typeListener)
  6. initButton(String text, String actionKey, int shortcutKey, int modifiers, JComponent component, AbstractAction action)
  7. installCloseKey(final RootPaneContainer c)
  8. invoke(Action action, Object source)
  9. isActionSelected(Action action)