JSlider for doubles

18,836

Solution 1

You can multiply the value by 10

EDIT

You can change the labels displayed as follows:

Hashtable labelTable = new Hashtable();
labelTable.put( new Integer( 0 ), new JLabel("0.0") );
labelTable.put( new Integer( 5 ), new JLabel("0.5") );
labelTable.put( new Integer( 10 ), new JLabel("1.0") );
framesPerSecond.setLabelTable( labelTable );

Solution 2

One trick you can use with JSlider is to use the units in cents. So if you want the user to select an amount between 1 and 10 dollars you actually set the JSlider to the range 100 to 1000. Then you simply convert the value to dollars and cents when the user selects a value.

You can then use a Dictionary of values to specify what label is displayed at any value on the slider. Use the setLabelTable() method for this.

Share:
18,836

Related videos on Youtube

Aly
Author by

Aly

SOreadytohelp

Updated on April 16, 2022

Comments

  • Aly
    Aly about 2 years

    I am making a GUI (using swing) for a poker framework and need some sort of slider to allow players to select a bet size. However the Swing JSlider only works with int values whereas I need something that can support doubles for 1 decimal point. Are there any libraries I can use, or tricks with the JSlider?

  • Muhammad Ahmad Afzal
    Muhammad Ahmad Afzal over 14 years
    You can use setLabelTable to change the components (JLabels) used for the values displayed.
  • aperkins
    aperkins over 14 years
    I forgot about that - deleting my post in favor of that solution.
  • Aly
    Aly over 14 years
    What about the situation where the minimum number must be a double, lets say 5.5, I would make the JLabel read 5.5 and map the integer 5 to this JLabel. But when I call JSlider.getValue() I will get 5, and not 5.5. Any ideas on how to deal with this?
  • Aly
    Aly over 14 years
    What about the situation where the minimum number must be a double, lets say 5.5, I would make the JLabel read 5.5 and map the integer 5 to this JLabel. But when I call JSlider.getValue() I will get 5, and not 5.5. Any ideas on how to deal with this?
  • aperkins
    aperkins over 14 years
    You would make the minimum value be 55, not 5, and would then divide any value by 10.0 to get the decimal point value.
  • Vincent Ramdhanie
    Vincent Ramdhanie over 14 years
    The minimum value in this case is 550. You map this to 5.50 for display. When you get the value that the user selected you simply divide by 100 to get 5.5.