JSpinner return more than one value after single pressed

E

ereuseen

Hello,
I made the JSpinner:

SpinnerModel model = new SpinnerNumberModel(10, 0, 100, 0.1);
JSpinner spinner = new JSpinner(model);

and listener method for it:

public void stateChanged(ChangeEvent changeEvent)
{
Double value = new Double(0);
value =
(Double)((SpinnerNumberModel)changeEvent.getSource()).getNumber();
System.out.println(value.doubleValue());
}

generaly it works, but sometimes it write on screen ( after one mouse
pressed ! ) 2 values insted 1, eg.
9.700000000001 and 9.7

does some know what is the bonus 9.700000000001 value?
 
D

Daniel Pitts

Hello,
I made the JSpinner:

SpinnerModel model = new SpinnerNumberModel(10, 0, 100, 0.1);
JSpinner spinner = new JSpinner(model);

and listener method for it:

public void stateChanged(ChangeEvent changeEvent)
{
Double value = new Double(0);
value =
(Double)((SpinnerNumberModel)changeEvent.getSource()).getNumber();
System.out.println(value.doubleValue());
}

generaly it works, but sometimes it write on screen ( after one mouse
pressed ! ) 2 values insted 1, eg.
9.700000000001 and 9.7

does some know what is the bonus 9.700000000001 value?

Please create an SSCCE so that we can observe that behavior for
ourselves.
 
E

ereuseen

Please create an SSCCE so that we can observe that behavior for
ourselves.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class UsingSpinner implements ChangeListener
{
public UsingSpinner()
{
JFrame MainFrame = new JFrame();
SpinnerModel Model = new SpinnerNumberModel(10, 0, 100, 0.1);
Model.addChangeListener(this);
JSpinner MySpinner = new JSpinner(Model);
MainFrame.getContentPane().add(MySpinner);
MainFrame.setSize(300, 100);
MainFrame.setVisible(true);
}

public void stateChanged(ChangeEvent ce)
{
Double value = new Double(0);
value = (Double)((SpinnerNumberModel)ce.getSource()).getValue();
System.out.println(value.doubleValue());
}

public static void main(String arg[])
{
UsingSpinner MySpinner = new UsingSpinner();
}
}
 
R

Roedy Green

does some know what is the bonus 9.700000000001 value?
one thing that drives you a bit nuts is when you set the value of a
Component programmatically it will generate a change event. So you
can get yourself in infinite regress if you are not careful, e.g. if
you tidy the value on a change event.
 
D

Daniel Pitts

one thing that drives you a bit nuts is when you set the value of a
Component programmatically it will generate a change event. So you
can get yourself in infinite regress if you are not careful, e.g. if
you tidy the value on a change event.

Looking at his SSCCE, I've noticed the same behavior, but here's
exactly what I notice...

"Spinning" the value up or down will sometimes cause a non-exact
floating point number (11.29999999999999). The next click after that
will cause two values, first the rounded version, THEN the new current
version.

Very odd indeed.
 
M

Michael Jung

Daniel Pitts said:
Looking at his SSCCE, I've noticed the same behavior, but here's
exactly what I notice...
"Spinning" the value up or down will sometimes cause a non-exact
floating point number (11.29999999999999). The next click after that
will cause two values, first the rounded version, THEN the new current
version.

Not having seen the implementation of SpinnerModel, I assume the following
happens. A new number is an increment of the old as (integer) multiples of
0.1. After this is calculated, a stateChange event is fired. The next step
is rounding the resulting number to a multiple of 0.1. If this is different
from the originally calculated number, another state change is fired. Note
that this can happen, since 0.1 is not a machine number and multiples must be
rounded.

Michael
 
B

Ben Phillips

Roedy said:
one thing that drives you a bit nuts is when you set the value of a
Component programmatically it will generate a change event. So you
can get yourself in infinite regress if you are not careful, e.g. if
you tidy the value on a change event.

This can be avoided. Make each event handler a separate instance of some
class implementing the appropriate interface. Put an ivar like this in:

private boolean inHandler = false;

and in the handler method,

if (inHandler) return;
inHandler = true;
try {
..
..
..
} finally {
inHandler = false;
}
 
R

Roedy Green

"Spinning" the value up or down will sometimes cause a non-exact
floating point number (11.29999999999999). The next click after that
will cause two values, first the rounded version, THEN the new current
version.

He needs a DecimalFormat that will round the value to X decimal
places.

See the code for CanadianTax where I do that on a spinner to display
price.

see http://mindprod.com/applet/canadiantax.html
 
R

Roedy Green

JSpinner MySpinner = new JSpinner(Model);

Apply a DecimalFormat

saleAmountSpinnerModel =
new SpinnerNumberModel( /* initial value */ 100.00d,
/* min */ 0.00d,
/* max */ 999999.99d,
/* step */ 0.01d );

// wants a String, not a DecimalFormat.
saleAmountNumberEditor =
new JSpinner.NumberEditor( saleAmountSpinner,
"'$'###,##0.00" );

saleAmountSpinner.setModel( saleAmountSpinnerModel );
saleAmountSpinner.setEditor( saleAmountNumberEditor );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top