Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 517415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:49:54+00:00 2026-05-13T07:49:54+00:00

I am trying to bind a JTextField to a bean’s field that is a

  • 0

I am trying to bind a JTextField to a bean’s field that is a double using JGoodies Binding:

JTextField myJTextField = ...
BeanAdapter adapter = ...
Bindings.bind(myJTextField,
ConverterFactory.createStringConverter(adapter.getValueModel("amplitude"),
                    new DecimalFormat("0.00000")));

This works, at least in the bean → JTextField direction. In the JTextField → bean direction, it has one hitch: if I start typing in the JTextField it takes my update immediately after the first digit after the decimal point, messes up the JTextField focus, and tweaks my JTextField value.

(the problem seems to come from trying to adapt a GUI’s String to a model’s double)

How do I fix this????

sample program that demonstrates this:

package com.example.test.gui;

import java.awt.GridLayout;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import com.jgoodies.binding.adapter.Bindings;
import com.jgoodies.binding.adapter.BoundedRangeAdapter;
import com.jgoodies.binding.beans.BeanAdapter;
import com.jgoodies.binding.beans.ExtendedPropertyChangeSupport;
import com.jgoodies.binding.value.ConverterFactory;

public class FloatPointBinding {
    public static class MyModel {
        private int x;

        final private ExtendedPropertyChangeSupport changeSupport = 
            new ExtendedPropertyChangeSupport(this);

        public void addPropertyChangeListener(PropertyChangeListener x) {
            this.changeSupport.addPropertyChangeListener(x);
        }

        public void removePropertyChangeListener(PropertyChangeListener x) {
            this.changeSupport.removePropertyChangeListener(x);
        }

        static private int clip(int a)
        {
            return Math.min(Math.max(a, -32768), 32767);
        }
        static private int d2i(double a) {
            return clip((int) Math.floor(a*32768 + 0.5));
        }
        static private double i2d(int a) {
            return (clip(a)/32768.0);
        }

        public void setXCount(int x) {
            int oldX = this.x;
            int newX = x;
            this.x=newX; 
            this.changeSupport.firePropertyChange("x", i2d(oldX), i2d(newX));
            this.changeSupport.firePropertyChange("XCount", oldX, newX);
        }
        public void setX(double x) { setXCount(d2i(x)); }
        public double getX() { return i2d(this.x); }
        public int getXCount() { return this.x; }

    }

    public static class MyView extends JFrame
    {
        public MyView(MyModel model, String title)
        {
            super(title);
            JTextField jtf = new JTextField();
            JSlider jsl = new JSlider();

            jsl.setMinimum(-32768);
            jsl.setMaximum(32767);
            jsl.setMajorTickSpacing(4096);
            jsl.setPaintTicks(true);

            Hashtable labelTable = new Hashtable();
            labelTable.put( new Integer( -32768 ), new JLabel("-1") );
            labelTable.put( new Integer( 0 ), new JLabel("0") );
            labelTable.put( new Integer( 32767 ), new JLabel("1") );
            jsl.setLabelTable( labelTable );
            jsl.setPaintLabels(true);


            setLayout(new GridLayout());
            add(jsl);
            add(jtf);

            BeanAdapter adapter = new BeanAdapter(model, true);
            Bindings.bind(jtf, 
                    ConverterFactory.createStringConverter(adapter.getValueModel("x"),
                            new DecimalFormat("0.#####")));
            jsl.setModel(new BoundedRangeAdapter(adapter.getValueModel("XCount"), 0, -32768, 32767));
        }
    }

    public static void main(String[] args)
    {
        MyModel model = new MyModel();
        MyView view = new MyView(model, "FloatPointBinding");
        view.pack();
        view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        view.setVisible(true);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:49:54+00:00Added an answer on May 13, 2026 at 7:49 am

    I am not sure if this is what you are trying to solve, but if you change the binding to only commit on focus lost you shouldn’t have that issue anymore. Just specify true as the third argument to the bind method below:

    Bindings.bind(jtf, 
                  ConverterFactory.createStringConverter(adapter.getValueModel("x"),
                            new DecimalFormat("0.#####")),
                  true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.net gridview that I am trying bind to. My DataSource has
Jython 2.5 I'm trying to bind a method to the focusGained event of a
Im trying to bind a dropdownlist in ASP.NET MVC to a class. Luckily for
I trying to bind List to Listbox. And at the Button1Click method new instance
Im trying to bind a list with datetime objects to my repeater. if (e.Item.ItemType
I'm trying to bind a list of custom objects to a WPF Image like
I'm trying to bind the following shortcut: Ctrl + W to close tabs How
I am trying to create a model for Article site. I want to link
I am building an article site using django. I've added many to many relation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.