gpt4 book ai didi

swing - jgoodies 绑定(bind) : using a JTextField with a formatted number?

转载 作者:行者123 更新时间:2023-12-04 20:59:51 25 4
gpt4 key购买 nike

我正在尝试将 JTextField 绑定(bind)到一个 bean 的字段 double使用 JGoodies 绑定(bind):

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

这工作,至少在 bean → JTextField 方向。在 JTextField → bean 方向,它有一个障碍:如果我开始在 JTextField 中输入,它会在小数点后的第一个数字后立即更新,弄乱 JTextField 焦点,并调整我的 JTextField 值。

(问题似乎来自试图将 GUI 的 String 调整为模型的 double )

我该如何解决????

演示这一点的示例程序:
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);
}
}

最佳答案

我不确定这是否是您要解决的问题,但是如果您将绑定(bind)更改为仅在失去焦点时提交,您就不应该再遇到这个问题了。只需将 true 指定为以下 bind 方法的第三个参数:

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

关于swing - jgoodies 绑定(bind) : using a JTextField with a formatted number?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1969615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com