import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MainClass {
public static void main(String[] a) {
final JOptionPane optionPane = new JOptionPane();
JSlider slider = new JSlider();
slider.setMajorTickSpacing (10);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JSlider theSlider = (JSlider)changeEvent.getSource();
if (!theSlider.getValueIsAdjusting()) {
optionPane.setInputValue(new Integer(theSlider.getValue()));
}
}
};
slider.addChangeListener(changeListener);
optionPane.setMessage(new Object[] {"Select a value: " , slider});
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = optionPane.createDialog(null, "My Slider");
dialog.setVisible(true);
System.out.println ("Input: " + optionPane.getInputValue());
}
}
|