I need to use JSlider with getting input live, meaning that it will return an input without pressing any button. I have this piece of code for the slider:
JPanel panel = new JPanel();
JSlider js = new JSlider(JSlider.VERTICAL, 0, 20, 10);
js.setMajorTickSpacing(2);
js.setPaintTicks(true);
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(js.getMinimum()), new JLabel("x0"));
labelTable.put(new Integer((js.getMinimum() + js.getMaximum()) / 2), new JLabel("x1"));
labelTable.put(new Integer(js.getMaximum()), new JLabel("x2"));
js.setLabelTable(labelTable);
js.setPaintLabels(true);
panel.add(js);
int result = JOptionPane.showConfirmDialog(null, panel, "choose size", JOptionPane.YES_OPTION);
Is it even possible to do so? I thought using actionListener but I didn't succeed.
It's possible, using a ChangeListener, for example
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
int value = slider.getValue();
...
}
});
来源:https://stackoverflow.com/questions/21261506/get-jsliders-input-and-changes