基本上所有你需要做的,在适当的位置插入一个新的项目到表的项目列表:
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);
}
}
你问在JavaFX中如何插入行到TableView? 你可以更具体吗? – amkz
表中的行只不过是作为其中的项目设置的“ObservableList”中的数据。如果更改列表,则表中的项目/行会更改。 – ItachiUchiha
您所要做的就是在适当的索引处将新项目插入表格的项目列表中。你能更具体地说明你希望用户采取什么行动来插入新行吗? –