下面列出了javafx.scene.control.Dialog#setContentText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void closeDialog(Stage primaryStage) {
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("关闭");
dialog.setContentText("确认关闭吗?");
dialog.initOwner(primaryStage);
dialog.getDialogPane().getButtonTypes().add(ButtonType.APPLY);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.getDialogPane().setPrefSize(350, 100);
Optional<ButtonType> s = dialog.showAndWait();
s.ifPresent(s1 -> {
if (s1.equals(ButtonType.APPLY)) {
primaryStage.close();
} else if (s1.equals(ButtonType.CLOSE)) {
dialog.close();
}
});
}
private void onOK(final ActionEvent ignored)
{
Toggle selectedToggle = toggleGroup.getSelectedToggle();
Logging.debugPrint("selected toggle is " + selectedToggle);
if (selectedToggle != null)
{
Integer whichItemId = (Integer)selectedToggle.getUserData();
InfoFacade selectedItem = chooser.getAvailableList().getElementAt(whichItemId);
chooser.addSelected(selectedItem);
}
if (chooser.isRequireCompleteSelection() && (chooser.getRemainingSelections().get() > 0))
{
Dialog<ButtonType> alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(chooser.getName());
alert.setContentText(LanguageBundle.getFormattedString("in_chooserRequireComplete",
chooser.getRemainingSelections().get()));
alert.showAndWait();
return;
}
chooser.commit();
committed = true;
this.dispose();
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
private void onOK(final ActionEvent ignored)
{
Toggle selectedToggle = toggleGroup.getSelectedToggle();
Logging.debugPrint("selected toggle is " + selectedToggle);
if (selectedToggle != null)
{
Integer whichItemId = (Integer)selectedToggle.getUserData();
InfoFacade selectedItem = chooser.getAvailableList().getElementAt(whichItemId);
chooser.addSelected(selectedItem);
}
if (chooser.isRequireCompleteSelection() && (chooser.getRemainingSelections().get() > 0))
{
Dialog<ButtonType> alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(chooser.getName());
alert.setContentText(LanguageBundle.getFormattedString("in_chooserRequireComplete",
chooser.getRemainingSelections().get()));
alert.showAndWait();
return;
}
chooser.commit();
committed = true;
this.dispose();
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
/**
* Initialized a dialog with the given owner window, and String header and content
* @param dialog dialog box to initialize
* @param window owning window or null if there is none
* @param header dialog header
* @param content dialog content
*/
public static void initializeDialog(Dialog dialog, Window window, String header, String content){
if (window != null)
dialog.initOwner(window);
dialog.setTitle("CPU Sim");
dialog.setHeaderText(header);
dialog.setContentText(content);
// Allow dialogs to resize for Linux
// https://bugs.openjdk.java.net/browse/JDK-8087981
dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
}
public Optional<String> showDialog(final String contentText) throws ProjectDirectoryNotSpecified
{
if (this.defaultToTempDirectory) { return Optional.of(tmpDir()); }
final StringProperty projectDirectory = new SimpleStringProperty(null);
final ButtonType specifyProject = new ButtonType("Specify Project", ButtonData.OTHER);
final ButtonType noProject = new ButtonType("No Project", ButtonData.OK_DONE);
final Dialog<String> dialog = new Dialog<>();
dialog.setResultConverter(bt -> {
return ButtonType.CANCEL.equals(bt)
? null
: noProject.equals(bt)
? tmpDir()
: projectDirectory.get();
});
dialog.getDialogPane().getButtonTypes().setAll(specifyProject, noProject, ButtonType.CANCEL);
dialog.setTitle("Paintera");
dialog.setHeaderText("Specify Project Directory");
dialog.setContentText(contentText);
final Node lookupProjectButton = dialog.getDialogPane().lookupButton(specifyProject);
if (lookupProjectButton instanceof Button)
{
((Button) lookupProjectButton).setTooltip(new Tooltip("Look up project directory."));
}
Optional
.ofNullable(dialog.getDialogPane().lookupButton(noProject))
.filter(b -> b instanceof Button)
.map(b -> (Button) b)
.ifPresent(b -> b.setTooltip(new Tooltip("Create temporary project in /tmp.")));
Optional
.ofNullable(dialog.getDialogPane().lookupButton(ButtonType.CANCEL))
.filter(b -> b instanceof Button)
.map(b -> (Button) b)
.ifPresent(b -> b.setTooltip(new Tooltip("Do not start Paintera.")));
lookupProjectButton.addEventFilter(ActionEvent.ACTION, event -> {
final DirectoryChooser chooser = new DirectoryChooser();
final Optional<String> d = Optional.ofNullable(chooser.showDialog(dialog.getDialogPane().getScene()
.getWindow())).map(
File::getAbsolutePath);
if (d.isPresent())
{
projectDirectory.set(d.get());
}
else
{
// consume on cancel, so that parent dialog does not get closed.
event.consume();
}
});
dialog.setResizable(true);
final Optional<String> returnVal = dialog.showAndWait();
if (!returnVal.isPresent()) { throw new ProjectDirectoryNotSpecified(); }
return returnVal;
}
public void connectDialog() {
// Create the custom dialog.
Dialog<String> dialog = new Dialog<>();
// Get the Stage.
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
/*
stage.setOnCloseRequest(e -> {
boolean isExit = mainMenu.exitDialog(stage);//.getScreensSwitcher().exitDialog(stage);
if (!isExit) {
e.consume();
}
else {
Platform.exit();
}
});
*/
// Add corner icon.
stage.getIcons().add(new Image(this.getClass().getResource("/icons/network/client48.png").toString()));
// Add Stage icon
dialog.setGraphic(new ImageView(this.getClass().getResource("/icons/network/network256.png").toString()));
// dialog.initOwner(mainMenu.getStage());
dialog.setTitle("Mars Simulation Project");
dialog.setHeaderText("Multiplayer Client");
dialog.setContentText("Enter the host address : ");
// Set the button types.
ButtonType connectButtonType = new ButtonType("Connect", ButtonData.OK_DONE);
// ButtonType localHostButton = new ButtonType("localhost");
// dialog.getDialogPane().getButtonTypes().addAll(localHostButton,
// loginButtonType, ButtonType.CANCEL);
dialog.getDialogPane().getButtonTypes().addAll(connectButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
// TextField tfUser = new TextField();
// tfUser.setPromptText("Username");
TextField tfServerAddress = new TextField();
// PasswordField password = new PasswordField();
tfServerAddress.setPromptText("192.168.xxx.xxx");
tfServerAddress.setText("192.168.xxx.xxx");
Button localhostB = new Button("Use localhost");
// grid.add(new Label("Username:"), 0, 0);
// grid.add(tfUser, 1, 0);
grid.add(new Label("Server Address:"), 0, 1);
grid.add(tfServerAddress, 1, 1);
grid.add(localhostB, 2, 1);
// Enable/Disable connect button depending on whether the host address
// was entered.
Node connectButton = dialog.getDialogPane().lookupButton(connectButtonType);
connectButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
tfServerAddress.textProperty().addListener((observable, oldValue, newValue) -> {
connectButton.setDisable(newValue.trim().isEmpty());
} );
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> tfServerAddress.requestFocus());
// Convert the result to a username-password-pair when the login button
// is clicked.
dialog.setResultConverter(dialogButton -> {
if (dialogButton == connectButtonType) {
return tfServerAddress.getText();
}
return null;
} );
localhostB.setOnAction(event -> {
tfServerAddress.setText("127.0.0.1");
} );
// localhostB.setPadding(new Insets(1));
// localhostB.setPrefWidth(10);
Optional<String> result = dialog.showAndWait();
result.ifPresent(input -> {
String serverAddressStr = tfServerAddress.getText();
this.serverAddressStr = serverAddressStr;
logger.info("Connecting to server at " + serverAddressStr);
loginDialog();
} );
}
@Override
public String gitOpenDialog(String title, String header, String content) {
String repo;
DirectoryChooser chooser = new DirectoryChooser();
Dialog<Pair<String, GitFxDialogResponse>> dialog = new Dialog<>();
//Dialog<Pair<String, GitFxDialogResponse>> dialog = getCostumeDialog();
dialog.setTitle(title);
dialog.setHeaderText(header);
dialog.setContentText(content);
ButtonType okButton = new ButtonType("Ok");
ButtonType cancelButton = new ButtonType("Cancel");
dialog.getDialogPane().getButtonTypes().addAll(okButton, cancelButton);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField repository = new TextField();
repository.setPrefWidth(250.0);
repository.setPromptText("Repo");
grid.add(new Label("Repository:"), 0, 0);
grid.add(repository, 1, 0);
/////////////////////Modification of choose Button////////////////////////
Button chooseButtonType = new Button("Choose");
grid.add(chooseButtonType, 2, 0);
// Button btnCancel1 = new Button("Cancel");
// btnCancel1.setPrefWidth(90.0);
// Button btnOk1 = new Button("Ok");
// btnOk1.setPrefWidth(90.0);
// HBox hbox = new HBox(4);
// hbox.getChildren().addAll(btnOk1,btnCancel1);
// hbox.setPadding(new Insets(2));
// grid.add(hbox, 1, 1);
chooseButtonType.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
// label.setText("Accepted");
File path = chooser.showDialog(dialog.getOwner());
getFileAndSeText(repository,path);
chooser.setTitle(resourceBundle.getString("repo"));
}
});
// btnCancel1.setOnAction(new EventHandler<ActionEvent>(){
// @Override public void handle(ActionEvent e) {
// System.out.println("Shyam : Testing");
// setResponse(GitFxDialogResponse.CANCEL);
// }
// });
//////////////////////////////////////////////////////////////////////
dialog.getDialogPane().setContent(grid);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == okButton) {
String filePath = repository.getText();
//filePath = filePath.concat("/.git");
//[LOG] logger.debug(filePath);
if (!filePath.isEmpty() && new File(filePath).exists()) {
setResponse(GitFxDialogResponse.OK);
return new Pair<>(repository.getText(), GitFxDialogResponse.OK);
} else {
this.gitErrorDialog(resourceBundle.getString("errorOpen"),
resourceBundle.getString("errorOpenTitle"),
resourceBundle.getString("errorOpenDesc"));
}
}
if (dialogButton == cancelButton) {
//[LOG] logger.debug("Cancel clicked");
setResponse(GitFxDialogResponse.CANCEL);
return new Pair<>(null, GitFxDialogResponse.CANCEL);
}
return null;
});
Optional<Pair<String, GitFxDialogResponse>> result = dialog.showAndWait();
result.ifPresent(repoPath -> {
//[LOG] logger.debug(repoPath.getKey() + " " + repoPath.getValue().toString());
});
Pair<String, GitFxDialogResponse> temp = null;
if (result.isPresent()) {
temp = result.get();
}
if(temp != null){
return temp.getKey();// + "/.git";
}
return EMPTY_STRING;
}
@Override
public Pair<String, String> gitInitDialog(String title, String header, String content) {
Pair<String, String> repo;
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle(title);
dialog.setHeaderText(header);
dialog.setContentText(content);
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField localPath = new TextField();
localPath.setPromptText("Local Path");
localPath.setPrefWidth(250.0);
grid.add(new Label("Local Path:"), 0, 0);
grid.add(localPath, 1,0);
ButtonType initRepo = new ButtonType("Initialize Repository");
ButtonType cancelButtonType = new ButtonType("Cancel");
dialog.getDialogPane().setContent(grid);
dialog.getDialogPane().getButtonTypes().addAll( initRepo, cancelButtonType);
/////////////////////Modification of choose Button////////////////////////
Button chooseButtonType = new Button("Choose");
grid.add(chooseButtonType, 2, 0);
chooseButtonType.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle(resourceBundle.getString("selectRepo"));
File initialRepo = chooser.showDialog(dialog.getOwner());
getFileAndSeText(localPath, initialRepo);
dialog.getDialogPane();
}
});
//////////////////////////////////////////////////////////////////////
dialog.setResultConverter(dialogButton -> {
if (dialogButton == initRepo) {
setResponse(GitFxDialogResponse.INITIALIZE);
String path = localPath.getText();
//[LOG] logger.debug( "path" + path + path.isEmpty());
if (new File(path).exists()) {
//[LOG] logger.debug(path.substring(localPath.getText().lastIndexOf(File.separator)));
String projectName= path.substring(localPath.getText().lastIndexOf(File.separator)+1);
return new Pair<>(projectName, localPath.getText());
} else {
this.gitErrorDialog(resourceBundle.getString("errorInit"),
resourceBundle.getString("errorInitTitle"),
resourceBundle.getString("errorInitDesc"));
}
}
if (dialogButton == cancelButtonType) {
setResponse(GitFxDialogResponse.CANCEL);
return new Pair<>(null, null);
}
return null;
});
Optional<Pair<String, String>> result = dialog.showAndWait();
Pair<String, String> temp = null;
if (result.isPresent()) {
temp = result.get();
}
return temp;
}