Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jgoodies binding: using a JTextField with a formatted number?

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);
    }
}
like image 618
Jason S Avatar asked Dec 28 '09 14:12

Jason S


1 Answers

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);
like image 182
Tony Eichelberger Avatar answered Sep 18 '22 15:09

Tony Eichelberger