Reputation: 1014
I have a simple panel with a JSlider and 3 jtextfields. I am constructing a JSlider using the constructor JSlider(int min, int max, int value) like this :
public class Slide extends JFrame{
JSlider slider=new JSlider(100,200,50);//here lies the problem
JTextField maxVal=new JTextField(10);
JTextField minVal=new JTextField(10);
JTextField curVal=new JTextField(10);
public Slide(){
JPanel panel=new JPanel();
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
minVal.setText(slider.getMinimum()+"");
maxVal.setText(slider.getMaximum()+"");
curVal.setText(slider.getValue()+"");
}
});
panel.setLayout(new MigLayout("", "[][][][]", "[][][][]"));
panel.add(slider, "cell 0 0,alignx left,aligny top");
JLabel label = new JLabel("Min val");
panel.add(label,"cell 0 2");
panel.add(minVal, "cell 3 2");
this.setContentPane(panel);
JLabel label_1 = new JLabel("Max val:");
panel.add(maxVal,"cell 3 3");
panel.add(label_1, "cell 0 3");
panel.add(new JLabel("Cur value:"), "cell 0 4");
panel.add((curVal),"cell 3 4");
this.setSize(400, 400);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
new Slide();
}
}
The problem is that when I try to run this program I get an illegal argument exception but if i remove the third argument everything is ok.
Exception in thread "main" java.lang.IllegalArgumentException: invalid range properties
at javax.swing.DefaultBoundedRangeModel.<init>(DefaultBoundedRangeModel.java:102)
at javax.swing.JSlider.<init>(JSlider.java:274)
at javax.swing.JSlider.<init>(JSlider.java:241)
at JSlider.Slide.<init>(Slide.java:20)
at JSlider.Slide.main(Slide.java:54)
Java Result: 1
Any ideas why this is happening?
Upvotes: 1
Views: 2191
Reputation: 13402
From the Java Documentation:
JSlider(int min, int max, int value)
Creates a horizontal slider using the specified min, max and value.
The BoundedRangeModel that holds the slider's data handles any issues that may arise from improperly setting the minimum, initial, and maximum values on the slider. See the BoundedRangeModel documentation for details.
minimum <= value <= value+extent <= maximum
The parameters should hold the above relationship. In your case 100 (min
) is more than 50 (value
).
Upvotes: 3