class MyBean extends AbstractBean { private String strProperty = ""; // // , . // } // ...- ... MyBean modelBean = new MyBean(); // ... JTextField textField = new JTextField(); // ... Bindings.createAutoBinding(READ_WRITE, modelBean, BeanProperty.create("strProperty"), textField, BeanProperty.create("text")).bind(); class ComboBoxVal { private int id = 0; private String text = ""; // // , . // } class MyBean extends AbstractBean { private String stringProperty = "Hello!"; private ComboBoxVal comboProperty = new ComboBoxVal(0, "Item0"); // ... // ...- ... private List comboBoxData = new ArrayList(); // ... for (int i = 0; i < 10; i++) comboBoxData.add( new ComboBoxVal(i, "Item" + i)); JComboBox comboBox = new JComboBox(); // ... SwingBindings.createJComboBoxBinding( READ, comboBoxData, comboBox).bind(); Bindings.createAutoBinding(READ_WRITE, comboBox, BeanProperty.create("selectedItem"), modelBean, BeanProperty.create("comboProperty")).bind(); class StringValidator extends Validator { private int maxStrLength = 10; // ... // , . // ... public Result validate(String value) { if (value == null || value.length() == 0) return new StringEmptyResult(); if (value.length() > maxStrLength) return new StringTooLong(); return null; } public static class StringEmptyResult extends Result { public StringEmptyResult() { super(0, "Str empty msg"); } } public static class StringTooLong extends Result { public StringTooLong() { super(1, "Str too long msg"); } } } //...- ... private Binding textBinding; // ... textBinding = Bindings.createAutoBinding( READ_WRITE, modelBean, BeanProperty.create("strProperty"), textField, BeanProperty.create("text")); textBinding.setValidator(validator); textBinding.bind(); textBinding.addBindingListener( new BindingListener(){ public void syncFailed(Binding binding, Binding.SyncFailure failure) { // . } public void synced(Binding binding) { // . } // ... Source: https://habr.com/ru/post/38211/
All Articles