如何直接在javafx TableView中创建新记录?

时间:2015-05-10 17:16:17

标签: mysql javafx tableview

我正在使用JDBC和MySQL。

我可以创建文本字段并将其内容作为MySQL表中的记录输入(然后填充相应的javafx TableView)。

我想知道用户是否可以通过单击TableView单元格直接向TableView添加新记录。

这样做是否是一个好习惯? TableView显示销售发票的详细信息,如项目名称,销售数量,费率等。

1 个答案:

答案 0 :(得分:1)

我刚刚发布了这个例子,但我现在找不到了。

执行此操作的一种方法是在表格的项目列表末尾添加“空白”项。注册一个监听器,以便在用户编辑该值时,最后添加一个新的空白项目。

这是一个示例(它还有一些与默认值不同的编辑功能)。

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TableViewSingleClickEditTest extends Application {


    @Override
    public void start(Stage primaryStage) {


        TableView<Person> table = new TableView<>();
        table.setEditable(true);

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

        TableColumn<Person, Void> indexCol = new TableColumn<>("");
        indexCol.setCellFactory(col -> {
            TableCell<Person, Void> cell = new TableCell<>();
            cell.textProperty().bind(Bindings.createStringBinding(() -> {
                if (cell.getIndex() >= 0 && cell.getIndex() < table.getItems().size() - 1) {
                    return Integer.toString(cell.getIndex() + 1);
                } else if (cell.getIndex() == table.getItems().size() - 1) {
                    return "*" ;
                } else return "" ;
            }, cell.indexProperty(), table.getItems()));
            return cell ;
        });
        indexCol.setPrefWidth(32);

        table.getItems().addAll(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@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")
        );

        ChangeListener<String> lastPersonTextListener = new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> obs, String oldText, String newText) {
                if (oldText.isEmpty() && ! newText.isEmpty()) {
                    Person lastPerson = table.getItems().get(table.getItems().size() - 1);
                    lastPerson.firstNameProperty().removeListener(this);
                    lastPerson.lastNameProperty().removeListener(this);
                    lastPerson.emailProperty().removeListener(this);
                    Person newBlankPerson = new Person("", "", "");
                    newBlankPerson.firstNameProperty().addListener(this);
                    newBlankPerson.lastNameProperty().addListener(this);
                    newBlankPerson.emailProperty().addListener(this);
                    table.getItems().add(newBlankPerson);
                }
            }
        };

        Person blankPerson = new Person("", "", "");
        blankPerson.firstNameProperty().addListener(lastPersonTextListener);
        blankPerson.lastNameProperty().addListener(lastPersonTextListener);
        blankPerson.emailProperty().addListener(lastPersonTextListener);
        table.getItems().add(blankPerson);

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

        VBox root = new VBox(15, table);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 800, 600);

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


    private TableColumn<Person, String> createCol(String title, 
            Function<Person, ObservableValue<String>> mapper, double size) {

        TableColumn<Person, String> col = new TableColumn<>(title);

        col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));

        Callback<TableColumn<Person, String>, TableCell<Person, String>> defaultCellFactory
            = TextFieldTableCell.forTableColumn();

        col.setCellFactory(column -> {
            TableCell<Person, String> cell = defaultCellFactory.call(column);
            cell.setOnMouseClicked(e -> {
                if (! cell.isEditing() && ! cell.isEmpty()) {
                    cell.getTableView().edit(cell.getIndex(),  column);
                }
            });
            return cell ;
        });

        col.setPrefWidth(size);


        return col ;
    }

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

        public Person(String firstName, String lastName, String email) {
            this.firstName.set(firstName);
            this.lastName.set(lastName);
            this.email.set(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);
    }
}