将Text字段值绑定到Info Class

时间:2012-09-09 09:34:03

标签: java swing data-binding jtextfield documentlistener

有没有办法将Swing JTextFields自动绑定到Info Class。 e.g

 private JTextField receiptId;
 private JTextField Id;


 public class Info {
  private string receiptId;
  private String id;

  // Getters and Setters
  }

当我从Swing页面输入值然后自动将值与我的Info类绑定时,有什么办法吗?之后,我可以将我的Info对象传递给其他类,而不是发送所有文本字段。

2 个答案:

答案 0 :(得分:5)

此“绑定”通常使用DocumentListener完成。

从每个Info.setReceiptId()注册的听众中致电您的二传手Info.setId()JTextFields

答案 1 :(得分:4)

这是一个适合您的复制粘贴乐趣的工作解决方案:)

这是做魔术的听众..;)

public class BindingListener implements DocumentListener {
    private ModelObject model;
    private String fieldName;

    public BindingListener(ModelObject model, String fieldName) {
        this.model = model;

        String firstChar = String.valueOf(fieldName.charAt(0));
        if (firstChar.equals(firstChar.toLowerCase())) {
            fieldName = firstChar.toUpperCase()
                    + fieldName.substring(1, fieldName.length());
        }

        this.fieldName = fieldName;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        dataUpdated(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        dataUpdated(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        dataUpdated(e);
    }

    private void dataUpdated(DocumentEvent e) {
        try {
            String text = e.getDocument().getText(
                    e.getDocument().getStartPosition().getOffset(),
                    e.getDocument().getEndPosition().getOffset() - 1);
            System.out.println("Entered text: " + text);
            System.out.println("Field to be updated: " + fieldName);

            System.out.println("Old model values: " + model);

            Method method = model.getClass().getDeclaredMethod(
                    "set" + fieldName, String.class);
            method.invoke(model, text);

            System.out.println("New model values: " + model);
        } catch (BadLocationException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (SecurityException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (IllegalArgumentException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        }
    }
}

假设您的模型对象看起来像下面的类。

public class ModelObject {
    private String field1;
    private String field2;

    /**
     * @return the field1
     */
    public String getField1() {
        return field1;
    }

    /**
     * @param field1
     *            the field1 to set
     */
    public void setField1(String field1) {
        this.field1 = field1;
    }

    /**
     * @return the field2
     */
    public String getField2() {
        return field2;
    }

    /**
     * @param field2
     *            the field2 to set
     */
    public void setField2(String field2) {
        this.field2 = field2;
    }

    @Override
    public String toString() {
        return "ModelObject [field1=" + field1 + ", field2=" + field2 + "]";
    }
}

假设您的GUI正在处理此类代码..

JTextField textField1 = new JTextField();
textField1.setPreferredSize(new Dimension(100, 25));
JTextField textField2 = new JTextField();
textField2.setPreferredSize(new Dimension(100, 25));
panel.add(textField1);
panel.add(textField2);

ModelObject model = new ModelObject();

textField1.getDocument().addDocumentListener(new BindingListener(model, "field1"));
textField2.getDocument().addDocumentListener(new BindingListener(model, "field2"));

更新:以下是一些示例输出..

Entered text: x
Field to be updated: Field1
Old model values: ModelObject [field1=null, field2=null]
New model values: ModelObject [field1=x, field2=null]
Entered text: y
Field to be updated: Field2
Old model values: ModelObject [field1=x, field2=null]
New model values: ModelObject [field1=x, field2=y]