- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在开发一个 JAVAFX
应用程序。在我的应用程序中,单击一个按钮后,它会打开一个窗口,其中有一个 TableView
以及一个 Apply
和 Save
按钮。单击 Apply
按钮时,它将保留 TableView
的当前状态(以防我们添加/删除表行并单击应用并重新打开之前更新的表TableView
应显示)。 Save
按钮用于将表格记录保存到数据库中。假设表中有两行(来自数据库),如果我添加第 3 行并单击 Apply
,我的 TableView
窗口将关闭。如果我重新打开表格,第三行将不存在。
如何保留之前添加的第三行,而不将其插入数据库?
最佳答案
您可以尝试将 TableView
的项目(或者可选地只存储 TableView
的添加项目的列表)作为打开窗口。
我创建了一个示例:
应用程序 TableViewSample
可用于打开第二个窗口。此应用程序存储 TablePopUp
的实例,该类可以显示第二个模态 Stage
,同时维护一个“缓冲区” - Person
列表(数据模型显示在 TableView
) 对象上,添加到 TableView
并“应用”但尚未存储在数据库中。
public class TableViewSample extends Application {
// Stores the state of the TableView and opens the second window
TablePopUp popUp = new TablePopUp();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Scene scene = new Scene(new BorderPane());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);
BorderPane root = (BorderPane) scene.getRoot();
Button button = new Button("Open window");
button.setOnAction((e) -> popUp.showTable());
root.setCenter(button);
stage.setScene(scene);
stage.show();
}
class TablePopUp {
// Stores the Person object which were added and applied but not stored
// in DB
ObservableList<Person> bufferAdd = FXCollections.observableArrayList();
// Simulate the items coming from the DV
private ObservableList<Person> dataFromDB = FXCollections.observableArrayList(
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"));
void showTable() {
// Temporary buffer for the added Persion objects
ObservableList<Person> tempBuffer = FXCollections.observableArrayList();
// Temporary buffer to store persons to be deleted on apply
ObservableList<Person> bufferRemoveFromBuffer = FXCollections.observableArrayList();
// Data what the TableView displays
ObservableList<Person> tableData = FXCollections.observableArrayList();
// Stores the person objects that will be removed from the DB if Save is pressed
ObservableList<Person> bufferRemoveFromDB = FXCollections.observableArrayList();
// The Table displays elements from the DB + the applied buffer
tableData.addAll(dataFromDB);
tableData.addAll(bufferAdd);
// Create the table
TableView<Person> table = new TableView<Person>();
table.setItems(tableData);
Scene scene = new Scene(new BorderPane());
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setPromptText("Last Name");
TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setPromptText("Email");
// Button to add a new Person
Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Person newPerson = new Person(addFirstName.getText(), addLastName.getText(), addEmail.getText());
// Add a new element to the temporary buffer and add it to
// the table data also
tempBuffer.add(newPerson);
tableData.add(newPerson);
addFirstName.clear();
addLastName.clear();
addEmail.clear();
}
});
// Button to remove a Person
Button removeButton = new Button("Remove");
removeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
Person selectedItem = table.getSelectionModel().getSelectedItem();
if(selectedItem != null) {
// Remove the item from the list of the displayed persons
tableData.remove(selectedItem);
// Check the buffers: if one of the buffer contains the selected item, remove it from the buffer
if(tempBuffer.contains(selectedItem))
tempBuffer.remove(selectedItem);
else if(bufferAdd.contains(selectedItem))
bufferRemoveFromBuffer.add(selectedItem);
else {
// The item is not in the buffers -> remove the item from the DB
bufferRemoveFromDB.add(selectedItem);
}
}
}
});
HBox hb = new HBox();
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, removeButton);
hb.setSpacing(3);
VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
BorderPane root = (BorderPane) scene.getRoot();
root.setCenter(vbox);
Stage stage = new Stage();
HBox applySave = new HBox();
// On Save:
// Remove all elements from the buffer that were selected to be deleted
// Remove all elements from the BD that were selected to be deleted
// Add all the elements from the persistent buffer to the DB
// Add all the elements from the temporary buffer to the DB
// Clear both buffers
Button saveButton = new Button("Save to DB");
saveButton.setOnAction((e) -> {
bufferAdd.removeAll(bufferRemoveFromBuffer);
dataFromDB.removeAll(bufferRemoveFromDB);
dataFromDB.addAll(bufferAdd);
dataFromDB.addAll(tempBuffer);
bufferAdd.clear();
stage.close();
});
// On Apply:
// Add elements from the temporary buffer to the persistent buffer
// Remove elements from the buffer
Button applyButton = new Button("Apply");
applyButton.setOnAction((e) -> {
bufferAdd.addAll(tempBuffer);
bufferAdd.removeAll(bufferRemoveFromBuffer);
stage.close();
});
applySave.getChildren().addAll(saveButton, applyButton);
root.setBottom(applySave);
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.show();
}
}
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 String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
}
TablePopUp
类实际上有两个缓冲区:一个是临时缓冲区,用于存储添加的元素,另一个是持久缓冲区,它保存在不同的窗口开口之间。如果按下“应用”按钮,临时缓冲区将存储在持久缓冲区中。如果按下“保存”按钮,两个缓冲区都将存储在数据库中,然后它们将被清除。
在示例中,删除操作也被缓冲。删除时会发现,选择要删除的 Person
对象是否来自数据库。如果它来自数据库,它会被放入一个缓冲区,并且只有在按下保存按钮时才会从数据库中删除。相同的工作流程对已添加但尚未放入数据库的人员有效:删除时,如果按下应用按钮,他们只会被删除。
关于JavaFX 如何保存 TableView 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38345317/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!