3

我有这个可编辑的 JavaFX 表来编辑数据:

public class DataTable {

    public void initTable(Stage primaryStage, Group root, Scene scene) {

        tableView.setEditable(true);
        Callback<TableColumn, TableCell> cellFactory =
                new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn p) {
                return new EditingCell();
            }
        };

        TableColumn columnMonth = new TableColumn("Month");
        columnMonth.setCellValueFactory(
                new PropertyValueFactory<Record, String>("fieldMonth"));

        TableColumn columnValue = new TableColumn("Value");
        columnValue.setCellValueFactory(
                new PropertyValueFactory<Record, Double>("fieldValue"));

        //--- Add for Editable Cell of Value field, in Double
        columnValue.setCellFactory(cellFactory);
        columnValue.setOnEditCommit(
                new EventHandler<TableColumn.CellEditEvent<Record, Double>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<Record, Double> t) {
                ((Record) t.getTableView().getItems().get(
                        t.getTablePosition().getRow())).setFieldValue(t.getNewValue());
            }
        });
        //---

        tableView.setItems(dataList);
        tableView.getColumns().addAll(columnMonth, columnValue);

        VBox vBox = new VBox();
        vBox.setSpacing(10);
        vBox.getChildren().add(tableView);
        vBox.setLayoutX(410);
        vBox.setLayoutY(34);

        root.getChildren().add(vBox);


    }

    public class Record {

        private SimpleStringProperty fieldMonth;
        private SimpleDoubleProperty fieldValue;

        Record(String fMonth, double fValue) {
            this.fieldMonth = new SimpleStringProperty(fMonth);
            this.fieldValue = new SimpleDoubleProperty(fValue);
        }

        public String getFieldMonth() {
            return fieldMonth.get();
        }

        public double getFieldValue() {
            return fieldValue.get();
        }

        public void setFieldMonth(String fMonth) {
            fieldMonth.set(fMonth);
        }

        public void setFieldValue(Double fValue) {
            fieldValue.set(fValue);
        }
    }
    private TableView<Record> tableView = new TableView<>();
    private ObservableList<Record> dataList =
            FXCollections.observableArrayList(
            new Record("January", 100),
            new Record("February", 200),
            new Record("March", 50),
            new Record("April", 75),
            new Record("May", 110),
            new Record("June", 300),
            new Record("July", 111),
            new Record("August", 30),
            new Record("September", 75),
            new Record("October", 55),
            new Record("November", 225),
            new Record("December", 99));

    class EditingCell extends TableCell<Record, Double> {

        private TextField textField;

        public EditingCell() {
        }

        @Override
        public void startEdit() {
            super.startEdit();

            if (textField == null) {
                createTextField();
            }

            setGraphic(textField);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            textField.selectAll();
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();

            setText(String.valueOf(getItem()));
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        }

        @Override
        public void updateItem(Double item, boolean empty) {
            super.updateItem(item, empty);

            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                        textField.setText(getString());
                    }
                    setGraphic(textField);
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                } else {
                    setText(getString());
                    setContentDisplay(ContentDisplay.TEXT_ONLY);
                }
            }
        }

        private void createTextField() {
            textField = new TextField(getString());
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
            textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        commitEdit(Double.parseDouble(textField.getText()));
                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });
        }

        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }
    }
}

我必须创建可以显示数据库表中的数据并编辑表的小程序。当我单击一个表时,我遇到了这个问题,我必须从 JPA 对象生成动态表视图,所以我每次都有一个表但有不同的列。我可以以某种方式使用来自 JPA 的数据生成每个时间表并根据案例自动更新列吗?

4

1 回答 1

7

您可以使用反射来实现这一点。例如,解析数据类的方法并为每个以 结尾的方法创建列Property

public class SmartTableView extends Application {

    private void populateColumns(Class clazz, TableView table) {
        for (Method method : clazz.getMethods()) {
            String name = method.getName();
            if (name.endsWith("Property")) {
              String propName = name.replace("Property", "");

              TableColumn column = new TableColumn(propName);
              column.setCellValueFactory(new PropertyValueFactory<>(propName));
              table.getColumns().add(column);
            }
        }
    }

    @Override
    public void start(Stage primaryStage) {
        final ObservableList<Person> data = FXCollections.observableArrayList(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Dow", "isabella.dow@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com"));

        TableView table = new TableView(data);
        populateColumns(Person.class, table);

        Scene scene = new Scene(table, 300, 250);
        primaryStage.setTitle("Hello");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

        public StringProperty emailProperty() {
            return lastName;
        }
    }
}
于 2013-04-24T10:01:43.397 回答