我正在尋找javafx JOptionPane等價物,並且我找到了一個很棒的類Dialog。所以在本教程中,導師使用:Dialog<Pair<String,String>>
來獲得兩個字符串輸入字段,從這裏我想知道是否可以使用類說:Dialog<Product>
。如果可能的話,我應該如何寫這個課程是他們的任何特定模式或情節? 謝謝是否可以在javafx對話框中使用特定的類?
2
A
回答
2
是的,你可以做到這一點。我的回答是立足於:
https://examples.javacodegeeks.com/desktop-java/javafx/dialog-javafx/javafx-dialog-example/
假設你的產品有兩個字段,可以通過構造函數傳遞:
String name;
float price;
您可以用這樣的方式來創建你的對話:
Dialog<Product> dialog = new Dialog<>();
dialog.setTitle("Product Dialog");
dialog.setResizable(true);
Label nameLabel = new Label("Name: ");
Label priceLabel = new Label("Price: ");
TextField nameField = new TextField();
TextField priceField = new TextField();
GridPane grid = new GridPane();
grid.add(nameLabel, 1, 1);
grid.add(nameField, 2, 1);
grid.add(priceLabel, 1, 2);
grid.add(priceField, 2, 2);
dialog.getDialogPane().setContent(grid);
ButtonType saveButton = new ButtonType("Save", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(saveButton);
dialog.setResultConverter(new Callback<ButtonType, Product>() {
@Override
public Product call(ButtonType button) {
if (button == saveButton) {
String name = nameField.getText();
Float price;
try {
price = Float.parseFloat(priceField.getText());
} catch (NumberFormatException e) {
// Add some log or inform user about wrong price
return null;
}
return new Product(name, price);
}
return null;
}
});
Optional<Product> result = dialog.showAndWait();
if (result.isPresent()) {
Product product = result.get();
// Do something with product
}
相關問題
- 1. 在Sql Service Broker中,是否可以使用特定的conversation_handle創建對話框
- 2. 是否可以在AS3中使用特定類引用類型的變量?
- 3. 是否可以使用IntelliJ插件啓動.fxml(對話框)?
- 4. 是否可以使用JavaScript生成保存文件對話框?
- 5. 是否可以在Visual Studio擴展中創建類似於Intellisense對話框的模式對話框?
- 6. 是否可以指定要從資源文件使用的COM對話框?
- 7. 是否可以在Service [android app]中顯示對話框?
- 8. 是否可以在wix中添加卸載對話框?
- 9. 是否可以在觸摸UI對話框中包含文件?
- 10. 是否可以在alert()對話框中顯示上標字符?
- 11. 是否可以使用WINAPI或GDI +在C++中創建CommandLink對話框?
- 12. 是否可以抑制Extendscript中的CheckOut/CheckIn對話框?
- 13. 使用JavaFX確認對話框
- 14. 是否可以指示ServicePartitionClient與服務結構中的特定節點對話?
- 15. 在用於Angular4的Material中,是否可以打開嵌套對話框?
- 16. 是否可以在自定義的AuthorizeAttribute類中使用RedirectToAction()?
- 17. dijit對話框是否可以在模塊創建時關閉
- 18. 是否可以將PDF嵌入到jQuery UI對話框中?
- 19. 是否可以將CMFCToolBar添加到對話框中?
- 20. 瞭解Android Activity是否從特定的對話框彈出?
- 21. 是否可以爲特定的選擇框禁用chosen.js?
- 22. 我們可以在對話框中打開對話框嗎?
- 23. 是否可以創建會話特定的SIgnalR連接?
- 24. 是否可以在IntelliJ中的文件對話框中過濾文件?
- 25. 是否可以動態更改模態對話框的語言?
- 26. 是否可以動態更改Rich-Faces對話框的大小?
- 27. 是否可以給對話框的時間限制
- 28. 是否可以在全屏應用程序中創建GetOpenFileName對話框?
- 29. 是否可以在Matlab中「追蹤」特定的特徵值?
- 30. 是否可以使用android的語音識別而不顯示對話框?
這個想法超出了我的想法,因爲我已經使用JAVAFX和JPA之間的適配器模式 –