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