To create a bar with a value (slider) in Java, you can use the JSlider
class. When you create an instance of this class you can specify all the parameters like min. and max. value, step size, etc.
You can add a ChangeListener
to the slider. This Changelistener
should implement the method stateChanged()
and in this method you can change the value displayed in the text box based on the position of the slider.
JSlider slider= new JSlider(JSlider.HORIZONTAL,0,100,50); //min value 0, max value 100, initial value 50
slider.addChangeListener(this)
JTextFox text = new JTextFox("50");
//Some other code, adding the the slider, text box (and other stuff) to the application
//...
public void stateChanged(ChangeEvent e)
{
JSlider source = (JSlider)e.getSource();
int value = (int)source.getValue();
text.setText(Integer.toString(value));
}