在 JavaFX 中创建行索引列

2024-02-17

我有一个 JavaFX TableView,我正在用 ObservableList 的任务填充它。我一直在尝试创建一个显示每行索引的列,该索引用作表中任务的 ID,但我尝试过该方法here https://stackoverflow.com/questions/13449707/automatic-row-numbering-in-javafx-table以及其他来源的类似方法,但收效甚微。

我的参考代码,没有任何表面错误(据 Eclipse 所知):

    @FXML private TableColumn<Task, String> taskIndexCol;
    Callback<TableColumn<Task, String>, TableCell<Task, String>> cb =
            new Callback<TableColumn<Task, String>, TableCell<Task, String>>(){
                @Override
                public TableCell<Task, String> call(TableColumn<Task, String> col) {
                    TableCell<Task, String> cell = new TableCell<Task, String>() {
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (item == null) {
                                setText("");
                            } else {
                                setText(getIndex()+"");
                            }
                        }
                    };
                    return cell;
                }
    };

    taskIndexCol.setCellFactory(cb);

目前,当我尝试设置列的 CellFactory 时,我的代码给出了 NullPointerException。我已经尝试使用填充的任务列表,但这没有帮助。我被难住了很长一段时间——理论上这应该很容易,因为它只是对行进行编号?感觉就像我正在跳过一百万个圈子去做一些令人沮丧的简单事情。

编辑:最后一行给了我 NPE。


不可能说出空指针异常的原因,因为您没有显示堆栈跟踪,没有识别引发异常的行,也没有发布足够的代码(回调中的任何代码都不能引发空指针异常,所以其他地方出了问题)。

对于您的实际单元实现,您没有显示您是否有cellValueFactory设置在柱上。如果你不这样做,那么item will always be null,因此您将永远不会在该列的单元格中看到任何文本。您可以检查empty属性(或方法参数)作为检查单元格是否位于空行或包含实际数据的行中的一种方法。 (请注意,这意味着该列实际上根本不需要提供任何数据:它可以是TableColumn<Task, Void>.)

此外,依赖使用可能更安全updateIndex(...)代替updateItem(...). updateIndex当索引改变时保证被调用;如果你实施updateItem您假设索引是在项目之前设置的,这意味着您依赖于实现细节。

如果您使用 Java 8 lambda 表达式,您的代码会更短且更容易阅读:

taskIndexCol.setCellFactory(col -> new TableCell<Task, String>() {
    @Override
    protected void updateIndex(int index) {
        super.updateIndex(index);
        if (isEmpty() || index < 0) {
            setText(null);
        } else {
            setText(Integer.toString(index));
        }
    }
});

或者替代地

taskIndexCol.setCellFactory(col -> {
    TableCell<Task, String> cell = new TableCell<>();
    cell.textProperty().bind(Bindings.when(cell.emptyProperty())
        .then("")
        .otherwise(cell.indexProperty().asString()));
    return cell ;
});

这是一个 SSCCE:

import java.util.function.Function;

import static javafx.beans.binding.Bindings.when ;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableViewWithIndexColumn extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> table = new TableView<>();
        table.setEditable(true);

        table.getItems().addAll(
                new Person("Jacob", "Smith", "[email protected] /cdn-cgi/l/email-protection"),
                new Person("Isabella", "Johnson",
                        "[email protected] /cdn-cgi/l/email-protection"),
                new Person("Ethan", "Williams", "[email protected] /cdn-cgi/l/email-protection"),
                new Person("Emma", "Jones", "[email protected] /cdn-cgi/l/email-protection"),
                new Person("Michael", "Brown", "[email protected] /cdn-cgi/l/email-protection"));


        TableColumn<Person, String> firstNameCol = createColumn("First Name",
                Person::firstNameProperty, 150);
        TableColumn<Person, String> lastNameCol = createColumn("Last Name",
                Person::lastNameProperty, 150);
        TableColumn<Person, String> emailCol = createColumn("Email",
                Person::emailProperty, 150);

        // index column doesn't even need data...
        TableColumn<Person, Void> indexCol = createColumn("Index", person -> null, 50);

        // cell factory to display the index:
//        indexCol.setCellFactory(col -> {
//            
//            // just a default table cell:
//            TableCell<Person, Void> cell = new TableCell<>();
//            
//            cell.textProperty().bind(
//                when(cell.emptyProperty())
//                    .then("")
//                    .otherwise(cell.indexProperty().asString()));
//            
//            return cell ;
//        });

        indexCol.setCellFactory(col -> new TableCell<Person, Void>() {
            @Override
            public void updateIndex(int index) {
                super.updateIndex(index);
                if (isEmpty() || index < 0) {
                    setText(null);
                } else {
                    setText(Integer.toString(index));
                }
            }
        });

        table.getColumns().add(indexCol);
        table.getColumns().add(firstNameCol);
        table.getColumns().add(lastNameCol);
        table.getColumns().add(emailCol);

        primaryStage.setScene(new Scene(new BorderPane(table), 600, 400));
        primaryStage.show();
    }

    private <S, T> TableColumn<S, T> createColumn(String title,
            Function<S, ObservableValue<T>> property, double width) {
        TableColumn<S, T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        col.setPrefWidth(width);
        return col;
    }

    public static class Person {
        private final StringProperty firstName = new SimpleStringProperty();
        private final StringProperty lastName = new SimpleStringProperty();
        private final StringProperty email = new SimpleStringProperty();

        public Person() {
            this("", "", "");
        }

        public Person(String firstName, String lastName, String email) {
            setFirstName(firstName);
            setLastName(lastName);
            setEmail(email);
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final java.lang.String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final java.lang.String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final java.lang.String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final java.lang.String lastName) {
            this.lastNameProperty().set(lastName);
        }

        public final StringProperty emailProperty() {
            return this.email;
        }

        public final java.lang.String getEmail() {
            return this.emailProperty().get();
        }

        public final void setEmail(final java.lang.String email) {
            this.emailProperty().set(email);
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 JavaFX 中创建行索引列 的相关文章

随机推荐