Increase step size on JSpinner on specific key pressed
I want to be able to increase the step size by JSpinner
when a certain key is pressed. What I have tried so far was to put KeyListener
on my counter and when a specific key is pressed, change the step size value. When the key is released, it should revert to it by default.
I think that I do not need to directly bet KeyListener
on JSpinner
, but rather on its button.
I don't know how to achieve this. I am confused here with doubles Listener
.
Here is the code I wrote for KeyListener
:
public class SpinnerKeyIncrement implements KeyListener {
JSpinner spinner;
SpinnerNumberModel spinnerModel;
public SpinnerKeyIncrement(JSpinner s) {
this.spinner = s;
if(spinnerModel == null)
spinnerModel = (SpinnerNumberModel) spinner.getModel();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
spinnerModel.setStepSize(.10);
}
else if(e.getKeyCode() == KeyEvent.VK_SHIFT) {
spinnerModel.setStepSize(1);
}
}
@Override
public void keyReleased(KeyEvent e) {
spinnerModel.setStepSize(0.01);
}
@Override
public void keyTyped(KeyEvent e) {}
}
}
source to share
Again, I would use Key Bindings to solve this problem, and I would use the bindings bound to the JSpinner InputMap that are active when they have focus:
InputMap inputMap = spinner.getInputMap(JComponent.WHEN_FOCUSED);
A working small sample program might look like this:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChangeStepSize extends JPanel {
private static final String DOUBLE_STEP = "double step";
public static final int SMALL_STEP_SIZE = 1;
public static final int BIG_STEP_SIZE = 10;
// bind to the "d" key, but you could use any key you'd like
private static final int SPECIAL_KEY = KeyEvent.VK_D;
private SpinnerNumberModel numberModel = new SpinnerNumberModel(50, 0, 100, 1);
private JSpinner spinner = new JSpinner(numberModel);
public ChangeStepSize() {
add(spinner);
// set up key bindings. First get InputMap and ActionMap
InputMap inputMap = spinner.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = spinner.getActionMap();
// next set bindings for when key is pressed
boolean onKeyRelease = false;
KeyStroke keyStroke = KeyStroke.getKeyStroke(SPECIAL_KEY, 0, onKeyRelease);
inputMap.put(keyStroke, DOUBLE_STEP + onKeyRelease);
actionMap.put(DOUBLE_STEP + onKeyRelease, new DoubleStepAction(onKeyRelease));
// next set bindings for when key is released
onKeyRelease = true;
keyStroke = KeyStroke.getKeyStroke(SPECIAL_KEY, 0, onKeyRelease);
inputMap.put(keyStroke, DOUBLE_STEP + onKeyRelease);
actionMap.put(DOUBLE_STEP + onKeyRelease, new DoubleStepAction(onKeyRelease));
}
// our Action is passed in a parameter to indicate which state it
// gets activated in: key press or key release
private class DoubleStepAction extends AbstractAction {
private boolean onKeyRelease;
public DoubleStepAction(boolean onKeyRelease) {
this.onKeyRelease = onKeyRelease;
}
@Override
public void actionPerformed(ActionEvent e) {
if (onKeyRelease) {
numberModel.setStepSize(SMALL_STEP_SIZE);
} else {
numberModel.setStepSize(BIG_STEP_SIZE);
}
}
}
private static void createAndShowGui() {
ChangeStepSize mainPanel = new ChangeStepSize();
JFrame frame = new JFrame("ChangeStepSize");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
source to share