gpt4 book ai didi

java - 内容出现在自定义单元格工厂的空行中

转载 作者:搜寻专家 更新时间:2023-10-31 19:55:47 24 4
gpt4 key购买 nike

如果行 value.setCellFactory(NUMBER_CELL_FACTORY); 被注释掉,下面的程序工作正常。

但是,如果包含它,TableView 的行为会很奇怪* - 如果您只是复制代码,启动它并在 + 按钮上单击几次,您将看到内容出现在空行中表格底部。

我错过了什么吗?

*在 JavaFx 8 - b106 上测试。

import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TestFX extends Application {

@Override
public void start(final Stage stage) throws Exception {
MyTable table = new MyTable();

Scene scene = new Scene(table);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(TestFX.class);
}

static class MyTable extends AnchorPane {

private static final Callback NUMBER_CELL_FACTORY = (p) -> new NumberTableCell<>();

private final Button addRow = new Button("+");
private final TableView<Item> table = new TableView<>();

MyTable() {
super();
initTableView();
addRow.setOnMouseClicked((e) -> addItem());
VBox vbox = new VBox();
vbox.getChildren().addAll(addRow, table);
getChildren().add(vbox);
}

public void addItem() {
table.getItems().addAll(new Item());
}

private void initTableView() {
TableColumn<Item, Double> value = new TableColumn<>("Value");
value.setCellValueFactory(new PropertyValueFactory<>("value"));
value.setCellFactory(NUMBER_CELL_FACTORY);

table.getColumns().add(value);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
}

public static class Item {
private final DoubleProperty value = new SimpleDoubleProperty();

public DoubleProperty valueProperty() {
return value;
}
}

static class NumberTableCell<T> extends TableCell<T, Double> {
private final Color fill = Color.LIGHTGREY;

@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if (empty) return;
setText(String.valueOf(item));
setTextFill(fill);
}
}
}

最佳答案

我在 Win7 Java7b108 上试过你的示例程序,它似乎与 NumberTableCell 一起工作,但它仍然有问题。

问题是单元格被重复使用,因此如果单元格由于任何原因变空,您需要清除您在单元格中设置的所有自定义值,以便它呈现为默认的空单元格。下面的代码演示了如何执行此操作:

    @Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setTextFill(null);
return;
}
setText(String.valueOf(item));
setTextFill(fill);
}

这是对您的程序的一个小更新,可以更轻松地重现问题并演示修复。如果您注释掉 setText(null) 和 setTextFill(null) 行,然后按几次 + 按钮,然后开始按 - 按钮,您将看到 TableView 并没有真正反射(reflect)底层列表的状态。但是有了 null setter,一切都正确地同步了。这是一个陷阱...

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TestFX extends Application {

@Override
public void start(final Stage stage) throws Exception {
MyTable table = new MyTable();

System.getProperties().list(System.out);

Scene scene = new Scene(table);

stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {
launch(TestFX.class);
}

static class MyTable extends AnchorPane {

private static final Callback NUMBER_CELL_FACTORY = (p) -> new NumberTableCell<>();

private final Button addRow = new Button("+");
private final Button removeRow = new Button("-");
private final TableView<Item> table = new TableView<>();

MyTable() {
super();
initTableView();
addRow.setOnMouseClicked((e) -> addItem());
removeRow.setOnMouseClicked((e) -> removeItem());
VBox vbox = new VBox(5);
vbox.getChildren().addAll(addRow, removeRow, table);
getChildren().add(vbox);
}

public void addItem() {
table.getItems().addAll(new Item());
}

public void removeItem() {
if (table.getItems().size() > 0) {
table.getItems().remove(0);
}
}

private void initTableView() {
TableColumn<Item, Double> value = new TableColumn<>("Value");
value.setCellValueFactory(new PropertyValueFactory<>("value"));
value.setCellFactory(NUMBER_CELL_FACTORY);

table.getColumns().add(value);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
}

public static class Item {
private final DoubleProperty value = new SimpleDoubleProperty();

public DoubleProperty valueProperty() {
return value;
}
}

static class NumberTableCell<T> extends TableCell<T, Double> {
private final Color fill = Color.LIGHTGREY;

@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setTextFill(null);
return;
}
setText(String.valueOf(item));
setTextFill(fill);
}
}
}

关于java - 内容出现在自定义单元格工厂的空行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19182357/

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