gpt4 book ai didi

java - javafx2中的可编辑表

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:56 24 4
gpt4 key购买 nike

我使用 javafx2 在运行时创建了带有可编辑单元格的表格。按 Enter 键时,表格单元格已编辑但未更新,并且表类 set 方法在 Bankmodal.java 中的 tab_bankname.set(tab_bankname); 位置显示错误,错误消息

The method set(String) is undefined for the type 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);
}
}

Controller 类名称: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

最佳答案

看看这个方法:

public void setTab_bankname(String tab_bankname) {
tab_bankname.set(tab_bankname);
}

这里,tab_bankname可以是你的参数String tab_bankname或者你的成员字段public final StringProperty tab_bankname,目前还不清楚你引用的是什么。

运行时出现以下错误:

The method set(String) is undefined for the type String

因为在运行时tab_bankname有一个对参数的引用,它是一个String,它没有方法set(String)

通常,您的 IDE 会在编译时给出相同的错误,以避免运行时异常。

为了避免这种困惑:

第一个解决方案:使用“this”(并用它更新所有的 setter/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以避免将来出现此类困惑。

关于java - javafx2中的可编辑表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37535415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com