JavaFX ChoiceDialog Tutorial with Examples
1. JavaFX ChoiceDialog
ChoiceDialog is a subclass of Dialog class. It shows a list of choices to the user, from which they can pick one item at most.
This is image of a standard ChoiceDialog:
If you don't set a Header Text, Title, Content Text, then the following image is a default ChoiceDialog.
** Code **
Book java = new Book(1L, "J01", "Java Tutorial for Beginners");
Book csharp = new Book(2L, "C01", "CSharp Tutoral for Beginners");
Book python = new Book(3L, "P01", "Python for Beginners");
Book defaultBook = csharp;
ChoiceDialog<Book> dialog = new ChoiceDialog<Book>(defaultBook, java, csharp, python);
dialog.setTitle("o7planning");
dialog.setHeaderText("Select a Book:");
dialog.setContentText("Book:");
Optional<Book> result = dialog.showAndWait();
result.ifPresent(book -> {
this.label.setText(book.getName());
});
Use the following code snippet, if you want to hide the Header region:
dialog.setHeaderText(null);
2. ChoiceDialog Example
ChoiceDialogExample.java
package org.o7planning.javafx.choicedialog;
import java.util.Optional;
import org.o7planning.javafx.model.Book;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChoiceDialogExample extends Application {
private Label label;
private void showChoiceDialog() {
Book java = new Book(1L, "J01", "Java Tutorial for Beginners");
Book csharp = new Book(2L, "C01", "CSharp Tutoral for Beginners");
Book python = new Book(3L, "P01", "Python for Beginners");
Book defaultBook = csharp;
ChoiceDialog<Book> dialog = new ChoiceDialog<Book>(defaultBook, java, csharp, python);
dialog.setTitle("o7planning");
dialog.setHeaderText("Select a Book:");
dialog.setContentText("Book:");
Optional<Book> result = dialog.showAndWait();
result.ifPresent(book -> {
this.label.setText(book.getName());
});
}
@Override
public void start(Stage stage) {
VBox root = new VBox();
root.setPadding(new Insets(10));
root.setSpacing(10);
this.label = new Label();
Button button = new Button("Select a book");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showChoiceDialog();
}
});
root.getChildren().addAll(button, label);
Scene scene = new Scene(root, 450, 250);
stage.setTitle("JavaFX ChoiceDialog (o7planning.org)");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
Book.java
package org.o7planning.javafx.model;
public class Book {
private Long id;
private String code;
private String name;
public Book(Long id, String code, String name) {
this.id = id;
this.code = code;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.name;
}
}
JavaFX Tutorials
- Open a new Window in JavaFX
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- Install JavaFX Scene Builder for Eclipse
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX TitledPane Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX TableView Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX TreeTableView Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
Show More