我有以下代码..我意识到没有调用“setSearchedText”。所以绑定没有发生.. SearchInvestigationPanel 在 InvestigationsPanel 内。请注意,SearchInvestigationPanel 中的 textField 无法由类外部检索。我在这里错过了什么?
public class SearchInvestigationPanel extends JPanel {
private JTextField textField;
public SearchInvestigationPanel() {
init();
}
public String getText()
{
return (textField != null)? textField.getText() : "";
}
private void init() {
<code to add textField to this panel>
}
}
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
public class InvestigationsPanel extends JPanel {
private SearchInvestigationPanel searchInvestigationPanel;
private InvestigationsPanelModel investigationsPanelModel;
public InvestigationsPanel() {
init();
}
private SearchInvestigationPanel getSearchInvestigationPanel() {
if (searchInvestigationPanel == null) {
searchInvestigationPanel = new SearchInvestigationPanel();
}
return searchInvestigationPanel;
}
private void init() {
<code to add SearchInvestigationPanel inside this panel>
}
public void initBinding() {
BeanProperty<SearchInvestigationPanel, String> SearchInvestigationPanelSearchedTextProperty = BeanProperty
.create("text");
BeanProperty<InvestigationsPanelModel, String> investigationsPanelModelSearchedTextProperty = BeanProperty
.create("searchedText");
AutoBinding<SearchInvestigationPanel, String, InvestigationsPanelModel, String> filteredTextInvTreeBinding = Bindings
.createAutoBinding(AutoBinding.UpdateStrategy.READ,
getSearchInvestigationPanel(),
SearchInvestigationPanelSearchedTextProperty,
investigationsPanelModel,
investigationsPanelModelSearchedTextProperty);
filteredTextInvTreeBinding.bind();
}
}
public class InvestigationsPanelModel extends AbstractModelObject{
private String searchedText = "";
public String getSearchedText() {
return searchedText;
}
public void setSearchedText(String newSearchedText) {
String oldValue = searchedText;
this.searchedText = newSearchedText;
System.out.println(newSearchedText);
firePropertyChange("searchedText", oldValue, newSearchedText);
}
}
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public abstract class AbstractModelObject {
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName,
listener);
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}