JavaFX:如何刷新表?
我对JavaFX TableView中的刷新行样式有问题 java版本“1.8.0_51” Java(TM)SE运行时环境(build 1.8.0_51-b16) Java HotSpot(TM)服务器虚拟机(构建25.51-b03,混合模式) 逻辑:JavaFX:如何刷新表?,java,javafx,Java,Javafx,我对JavaFX TableView中的刷新行样式有问题 java版本“1.8.0_51” Java(TM)SE运行时环境(build 1.8.0_51-b16) Java HotSpot(TM)服务器虚拟机(构建25.51-b03,混合模式) 逻辑: 将数据加载到tableView 通过setRowFactory将新样式设置为行 将新数据加载到表中 刷新新表行的样式。(不为我工作。) 如何重新加载行的样式 我的代码片段: import javafx.application.Applicati
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class RowFactoryAndOptimisationDemo extends Application {
VBox root;
public ObservableList<RFDDomain> data = FXCollections.observableArrayList();
public TableView<RFDDomain> tableView = new TableView<RFDDomain>();
@Override
public void start(Stage stage) throws Exception {
root = new VBox();
root.autosize();
Scene scene = new Scene(root, Color.LINEN);
stage.setTitle("Row Factory Demo");
stage.setWidth(500);
stage.setHeight(300);
stage.setScene(scene);
stage.show();
configureTable();
}
public static void main(String[] args) {
Application.launch(args);
}
private void recheck_table() {
tableView.setRowFactory(new Callback<TableView<RFDDomain>, TableRow<RFDDomain>>() {
@Override
public TableRow<RFDDomain> call(TableView<RFDDomain> paramP) {
return new TableRow<RFDDomain>() {
@Override
protected void updateItem(RFDDomain paramT, boolean paramBoolean) {
super.updateItem(paramT, paramBoolean);
if (!isEmpty()) {
String style = "-fx-control-inner-background: #007F0E;"
+ "-fx-control-inner-background-alt: #007F0E;";
setStyle(style);
}
}
};
}
});
}
@SuppressWarnings("unchecked")
private void configureTable() {
int id =1;
for (int i = 1; i <= 1; i++) {
data.add(new RFDDomain(id++,"First Row", "This is for check.", 1));
data.add(new RFDDomain(id++,"Second Row", null, 2));
data.add(new RFDDomain(id++,"Third Row", "This is for check.", 3));
data.add(new RFDDomain(id++,"Fourth Row", "dil", 4));
}
tableView.setItems(data);
recheck_table();
TableColumn<RFDDomain, Integer> column0 = new TableColumn<RFDDomain, Integer>("Id");
column0.setCellValueFactory(new PropertyValueFactory<RFDDomain, Integer>("id"));
TableColumn<RFDDomain, String> column1 = new TableColumn<RFDDomain, String>("Title");
column1.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));
TableColumn<RFDDomain, String> column2 = new TableColumn<RFDDomain, String>("Description");
column2.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("description"));
TableColumn<RFDDomain, Number> column3 = new TableColumn<RFDDomain, Number>("Status");
column3.setPrefWidth(55);
column3.setCellValueFactory(new PropertyValueFactory<RFDDomain, Number>("status"));
TableColumn<RFDDomain, String> column4 = new TableColumn<RFDDomain, String>("Action");
column4.setCellValueFactory(new PropertyValueFactory<RFDDomain, String>("name"));
tableView.getColumns().addAll(column0, column1, column2, column3, column4);
this.root.getChildren().add(tableView);
Button button1 = new Button("Load new Data");
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
data.clear();
data.removeAll(data);
data.add(new RFDDomain(1,"First Row", "This is for check.", 1));
data.add(new RFDDomain(2,"Second Row", null, 2));
tableView.setItems(data);
recheck_table();
}
});
this.root.getChildren().add(button1);
}
/**
* Domain Model for this demo.
*/
public class RFDDomain {
private SimpleIntegerProperty id = new SimpleIntegerProperty();
private SimpleStringProperty name = new SimpleStringProperty();
private SimpleStringProperty description = new SimpleStringProperty();
private SimpleIntegerProperty status = new SimpleIntegerProperty();
public RFDDomain(int id,String name, String desc, int status) {
this.id.set(id);
this.name.set(name);
this.description.set(desc);
this.status.set(status);
}
public int getId() {
return id.get();
}
public SimpleIntegerProperty idProperty() {
return id;
}
public String getDescription() {
return description.get();
}
public SimpleStringProperty descriptionProperty() {
return description;
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public int getStatus() {
return status.get();
}
public SimpleIntegerProperty statusProperty() {
return status;
}
}
}
导入javafx.application.application;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类RowFactoryandOptimizationDemo扩展了应用程序{
VBox根;
公共ObservableList data=FXCollections.observableArrayList();
public TableView TableView=新建TableView();
@凌驾
public void start(Stage)引发异常{
root=新的VBox();
root.autosize();
场景=新场景(根、颜色、亚麻布);
stage.setTitle(“世界其他地区工厂演示”);
舞台设置宽度(500);
舞台设置高度(300);
舞台场景;
stage.show();
configureTable();
}
公共静态void main(字符串[]args){
应用程序启动(args);
}
专用作废复核表(){
tableView.setRowFactory(新回调(){
@凌驾
公共TableRow调用(TableView参数){
返回新的TableRow(){
@凌驾
受保护的void updateItem(RFDDomain参数,布尔参数){
super.updateItem(paramT,paramBoolean);
如果(!isEmpty()){
String style=“-fx控件内部背景:#007F0E;”
+“-fx控件内部背景高度:#007F0E;”;
设置样式(样式);
}
}
};
}
});
}
@抑制警告(“未选中”)
私有void configureTable(){
int-id=1;
对于(inti=1;i这是我一年前发现的一个小技巧
myTableView.getColumns().get(0).setVisible(false);
myTableView.getColumns().get(0).setVisible(true);
这可能不是最好的方法,但对我来说很有效…试试tableView.refresh()
。自Java8U60以来,它在JavaFX中就已经可用了。可能不再相关,只需添加
else { setStyle(null) }
到自定义工厂中的updateItem
方法-正在发生的事情是,只创建屏幕上绝对需要显示的单元格(防止创建不必要的许多UI元素)。当您通过其updateItem
方法滚动表时,这些单元格会被重复使用。有时(在您的情况下),现有单元格将更新为null
,以指示该单元格应为空。您尚未实现这种情况,但应该,并且应该清除该单元格的样式。在您的代码中,您在何处将现在为空的行设置为纯背景?按钮1.setOnAction-仅设置2行,但不会重新加载3-4行的样式。I在您的代码中可以看到,您在哪里为包含数据的行设置了样式,但我看不到您在哪里将已更改的行设置为普通样式,以便它们不包含数据。顺便说一句,您为什么要调用recheck_table()?您只需要设置行工厂一次。哦,感谢正确的流…我添加了:else{this.setStyle(“单元格索引单元格表行单元格”);}到updateItem方法,其工作看起来正确。无需再添加任何不正确的答案(刷新、隐藏/显示列、重置项…确保您可以想出更多):基本问题是updateItem的实现不正确(没有其他块可恢复正常),请参阅OP的最后一条注释不正确,如果其他行中有更改,则该方法会将表完整返回到加载数据的最后一个状态否,永远不要使用刷新-对于极为罕见的上下文(如0.0000099%)这是一种紧急回退这是正常的自动更新机制无法处理的!@atlantis设置有问题,对我来说很好。似乎这是唯一(简单的)一种立即反映表视图备份列表中元素的更改的方法,因为可观察列表只显示列表上的更改,而不显示其元素上的更改。@SilverBullett重复:如果有必要,则说明您的设置中有错误-在您的情况下是“某些错误”“列表中似乎缺少提取器(通知列表元素属性的更改)