在我向某人展示工作(ish)原型之前,我从未想过。他们询问是否可以在当前的一个表格上面添加一个新行,即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
非常感谢
答案 0 :(得分:0)
基本上您需要做的就是在适当的位置将新项目插入到表格的项目列表中:
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);
}
}
答案 1 :(得分:-1)
可以通过修改数据列表来实现。