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 / March 2006

Tip: Looking for answers? Try searching our database.

Problems with using JFormattedTextField

Thread view: 
Philipp - 31 Mar 2006 14:35 GMT
Hello,
I try to use a JFormattedTextField to show and enter numbers but can't
really figure out how to make it work.

The following code is the simplest example I have come up with.
If you start this program you get a window where you can enter a number
in the upper textfield and the lower should get update by multiplying
the upper number by 1.5.
Now do this: type 123.45 in the upper field and press return. the lower
field get 185.175 (correct). Now if you click on the lower field the
upper field gets truncated to just showing 123.
Why?
Maybe something to do with Locales?

Full code for example below.

Thanks for any suggestions
Karl

---- CODE  ----

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestFrame extends JFrame implements PropertyChangeListener{
    JTextField deltaXTf;
    JTextField xCalTf;

    public static void main(String[] args) {
        TestFrame n = new TestFrame();
    }

    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        deltaXTf = new
JFormattedTextField(NumberFormat.getNumberInstance());
        xCalTf= new JFormattedTextField(NumberFormat.getNumberInstance());
        deltaXTf.addPropertyChangeListener("value", this);
        getContentPane().add(deltaXTf, BorderLayout.NORTH);
        getContentPane().add(xCalTf, BorderLayout.SOUTH);
        deltaXTf.setText("0");
        xCalTf.setText("0");
        pack();
        setVisible(true);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        Object src = evt.getSource();
        double deltaXTfVal = Double.parseDouble(deltaXTf.getText());
        double newValue = deltaXTfVal * 1.5;
        xCalTf.setText("" + newValue);
    }
}
Chris Uppal - 31 Mar 2006 15:26 GMT
> I try to use a JFormattedTextField to show and enter numbers but can't
> really figure out how to make it work.

Try the appended code, it's just your code with a few changes:
+   We set the format's idea of how many decimal digits to use
       explicitly.
+   We don't use setText(), but setValue() which respects the
       requested formatting.
+   We don't use Double.parseDouble(), but ask the formatter to do the
       parsing for us.

(Double.parseDouble() would barf on:
       1,234.567
which is how that number is printed in my locale.  In others it would be:
       1.234,567
which Double.parseDouble() can't handle either ;-)

   -- chris

=====================
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestFrame
extends JFrame implements PropertyChangeListener
{
    JFormattedTextField deltaXTf;
    JFormattedTextField xCalTf;
    NumberFormat format = NumberFormat.getNumberInstance();
    Double zero = Double.valueOf(0.0);

    public static void main(String[] args)
    {
        TestFrame n = new TestFrame();
    }

    public TestFrame()
    {
        format.setMinimumFractionDigits(2);
        format.setMaximumFractionDigits(4);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        deltaXTf = new JFormattedTextField(format);
        xCalTf= new JFormattedTextField(format);
        deltaXTf.addPropertyChangeListener("value", this);
        getContentPane().add(deltaXTf, BorderLayout.NORTH);
        getContentPane().add(xCalTf, BorderLayout.SOUTH);
        deltaXTf.setValue(zero);
        xCalTf.setValue(zero);
        pack();
        setVisible(true);
    }

    public void propertyChange(PropertyChangeEvent evt)
    {
        Object src = evt.getSource();
        Number deltaXTfVal = zero;
        try
        {
            deltaXTfVal = format.parse(deltaXTf.getText());
        }
        catch (ParseException e)
        {
            System.err.println(e);
        }
        double newValue = deltaXTfVal.doubleValue() * 1.5;
        xCalTf.setValue(Double.valueOf(newValue));
    }
}


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.