javafx.scene.control.TextInputDialog Maven / Gradle / Ivy
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.scene.control;
import com.sun.javafx.scene.control.skin.resources.ControlResources;
import javafx.application.Platform;
import javafx.beans.NamedArg;
import javafx.geometry.Pos;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
/**
* A dialog that shows a text input control to the user.
*
* @see Dialog
* @since JavaFX 8u40
*/
public class TextInputDialog extends Dialog {
/* ************************************************************************
*
* Fields
*
**************************************************************************/
private final GridPane grid;
private final Label label;
private final TextField textField;
private final String defaultValue;
/* ************************************************************************
*
* Constructors
*
**************************************************************************/
/**
* Creates a new TextInputDialog without a default value entered into the
* dialog {@link TextField}.
*/
public TextInputDialog() {
this("");
}
/**
* Creates a new TextInputDialog with the default value entered into the
* dialog {@link TextField}.
* @param defaultValue the default value entered into the dialog
*/
public TextInputDialog(@NamedArg("defaultValue") String defaultValue) {
final DialogPane dialogPane = getDialogPane();
// -- textfield
this.textField = new TextField(defaultValue);
this.textField.setMaxWidth(Double.MAX_VALUE);
GridPane.setHgrow(textField, Priority.ALWAYS);
GridPane.setFillWidth(textField, true);
// -- label
label = DialogPane.createContentLabel(dialogPane.getContentText());
label.setPrefWidth(Region.USE_COMPUTED_SIZE);
label.textProperty().bind(dialogPane.contentTextProperty());
this.defaultValue = defaultValue;
this.grid = new GridPane();
this.grid.setHgap(10);
this.grid.setMaxWidth(Double.MAX_VALUE);
this.grid.setAlignment(Pos.CENTER_LEFT);
dialogPane.contentTextProperty().addListener(o -> updateGrid());
setTitle(ControlResources.getString("Dialog.confirm.title"));
dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
dialogPane.getStyleClass().add("text-input-dialog");
dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
updateGrid();
setResultConverter((dialogButton) -> {
ButtonData data = dialogButton == null ? null : dialogButton.getButtonData();
return data == ButtonData.OK_DONE ? textField.getText() : null;
});
}
/* ************************************************************************
*
* Public API
*
**************************************************************************/
/**
* Returns the {@link TextField} used within this dialog.
* @return the {@link TextField} used within this dialog
*/
public final TextField getEditor() {
return textField;
}
/**
* Returns the default value that was specified in the constructor.
* @return the default value that was specified in the constructor
*/
public final String getDefaultValue() {
return defaultValue;
}
/* ************************************************************************
*
* Private Implementation
*
**************************************************************************/
private void updateGrid() {
grid.getChildren().clear();
grid.add(label, 0, 0);
grid.add(textField, 1, 0);
getDialogPane().setContent(grid);
Platform.runLater(() -> textField.requestFocus());
}
}