插入表视图 JavaFX



直到我向某人展示一个工作原型,我才想到。他们询问是否可以在当前表格(即Excel)上方的表格视图中添加新行。

有问题的应用程序正在生成旅行行程,因此如果有人忘记输入旅程的某个特定部分,他们目前将不得不删除以下行并照常继续(有点痛苦)。

在Google上进行一些搜索并没有真正帮助它只是指向在以前的行之后添加新行,可以做到这一点吗?

例:

    04FEB London to New York
    --> insert here (forgot new york to san fran).
    23FEB San Francisco to New York
    23FEB New York to London 

非常感谢

基本上,您需要做的就是在表的项目列表中适当位置插入一个新项目:

table.getItems().add(index, newItem);

您可以添加一些用户功能,例如开始对新项目进行编辑,或者根据需要根据周围的数据对数据进行最佳猜测初始化。

这是一个非常快速(总共 15 分钟的编码,所以显然它可能包含一些错误)示例:我将"在表格行之前插入行"和"在之后插入行"功能放入表格行的上下文菜单中,但显然您可以将其挂接到您想要的任何用户操作。

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.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ItineraryBuilder extends Application {
    private TableView<ItineraryLeg> table;
    private void insert(int rowIndex, String origin, String destination, int editCol) {
        table.getItems().add(rowIndex, new ItineraryLeg(origin, destination));
        table.getSelectionModel().clearAndSelect(rowIndex);
        table.edit(rowIndex, table.getColumns().get(editCol));
    }
    @Override
    public void start(Stage primaryStage) {
        table = new TableView<>();
        table.setEditable(true);
        table.getColumns().add(column("Origin", ItineraryLeg::originProperty));
        table.getColumns().add(column("Destination", ItineraryLeg::destinationProperty));
        table.getItems().addAll(
                new ItineraryLeg("London","New York"),
                new ItineraryLeg("San Francisco","New York"),
                new ItineraryLeg("New York", "London"));

        table.setRowFactory(tv -> {
            TableRow<ItineraryLeg> row = new TableRow<>();
            MenuItem addBefore = new MenuItem("Insert leg before");
            MenuItem addAfter = new MenuItem("Insert leg after");
            ContextMenu menu = new ContextMenu(addBefore, addAfter);
            addBefore.setOnAction(e -> {
                String origin = row.getIndex() == 0 ? "" : table.getItems().get(row.getIndex()-1).getDestination();
                String destination = table.getItems().get(row.getIndex()).getOrigin() ;
                insert(row.getIndex(), origin, destination, 0);
            });
            addAfter.setOnAction(e -> {
                String origin = table.getItems().get(row.getIndex()).getDestination();
                String destination = row.getIndex() == table.getItems().size()-1 
                        ? "" 
                        : table.getItems().get(row.getIndex()+1).getOrigin() ;
                insert(row.getIndex()+1, origin, destination, 1);
            });
            row.contextMenuProperty().bind(
                    Bindings.when(row.emptyProperty())
                    .then((ContextMenu)null)
                    .otherwise(menu));
            return row ;
        });
        Label label = new Label("Build Itinerary");
        Button add = new Button("Add leg");
        HBox controls = new HBox(5, label, add);
        controls.setPadding(new Insets(10));
        add.setOnAction(e -> insert(table.getItems().size(), "Origin", "Destination", 0));
        BorderPane root = new BorderPane(table);
        root.setTop(controls);
        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static <S> TableColumn<S,String> column(String title, Function<S, StringProperty> property) {
        TableColumn<S,String> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        col.setCellFactory(TextFieldTableCell.forTableColumn());
        col.setPrefWidth(200);
        return col ;
    }
    public static class ItineraryLeg {
        private final StringProperty origin = new SimpleStringProperty();
        private final StringProperty destination = new SimpleStringProperty();
        public ItineraryLeg(String origin, String destination) {
            setOrigin(origin);
            setDestination(destination);
        }
        public final StringProperty originProperty() {
            return this.origin;
        }

        public final String getOrigin() {
            return this.originProperty().get();
        }

        public final void setOrigin(final String origin) {
            this.originProperty().set(origin);
        }

        public final StringProperty destinationProperty() {
            return this.destination;
        }

        public final String getDestination() {
            return this.destinationProperty().get();
        }

        public final void setDestination(final String destination) {
            this.destinationProperty().set(destination);
        }

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

可以通过修改数据列表来实现。

最新更新