After initializing the slider1 variable in the test() constructor, add these lines so that the jslider tick values ββcan be set and visible in the graphical interface:
slider1.setMajorTickSpacing( 5 ); slider1.setPaintLabels( true );
You can change the tick step that is set to 5. Adding a slider using the add() method is not good practice, use getContentPane().add() instead, so your constructor should look like this:
public test2() { slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0); setLayout(new FlowLayout(FlowLayout.TRAILING)); slider1.setMajorTickSpacing( 5 ); slider1.setPaintLabels( true ); this.getContentPane().add(slider1); }
I noticed that you are not calling changeValue() in the main() method. As your method name implies, it seems to be a setter, however you are not setting a parametric value parametrically, is this good practice? In my opinion, this is not so. And also changeValue() does the same with setValue() , why create a redundant method ?. Anyway, you can use this:
public void changeValue (int newValue) { slider1.setValue(newValue); }
In your main method, use the following instructions:
test2 gui = new test2(); gui.changeValue( 25 );
To see the direct effect of changing the value of the slider, I mean updating it, use a button or some other component, add an ActionListener to it ActionListener that you can, for example, update the value of the slider when pressed.
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { changeValue( 25 );