我在运行时使用javafx2创建了带可编辑单元格的表。按Enter键时,表单元格已编辑但未更新,表格类集方法在bankmodal.java中 tab_bankname.set(tab_bankname); 位置显示错误,错误消息
对于String
类型,方法set(String)未定义
表类名:bankmodal.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class bankmodal {
public final StringProperty tab_bankname;
public final StringProperty tab_bankaccno;
public final StringProperty tab_accname;
public final StringProperty tab_uniqueno;
public final StringProperty tab_address;
public final StringProperty tab_accounttype;
public final StringProperty tab_ifsc;
public final StringProperty tab_micr;
public bankmodal(String tab_uniqueno,String tab_accname,String tab_bankaccno,
String tab_address,String tab_accounttype,String tab_ifsc,String tab_micr,String tab_bankname)
{
this.tab_bankaccno = new SimpleStringProperty(tab_bankaccno);
this.tab_address = new SimpleStringProperty(tab_address);
this.tab_accounttype = new SimpleStringProperty(tab_accounttype);
this.tab_ifsc = new SimpleStringProperty(tab_ifsc);
this.tab_accname = new SimpleStringProperty(tab_accname);
this.tab_uniqueno = new SimpleStringProperty(tab_uniqueno);
this.tab_micr = new SimpleStringProperty(tab_micr);
this.tab_bankname = new SimpleStringProperty(tab_bankname);
}
public String getTab_bankname() {
return tab_bankname.get();
}
public void setTab_bankname(String tab_bankname) {
tab_bankname.set(tab_bankname);
}
public String getTab_bankaccno() {
return tab_bankaccno.get();
}
public void setTab_bankaccno(String tab_bankaccno) {
tab_bankaccno.set(getTab_bankaccno());
}
public String getTab_accname() {
return tab_accname.get();
}
public void setTab_accname(String tab_accname) {
tab_accname.set(tab_accname);
}
public String getTab_uniqueno() {
return tab_uniqueno.get();
}
public void setTab_uniqueno(String tab_uniqueno) {
tab_uniqueno.set(tab_uniqueno);
}
public String getTab_address() {
return tab_address.get();
}
public void setTab_address(String tab_address) {
tab_address.set(tab_address);
}
public String getTab_accounttype() {
return tab_accounttype.get();
}
public void setTab_accounttype(String tab_accounttype) {
tab_accounttype.set(tab_accounttype);
}
public String getTab_ifsc() {
return tab_ifsc.get();
}
public void setTab_ifsc(String tab_ifsc) {
tab_ifsc.set(tab_ifsc);
}
public String getTab_micr() {
return tab_micr.get();
}
public void setTab_micr(String tab_micr) {
tab_micr.set(tab_micr);
}
}
控制器类名称:Bank.java
public void fetchData(){
bankDetails.setEditable(true);
try{
bank = FXCollections.observableArrayList();
ps = con.prepareStatement("select clientid,clientname,accountnumber,bankaddress,accounttype,ifsccode,branchname,bankname from bank");
rs = ps.executeQuery();
while(rs.next()){
bank.add(new bankmodal(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)));
}
tab_uniqueno.setCellValueFactory(new PropertyValueFactory("tab_uniqueno"));
tab_accname.setCellValueFactory(new PropertyValueFactory("tab_accname"));
tab_accname.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_accname.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_accname(t.getNewValue());
}
}
);
tab_bankaccno.setCellValueFactory(new PropertyValueFactory("tab_bankaccno"));
tab_bankaccno.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_bankaccno.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_bankaccno(t.getNewValue());
}
}
);
tab_address.setCellValueFactory(new PropertyValueFactory("tab_address"));
tab_address.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_address.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_address(t.getNewValue());
}
}
);
tab_accounttype.setCellValueFactory(new PropertyValueFactory("tab_accounttype"));
tab_accounttype.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_accounttype.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_accounttype(t.getNewValue());
}
}
);
tab_ifsc.setCellValueFactory(new PropertyValueFactory("tab_ifsc"));
tab_ifsc.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_ifsc.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_ifsc(t.getNewValue());
}
}
);
tab_micr.setCellValueFactory(new PropertyValueFactory("tab_micr"));
tab_micr.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_micr.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_micr(t.getNewValue());
}
}
);
tab_bankname.setCellValueFactory(new PropertyValueFactory("tab_bankname"));
tab_bankname.setCellFactory(TextFieldTableCell.<bankmodal>forTableColumn());
tab_bankname.setOnEditCommit(
new EventHandler<CellEditEvent<bankmodal, String>>() {
@Override
public void handle(CellEditEvent<bankmodal, String> t) {
((bankmodal) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTab_bankname(t.getNewValue());
}
}
);
bankDetails.setItems(null);
bankDetails.setItems(bank);
}
catch(Exception e)
{e.printStackTrace();}
}
主类名称:Main.java
答案 0 :(得分:0)
看看这个方法:
public void setTab_bankname(String tab_bankname) {
tab_bankname.set(tab_bankname);
}
此处,tab_bankname
可以是您的参数String tab_bankname
,也可以是您的成员字段public final StringProperty tab_bankname
,目前还不清楚您所引用的内容。
您收到以下错误运行时:
对于String
类型,方法set(String)未定义
因为在运行时tab_bankname
有一个参数引用,String
没有方法set(String)
。
通常,IDE会在编译时给出相同的错误,以避免运行时异常。
第一个解决方案:使用“this”(并用此更新所有的getter)
这可确保this.tab_bankname
为StringProperty
public void setTab_bankname(String tab_bankname) {
this.tab_bankname.set(tab_bankname);
}
第二个解决方案:不要在参数中使用与任何成员相同的名称
public void setTab_bankname(String _bankName) {
tab_bankname.set(_bankName);
}
您还可以快速阅读Code Conventions for the Java TM Programming Language以避免将来出现这种混淆。