为什么不能在this
引用上调用firePropertyChange()
?
[javac] /home/thufir/NetBeansProjects/Legacy/src/legacy/gui/table/SelectTableFollowup.java:41: error: cannot find symbol
[javac] this.firePropertyChange("client", null, i);
[javac] ^
[javac] symbol: method firePropertyChange(String,<null>,int)
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error
[javac] 1 warning
BUILD FAILED
/home/thufir/NetBeansProjects/Legacy/nbproject/build-impl.xml:923: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/Legacy/nbproject/build-impl.xml:263: Compile failed; see the compiler error output for details.
Total time: 4 seconds
thufir@dur:~/NetBeansProjects/Legacy$
这是我的代码:
package legacy.gui.table;
import java.awt.BorderLayout;
import java.util.logging.Logger;
import javax.swing.JPanel;
import legacy.database.Queries;
public class SelectTableFollowup extends JPanel {
private static final Logger log = Logger.getLogger(SelectTableFollowup.class.getName());
private ClientsTable table;
private Followup followup;
private Select Select;
private final Queries queries = new Queries();
public SelectTableFollowup() {
initComponents();
}
private void initComponents() {
table = new ClientsTable();
followup = new Followup();
Select = new Select();
setLayout(new BorderLayout());
add(Select, BorderLayout.NORTH);
add(table, BorderLayout.CENTER);
add(followup, BorderLayout.SOUTH);
followupButton();
query();
cannotFirePropertyChangeOnThisObj();
}
private void cannotFirePropertyChangeOnThisObj() {
table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if ("client".equals(evt.getPropertyName())) {
String s = evt.getNewValue().toString();
int i = Integer.parseInt(s);
log.info("i is " + i);
this.firePropertyChange("client", null, i);
}
}
});
}
private void followupButton() {
followup.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if ("followup".equals(evt.getPropertyName())) {
try {
int id = table.getId();
queries.send(id);
table.updateModel();
} catch (ArrayIndexOutOfBoundsException obe) {
log.info(obe.toString()); //no big deal
}
}
}
});
}
private void query() {
Select.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if ("select".equals(evt.getPropertyName())) {
log.info(evt.getNewValue().toString());
table.select(evt.getNewValue().toString());
}
}
});
}
}
从Netbeans,我知道这是很可能调用this.firePropertyChange()
, JPanel
的Netbeans创建。
上面的类与Netbeans生成的类有什么不同?它们都扩展了同一个基类。
如果我误用了什么术语,请告诉我。
在此上下文中:
table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
...
this.firePropertyChange("client", null, i);
}
}
关键字this
是指创建新的PropertyChangeListener
实例时生成的匿名内部类。这里解释了这个事实:使用This关键字
看一下这个主题:关键字为外部类从一个匿名的内部类?您应该将这一行替换为引用面板类的这一行:
table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
...
SelectTableFollowup.this.firePropertyChange("client", null, i);
}
}
或者你可以删除this
,编译器会为你解决同样的问题:
table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
...
firePropertyChange("client", null, i);
}
}
如果匿名内部类没有任何firePropertyChange()
方法,编译器将查找外部类(或最终的层次树)并检查它是否有这样的方法,情况确实如此。
我认为错误发生在这个方法:
private void cannotFirePropertyChangeOnThisObj() {
table.addPropertyChangeListener(new java.beans.PropertyChangeListener()
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if ("client".equals(evt.getPropertyName())) {
String s = evt.getNewValue().toString();
int i = Integer.parseInt(s);
log.info("i is " + i);
this.firePropertyChange("client", null, i);
}
}
});
}
您正在匿名PropertyChangeListener
对象上调用firePropertyChange()
。文档声明它没有声明firePropertyChange()
方法。
我认为你想在你的SelectTableFollowup
对象上调用它。那么你必须像这样调用它:
private void cannotFirePropertyChangeOnThisObj() {
table.addPropertyChangeListener(new java.beans.PropertyChangeListener()
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if ("client".equals(evt.getPropertyName())) {
String s = evt.getNewValue().toString();
int i = Integer.parseInt(s);
log.info("i is " + i);
SelectTableFollowup.this.firePropertyChange("client", null, i); // Reference this of the outer class
}
}
});
}
因为PropertyChangeListener
没有声明这个方法,你也可以直接写firePropertyChange("client", null, i);