d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is There Any Hope For This Doublejslider > Need To And Values Like 3.3333333
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 22 2012 05:01pm
I have this doubleJSlider in netbeans. see below.

If I add a number like 10/3 using:
doubleJSlider1.setDoubleValue(doubleJSlider1.getDoubleValue()+somemethod()/3);

When this is done 3 times it equals 9.99 not 10 :(

The getDoubleValue only gives two digits and discards the rest. So the result more exact being 9.9933333

Is there any way I can add it without using getDoubleValue(). Or any other way to use a doubleslider.

Need to do log10 functions. So would prefer if the values are kept in double not standard int slider.

:)

Code
package uk.co.drpj.util;

import javax.swing.*;

/**
* <b>Programm:</b> WaveGradient<br>
* <b>Copyright:</b> 2002 Andreas Gohr, Frank Schubert<br>
* <b>License:</b> GPL2 or higher<br>
* <br>
* <b>Info:</b> This JSlider uses doubles for its values
*/
public class DoubleJSlider extends JSlider{
   private final double step;

 /**
  * Constructor - initializes with 0.0,100.0,50.0
  */
 public DoubleJSlider(){
   super();
   setDoubleMinimum(0.0);
   setDoubleMaximum(1.0);
   setDoubleValue(0.0);
   step=0.01;
 }

 /**
  * Constructor
  */
 public DoubleJSlider(double min, double max, double val,double step){
   super();
   this.step=step;
   setDoubleMinimum(min);
   setDoubleMaximum(max);
   setDoubleValue(val);

 }

 /**
  * returns Maximum in double precision
  */
 public double getDoubleMaximum() {
   return( getMaximum()*step );
 }

 /**
  * returns Minimum in double precision
  */
 public double getDoubleMinimum() {
   return( getMinimum()*step );
 }

 /**
  * returns Value in double precision
  */
 public double getDoubleValue() {
   return( getValue()*step );
 }

 /**
  * sets Maximum in double precision
  */
 public void setDoubleMaximum(double max) {
   setMaximum((int)(max/step));
 }

 /**
  * sets Minimum in double precision
  */
 public void setDoubleMinimum(double min) {
   setMinimum((int)(min/step));
 }

 /**
  * sets Value in double precision
  */
 public void setDoubleValue(double val) {
   setValue((int)(val/step));
   setToolTipText(Double.toString(val));
 }

}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 22 2012 06:52pm
Quote (tigeranden @ Sep 22 2012 07:01pm)
I have this doubleJSlider in netbeans. see below.

If I add a number like 10/3 using:
doubleJSlider1.setDoubleValue(doubleJSlider1.getDoubleValue()+somemethod()/3);

When this is done 3 times it equals 9.99 not 10 :(

The getDoubleValue only gives two digits and discards the rest. So the result more exact being 9.9933333

Is there any way I can add it without using getDoubleValue(). Or any other way to use a doubleslider.

Need to do log10 functions. So would prefer if the values are kept in double not standard int slider.

:)

Code
package uk.co.drpj.util;

import javax.swing.*;

/**
* <b>Programm:</b> WaveGradient<br>
* <b>Copyright:</b> 2002 Andreas Gohr, Frank Schubert<br>
* <b>License:</b> GPL2 or higher<br>
* <br>
* <b>Info:</b> This JSlider uses doubles for its values
*/
public class DoubleJSlider extends JSlider{
   private final double step;

 /**
  * Constructor - initializes with 0.0,100.0,50.0
  */
 public DoubleJSlider(){
   super();
   setDoubleMinimum(0.0);
   setDoubleMaximum(1.0);
   setDoubleValue(0.0);
   step=0.01;
 }

 /**
  * Constructor
  */
 public DoubleJSlider(double min, double max, double val,double step){
   super();
   this.step=step;
   setDoubleMinimum(min);
   setDoubleMaximum(max);
   setDoubleValue(val);

 }

 /**
  * returns Maximum in double precision
  */
 public double getDoubleMaximum() {
   return( getMaximum()*step );
 }

 /**
  * returns Minimum in double precision
  */
 public double getDoubleMinimum() {
   return( getMinimum()*step );
 }

 /**
  * returns Value in double precision
  */
 public double getDoubleValue() {
   return( getValue()*step );
 }

 /**
  * sets Maximum in double precision
  */
 public void setDoubleMaximum(double max) {
   setMaximum((int)(max/step));
 }

 /**
  * sets Minimum in double precision
  */
 public void setDoubleMinimum(double min) {
   setMinimum((int)(min/step));
 }

 /**
  * sets Value in double precision
  */
 public void setDoubleValue(double val) {
   setValue((int)(val/step));
   setToolTipText(Double.toString(val));
 }

}


can you describe what you want a little better? i assume a slider is like a progress bar? you can just take the ceiling to round up, or you can use a scale 100 times what you want to increase accuracy

eg if you have thirds, you can convert it from 3.333 to 3, 6.667 to 7, 9.999 to 10.

if you have a scale from 0 to 10, you can make it 0 to 1000 for more precision. so 6.667 becomes 667/1000 which is closer than 6/10
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Sep 23 2012 05:26am
Here is some good external links about JSlider.

Note that what I use is an extension of JSlider called doubleJSlider. I installed that because the double value fits better for my needs.

Hovever I had been warned that the double would cause alot of problems with accuracy and that I should use BigDecimal,

Thats what this is all about :(

Documentation for JSlider:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JSlider.html

A demo about JSlider
http://docs.oracle.com/javase/tutorial/uiswing/components/slider.html

This post was edited by tigeranden on Sep 23 2012 05:26am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll