TableView编辑列JAVA FX [英] TableView edit column JAVA FX
问题描述
我在java FX中创建一个TableView,我正在尝试使列可编辑。这是我在表中列名的代码:
i am creating a TableView in java FX and i'm trying to make a column editable . this is my code for the column name in the table :
TableColumn name = new TableColumn("name");
name.setMinWidth(150);
name.setCellValueFactory(
new PropertyValueFactory<Phone,String>("name")
);
name.setCellFactory(TextFieldTableCell.forTableColumn());
name.setOnEditCommit( new EventHandler<CellEditEvent<Phone,String>>() {
@Override
public void handle(CellEditEvent<Phone,String> t) {
((Phone) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setName(t.getNewValue());
}
});
我有2个错误说无法找到getTableView()和getTablePosition()
i跟着oracle教程 oracle tutorial
i have 2 errors saying cannot find getTableView() and getTablePosition() i followed the oracle tutorial oracle tutorial
Thx为您提供帮助。
Thx for your help.
推荐答案
您几乎肯定有错误的导入。确保你正在导入
You almost certainly have the wrong import. Make sure you are importing
import javafx.scene.control.TableColumn.CellEditEvent ;
您还应该避免使用原始类型;即你应该替换
You should also avoid using raw types; i.e. you should replace
TableColumn name = new TableColumn("name");
with
TableColumn<Phone, String> name = new TableColumn<>("name");
(同样请确保使用 TableView< Phone>
如果你还没有这样做的话。)
(and similarly make sure you use TableView<Phone>
if you are not doing so already).
这篇关于TableView编辑列JAVA FX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!