Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

JSpinner return more than one value after single pressed

Thread view: 
ereuseen - 14 Aug 2007 18:12 GMT
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?
Daniel Pitts - 14 Aug 2007 20:32 GMT
> Hello,
> I made the JSpinner:
[quoted text clipped - 17 lines]
>
> does some know what is the bonus 9.700000000001 value?

Please create an SSCCE so that we can observe that behavior for
ourselves.
ereuseen - 15 Aug 2007 07:13 GMT
> 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();
   }
}
Roedy Green - 16 Aug 2007 02:28 GMT
>    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 );

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 15 Aug 2007 10:43 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Daniel Pitts - 15 Aug 2007 16:20 GMT
On Aug 15, 2:43 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >does some know what is the bonus 9.700000000001 value?
>
[quoted text clipped - 5 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

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.
Michael Jung - 15 Aug 2007 18:41 GMT
> On Aug 15, 2:43 am, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
[quoted text clipped - 13 lines]
> 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
Roedy Green - 16 Aug 2007 00:41 GMT
On Wed, 15 Aug 2007 15:20:14 -0000, Daniel Pitts
<googlegroupie@coloraura.com> wrote, quoted or indirectly quoted
someone who said :

>"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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Ben Phillips - 15 Aug 2007 21:44 GMT
>>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.

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;
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.