Movable bars with value using java
Asked Answered
P

3

3

I want to create a bar with a value. If i move the circle, then the value in the field should change. How can i do this? Here is the screen shot. How can i achieve this?

Something similar to this is also ok. Please help me with this. Thank you in advance :)

enter image description here

Peppery answered 20/5, 2011 at 5:43 Comment(0)
G
9

SpinSlider may be a useful example.

Addendum: Here's an example using a suitable format string for JSpinner.NumberEditor.

Addendum: You can change the color of the slider by overriding paintTrack(), as shown here.

SpinSlider image

import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/** @see https://stackoverflow.com/questions/6067898 */
public class SpinSlider extends JPanel {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("SpinSlider!");
                f.add(new SpinSlider());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    public SpinSlider() {
        this.setLayout(new FlowLayout());
        final JSpinner spinner = new JSpinner();
        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSlider s = (JSlider) e.getSource();
                spinner.setValue(s.getValue());
            }
        });
        this.add(slider);
        spinner.setModel(new SpinnerNumberModel(50, 0, 100, 1));
        spinner.setEditor(new JSpinner.NumberEditor(spinner, "0'%'"));
        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner s = (JSpinner) e.getSource();
                slider.setValue((Integer) s.getValue());
            }
        });
        this.add(spinner);
    }
}
Grisgris answered 20/5, 2011 at 5:56 Comment(1)
@SRIKANTH KV: I saw your followup question and addressed the issue above.Grisgris
A
5

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));
}
Ayres answered 20/5, 2011 at 5:55 Comment(0)
M
1

In Java Swing you could use a JSlider (which already has the desired model and behavior) and then customize its UI

Mockheroic answered 20/5, 2011 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.