Binding of JText fields value to Info Class

时光怂恿深爱的人放手 提交于 2020-01-04 06:39:10

问题


Is there any way to automatic binding of Swing JTextFields to Info Class. e.g

 private JTextField receiptId;
 private JTextField Id;


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

  // Getters and Setters
  }

Is there any way when I entered values from Swing page then automatically values bind with my Info class? After that I can pass my Info object to other classes instead of sending all Text Fields.


回答1:


This 'binding' is typically done using a DocumentListener.

Call your setters Info.setReceiptId() and Info.setId() from the listeners registered with each of your JTextFields.




回答2:


Here's a working solution for your copy paste pleasure :)

This is the listener that does the magic.. ;)

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();
        }
    }
}

Assuming your model object looks like the following class.

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 + "]";
    }
}

Assume your GUI is working on code such as this..

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"));

Update: Here's some sample output as well..

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]


来源:https://stackoverflow.com/questions/12337986/binding-of-jtext-fields-value-to-info-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!