Re: JSlider and JSpinner
In article <nospam-C0693F.20500616072009@news.aioe.org>,
"John B. Matthews" <nospam@nospam.invalid> wrote:
In article <7c92aqF273skaU1@mid.dfncis.de>,
"Rita Erfurt" <Rita.Erfurt@rwth-aachen.de> wrote:
Can somebody help me to eliminate the error in the connection of
JSlider with JSpinner please? Changes from slider to spinner work.
Changes from spninner to slider don't work, if I use the arrow keys.
Using your spinnerlistener(), I get the expected result; but it depends
critically on the JSpinner's NumberModel and initial state, a well as
the JSlider's snap and tick settings. Moreover, the listeners must each
access the other's state regardless of order. I suspect the error is in
unseen code. It might help to show a more complete example:
<http://pscode.org/sscce.html>
Oops, I forgot this:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/** @author John B. Matthews */
public class SpinSlider extends JPanel {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("SpinSlider!");
frame.add(new SpinSlider());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
public SpinSlider() {
this.setLayout(new FlowLayout());
final JSpinner spinner = new JSpinner();
final JSlider slider = new JSlider();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider s = (JSlider)e.getSource();
spinner.setValue(s.getValue());
}
});
this.add(slider);
spinner.setModel(new SpinnerNumberModel(50, 0, 100, 1));
spinner.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSpinner s = (JSpinner)e.getSource();
slider.setValue((Integer)s.getValue());
}
});
this.add(spinner);
}
}
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>