更新javafx中突出显示的单元格
我使用两个文本字段和一个按钮向两列表中添加条目 如果我添加了一个新条目,表将立即更新:更新javafx中突出显示的单元格,java,javafx,tableview,Java,Javafx,Tableview,我使用两个文本字段和一个按钮向两列表中添加条目 如果我添加了一个新条目,表将立即更新: private void addBtn(ActionEvent event) { Test o = new Test(); o.setTitle(title.getText()); o.setCount(Integer.parseInt(count.getText())); mainApp.getData().add(o); } 在第二步中,我
private void addBtn(ActionEvent event) {
Test o = new Test();
o.setTitle(title.getText());
o.setCount(Integer.parseInt(count.getText()));
mainApp.getData().add(o);
}
在第二步中,我添加了一个额外的按钮来修改突出显示的计数单元格:
private void editBtn(ActionEvent event) {
Test o = getSelection();
o.setCount(Integer.parseInt(count.getText()));
mainApp.getData().set(tablePosition, o);
}
如果单击按钮,单元格将更新值,但在表中不可见。如果我再次单击该按钮,它将更新表
要检查高亮显示的行,我使用以下函数:
private final ListChangeListener<Test> selector = new ListChangeListener<Test>() {
@Override
public void onChanged(ListChangeListener.Change<? extends Test> c) {
setSelection();
}
};
public Test getSelection() {
if (testTable != null) {
List<Test> table = testTable.getSelectionModel().getSelectedItems();
if (table.size() == 1) {
final Test selection = table.get(0);
return selection;
}
}
return null;
}
private void setSelection() {
final Test o = getSelection();
tablePosition = mainApp.getData().indexOf(o);
if (o != null) {
title.setText(o.getTitle());
count.setText(o.getCount().toString());
}
}
如何使编辑按钮立即更新单元格值?假设您使用
PropertyValueFactory
作为表列的单元格工厂,您需要提供属性访问器方法,以便PropertyValueFactory
提供的表单元格可以侦听这些属性的更改
使用JavaFX属性模型的一个正确实现如下
public class Test {
private final IntegerProperty count = new SimpleIntegerProperty(this, "count", 0);
private final StringProperty title = new SimpleStringProperty(this, "title", "");
public final int getCount() {
return count.get();
}
public final void setCount(int count) {
this.count.set(count);
}
public IntegerProperty countProperty() {
return count ;
}
public final String getTitle() {
return title.get();
}
public final void setTitle(String title) {
this.title.set(title);
}
public StringProperty titleProperty() {
return title ;
}
}
这样,以下方法将正确更新表中选定的行:
private void editBtn(ActionEvent event) {
Test o = testTable.getSelectionModel().getSelectedItem();
if (o != null) {
o.setCount(Integer.parseInt(count.getText()));
}
}
如果这不能解决您的问题,我建议您完全编辑您的问题,并提供一个演示问题的示例 两个澄清:首先,什么是
Object.setCount()
和Object.setTitle()
。这些方法并不存在。第二,你不能只使用testTable.getSelectionModel().selectedItemProperty()
来跟踪所选项目吗?嗨,James\u D。对不起,我只是觉得我已经替换了一些不应该替换的东西。“对象”实际上是“测试”。这是我的模型课。我已经在上面的问题中添加了部分内容。对于你的第二个评论,我有很多问题,让选择工作,但我的代码上面这似乎工作得相当好。我唯一的问题就是编辑/更新。如果我高亮显示一行,编辑计数单元格并点击编辑按钮,它将更新值,但不会在表中显示更新后的值,除非我再次单击该按钮。是否有属性访问器(即public SimpleStringProperty titleProperty(){…}
和public SimpleIntegerProperty countProperty(){…}
方法)在你的测试
课上?我已经把我的测试课复制到了我的问题中。我有正常的方法。谢谢。我将以下内容用于单元格工厂:titleCol.setCellValueFactory(新属性值工厂(“标题”)代码>countCol.setCellValueFactory(新属性值工厂(“计数”)代码>我按照您的建议更新了我的测试类代码,但我仍然有点困惑,接下来如何更改我的编辑按钮方法?!
public class Test {
private final IntegerProperty count = new SimpleIntegerProperty(this, "count", 0);
private final StringProperty title = new SimpleStringProperty(this, "title", "");
public final int getCount() {
return count.get();
}
public final void setCount(int count) {
this.count.set(count);
}
public IntegerProperty countProperty() {
return count ;
}
public final String getTitle() {
return title.get();
}
public final void setTitle(String title) {
this.title.set(title);
}
public StringProperty titleProperty() {
return title ;
}
}
private void editBtn(ActionEvent event) {
Test o = testTable.getSelectionModel().getSelectedItem();
if (o != null) {
o.setCount(Integer.parseInt(count.getText()));
}
}