Jgoodies binding: using a JTextField with a formatted number?

I am trying to bind a JTextField to a bean field that is 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 bean -> JTextField Direction. In JTextField → bean, it has one clue: if I start entering text in JTextField, it immediately takes my update after the first digit after the decimal point, interferes with the focus of JTextField and changes the value of JTextField.

(the problem seems to be related to trying to adapt the String GUI to the double model)

How to fix this?

demonstrating 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); } } 
+4
source share
1 answer

I'm not sure if this is what you are trying to solve, but if you change the binding only to fix focus, you should no longer set this. Just specify true as the third argument to the binding method below:

 Bindings.bind(jtf, ConverterFactory.createStringConverter(adapter.getValueModel("x"), new DecimalFormat("0.#####")), true); 
+2
source

All Articles