Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSlider Problem with setValue()

843806Jul 11 2009 — edited Jul 11 2009
I have a JSlider, which look slik eit initialises with the correct values (i.e the GUI looks correct) however it is apparent from the behaviour of the program that the JSlider is misbehaving.

When the program starts up the slider is in the correct position for the initial value (In my case 40) but the program behaves as if its set to minimum value (in this case 10). only when the slider is moved does the value appear to be changed.

Heres my code:
JSlider boxSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 45, 40); //initialises to min:10 max: 45, initial val: 40
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1;
        boxSizeSlider.setMajorTickSpacing(10);
        boxSizeSlider.setPaintTicks(true);
        boxSizeSlider.setPaintLabels(true);
        boxSizeSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                if (!source.getValueIsAdjusting()){
                    boxSize = source.getValue();
                    
                }
                
            }
        });
        boxSizeSlider.setValue(40); //these two lines are two attemts by me to manually set the value
        boxSizeSlider.getModel().setValue(40); //to the desired value, neither seem to work
        mainPanel.add(boxSizeSlider, c);
Thanks

James
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2009
Added on Jul 11 2009
6 comments
773 views