JavaFx Chat TextField and Button Key Listener - javafx

Im a beginner working on a simple Chat with javafx, i have already searched for similar problems and didnt find a fitting solution. The first thing i need to do is the Graphics. My first Problem is, that my MousEvent Listener for the Button isnt working, i simply cant click the Button somehow. My second problem is, once the application is running and i click somewhere outside of the TextField, i cant return to it and enter new Text. Like the Listeners of the TextField, which listen for KeyStroke events dont run anymore. Code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ChatView extends Application {
String s;
#Override
public void start(Stage stage){
s = "";
StackPane rootPane = new StackPane();
TextField enterMessageField = new TextField();
enterMessageField.setEditable(true);
TextArea displayAllMessages = new TextArea();
displayAllMessages.setPrefHeight(500);
displayAllMessages.setEditable(false);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(displayAllMessages);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
displayAllMessages.setPrefWidth(650);
Button button = new Button("Send Message");
VBox vBoxChat = new VBox();
vBoxChat.setPadding(new Insets(650, 200, 20, 20));
vBoxChat.getChildren().addAll( enterMessageField);
VBox vBoxChatIncoming = new VBox();
vBoxChatIncoming.setPadding(new Insets(20, 20, 20, 20));
vBoxChatIncoming.getChildren().addAll( scrollPane);
VBox vBoxEnter = new VBox();
vBoxEnter.setPadding(new Insets(650, 20, 20, 550));
vBoxEnter.getChildren().add(button);
rootPane.getChildren().addAll(vBoxChat, vBoxEnter, vBoxChatIncoming);
Scene scene = new Scene(rootPane, 700, 700, Color.WHITE);
stage.setScene(scene);
stage.setTitle("Chat");
stage.show();
enterMessageField.setOnKeyPressed(new EventHandler <KeyEvent> () {
#Override
public void handle(KeyEvent event){
if (event.getCode() == KeyCode.ENTER){
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
displayAllMessages.appendText(s);
}
}
});
button.setOnAction((event) -> {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
System.out.println(s);
});
}
public static void main(String[] args){
launch(args);
}
}
Thank you for your help!

I changed the rootPane to GridPane and this fact fixed the problems.
Actually the StackPane is not suitable type for multicomponent interface. See the similar problem here: Mouse Events get Ignored on the Underlying Layer
See a bit improved code below:
public class ChatView extends Application {
String s = "";
#Override
public void start(Stage stage) {
TextField enterMessageField = new TextField();
enterMessageField.setEditable(true);
TextArea displayAllMessages = new TextArea();
displayAllMessages.setPrefHeight(500);
displayAllMessages.setEditable(false);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(displayAllMessages);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
displayAllMessages.setPrefWidth(650);
Button button = new Button("Send Message");
button.setDefaultButton(true);
VBox vBoxChat = new VBox(enterMessageField);
vBoxChat.setPadding(new Insets(10, 10, 10, 10));
VBox vBoxChatIncoming = new VBox(displayAllMessages);
vBoxChatIncoming.setPadding(new Insets(10, 10, 10, 10));
VBox vBoxEnter = new VBox(button);
vBoxEnter.setPadding(new Insets(10, 10, 10, 10));
GridPane rootPane = new GridPane();
rootPane.add(vBoxChatIncoming, 0, 0);
rootPane.add(vBoxChat, 0, 1);
rootPane.add(vBoxEnter, 1, 1);
Scene scene = new Scene(rootPane, 800, 700, Color.WHITE);
stage.setScene(scene);
stage.setTitle("Chat");
stage.show();
enterMessageField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
displayAllMessages.appendText(s);
}
});
button.setOnAction((event) -> {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
System.out.println(s);
});
}
public static void main(String[] args) {
launch(args);
}
}

Related

Exception in thread "JavaFX Application Thread" java.lang.illegalArgumentException: Grid hgap=10.0

my name is Omar, I made this code in javafx in order to switch between scene 0 and scene 1.
When an input scanner laser barcode number is introduced (6590055780)
it changes from scene 0 to scene 1, once in scene 1, if user pressed a button will change to scene 0.
But when i compile this code, it gives me the following error
"Exception in thread "JavaFX Application Thread" java.lang.illegalArgumentException: Grid hgap=10.0 alignment=CENTERis already set as root of another scene"
I do not know why i get this error, it is like the setOnKeyPressed does not allow me to leave from it, when i want to switch from scene 0 to scene 1, i got this error message, anyhelp is appreciated in order to solve this problem. thank you very much
and this is the code
import javafx.application.Application;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
public class scannerinputfx2 extends Application {
////////////* public class*///////
public static String salida;
#Override
public void start(Stage primaryStage){
primaryStage.setTitle("what's up");
//scene 0
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 300, 275);
TextArea area = new TextArea();
TextField userTextField = new TextField();
//grid.add(userTextField, 0, 0);
//grid.add(area, 0,1);
GridPane.setConstraints(userTextField, 0,0);
GridPane.setConstraints(area,0,1);
grid.getChildren().addAll(area, userTextField);
//scene 1
GridPane grid1 = new GridPane();
grid1.setAlignment(Pos.CENTER);
grid1.setHgap(10);
grid1.setVgap(10);
grid1.setPadding(new Insets(25, 25, 25, 25));
Scene scene1 = new Scene(grid1, 300, 275);
Button btnfinal = new Button("Press button to get back");
GridPane.setConstraints(btnfinal, 0, 0);
grid1.getChildren().addAll(btnfinal);
btnfinal.setOnAction(actionEvent -> {
scene1.setRoot(grid);
});
primaryStage.setScene(scene);
primaryStage.show();
userTextField.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
area.setText( userTextField.getText());
if (userTextField.getText().equals("6590055780"))
{
System.out.println("The number is " + userTextField.getText());
userTextField.setText("");
scene.setRoot(grid1);
}
else
{
System.out.println("The number is not the indicated");
}
}
});
}
////////*Here finish primary stage/////////
public static void main(String[] args) {
launch(args);
}
}

How to make scene local JavaFX

The scene at the start of lambda has an error that is not local. How to make it local. I tried adding up top but shows null. How to make scene local at the lambda because scene sets primaryStage. I need the other nodes for GridPane. I am trying to draw on the canvas. Please help fix this problem. Thank you in advanced!
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TestFX extends Application {
Node scene; // i tried adding this but throws null at the lambda
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("MyPaint");
RadioButton radioButton1 = new RadioButton("Draw");
RadioButton radioButton2 = new RadioButton("Erase");
RadioButton radioButton3 = new RadioButton("Circle");
ToggleGroup radioGroup = new ToggleGroup();
radioButton1.setToggleGroup(radioGroup);
radioButton2.setToggleGroup(radioGroup);
radioButton3.setToggleGroup(radioGroup);
Button b1 = new Button("Clear Canvas");
TextField colorText = new TextField();
colorText.setText("Colors");
Canvas canvas = new Canvas(300, 250);
GraphicsContext gc = canvas.getGraphicsContext2D();
// scene cannot be resolved without initializing at top: Node scene;
scene.setOnMousePressed(q -> {
gc.setFill(Color.BLACK);
gc.setLineWidth(1);
gc.beginPath();
gc.lineTo(q.getSceneX(),
q.getSceneY());
gc.getStroke();
});
gc.strokeText("Hello Canvas", 150, 100);
GridPane gridPane = new GridPane();
GridPane.setConstraints(radioButton1, 0, 0);
GridPane.setConstraints(radioButton2, 0, 1);
GridPane.setConstraints(radioButton3, 0, 2);
GridPane.setConstraints(colorText, 0, 3);
GridPane.setConstraints(b1, 0, 4);
GridPane.setConstraints(canvas, 1, 1);
// adding spaces between radiobutton, button and radiobutton
radioButton1.setPadding(new Insets(15));
radioButton2.setPadding(new Insets(15));
radioButton3.setPadding(new Insets(15));
b1.setPadding(new Insets(15));
colorText.setPadding(new Insets(15));
gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
Scene scene = new Scene(gridPane, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You define Scene in this part of the code:
gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
Scene scene = new Scene(gridPane, 800, 800);
primaryStage.setScene(scene);
primaryStage.show()
but you are calling this code before you define Scene.
// scene cannot be resolved without initializing at top: Node scene;
scene.setOnMousePressed(q -> {
gc.setFill(Color.BLACK);
gc.setLineWidth(1);
gc.beginPath();
gc.lineTo(q.getSceneX(),
q.getSceneY());
gc.getStroke();
});
You need to delete Node scene. and move the scene.setOnMousePressed() code to a place after where you defined the Scene.
Full Code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TestFX extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("MyPaint");
RadioButton radioButton1 = new RadioButton("Draw");
RadioButton radioButton2 = new RadioButton("Erase");
RadioButton radioButton3 = new RadioButton("Circle");
ToggleGroup radioGroup = new ToggleGroup();
radioButton1.setToggleGroup(radioGroup);
radioButton2.setToggleGroup(radioGroup);
radioButton3.setToggleGroup(radioGroup);
Button b1 = new Button("Clear Canvas");
TextField colorText = new TextField();
colorText.setText("Colors");
Canvas canvas = new Canvas(300, 250);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.strokeText("Hello Canvas", 150, 100);
GridPane gridPane = new GridPane();
GridPane.setConstraints(radioButton1, 0, 0);
GridPane.setConstraints(radioButton2, 0, 1);
GridPane.setConstraints(radioButton3, 0, 2);
GridPane.setConstraints(colorText, 0, 3);
GridPane.setConstraints(b1, 0, 4);
GridPane.setConstraints(canvas, 1, 1);
// adding spaces between radiobutton, button and radiobutton
radioButton1.setPadding(new Insets(15));
radioButton2.setPadding(new Insets(15));
radioButton3.setPadding(new Insets(15));
b1.setPadding(new Insets(15));
colorText.setPadding(new Insets(15));
gridPane.getChildren().addAll(radioButton1, radioButton2, radioButton3, colorText, b1, canvas);
Scene scene = new Scene(gridPane, 800, 800);
scene.setOnMousePressed(q -> {
gc.setFill(Color.BLACK);
gc.setLineWidth(1);
gc.beginPath();
gc.lineTo(q.getSceneX(),
q.getSceneY());
gc.getStroke();
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Automatically resizing the window based on dynamic content

I am checking for a feature to automatically resize the window based on the content. I am already aware of Window.sizeToScene() method. But this is so cumbersome that I need to take care in every place to call the sizeToScene(). For example, when I add/remove nodes, when I expanded the titlePanes, etc...
Can someone let me know if it is possible to automatically adjust the window based on the content. I am looking this feature for both normal and transparent windows.
Any hints/suggestion is highly appreciated.
Please find below a quick demo of what I am looking for. Everything works as expected if I consider calling sizeToScene(). I am targeting for a way to get the same behavior without calling sizeToScene for every scenario.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class TransparentWindowResizeDemo extends Application {
#Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setSpacing(15);
root.setAlignment(Pos.CENTER);
Scene sc = new Scene(root, 300, 300);
stage.setScene(sc);
stage.show();
Button showNormal = new Button("Show Normal Window");
showNormal.setOnAction(e -> showWindow(false));
Button showTransparent = new Button("Show Transparent Window");
showTransparent.setOnAction(e -> showWindow(true));
root.getChildren().addAll(showNormal, showTransparent);
}
private void showWindow(boolean isTransparent) {
Stage window = new Stage();
VBox root = new VBox();
root.setStyle("-fx-background-color: grey; -fx-border-width:2px;-fx-border-color:black;");
Scene scene = new Scene(root, Color.TRANSPARENT);
window.setScene(scene);
VBox layout = new VBox();
layout.setSpacing(5);
layout.setPadding(new Insets(5));
CheckBox sizeToScene = new CheckBox("sizeToScene");
sizeToScene.setSelected(true);
StackPane box = new StackPane();
box.setStyle("-fx-background-color:yellow; -fx-border-width:1px;-fx-border-color:black;");
box.setMinSize(200, 100);
box.setMaxSize(200, 100);
Button add = new Button("Add Pane");
Button remove = new Button("Remove Pane");
remove.setDisable(true);
add.setOnAction(e -> {
layout.getChildren().add(box);
add.setDisable(true);
remove.setDisable(false);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
remove.setOnAction(e -> {
layout.getChildren().remove(box);
add.setDisable(false);
remove.setDisable(true);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
HBox btnLayout = new HBox();
btnLayout.setSpacing(5);
btnLayout.getChildren().addAll(add, remove);
StackPane tpContent = new StackPane();
tpContent.setStyle("-fx-background-color:pink; -fx-border-width:1px;-fx-border-color:black;");
tpContent.setMinSize(200, 100);
tpContent.setMaxSize(200, 100);
TitledPane tp = new TitledPane("Titled Pane", tpContent);
tp.setAnimated(false);
tp.expandedProperty().addListener((obs, oldVal, newVal) -> {
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
if (isTransparent) {
window.initStyle(StageStyle.TRANSPARENT);
StackPane close = new StackPane();
close.setMaxWidth(30);
close.setStyle("-fx-background-color:red;");
close.getChildren().add(new Label("X"));
close.setOnMouseClicked(e -> window.hide());
DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
StackPane header = new StackPane();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
window.setX(e.getScreenX() - x.get());
window.setY(e.getScreenY() - y.get());
});
header.setStyle("-fx-border-width:0px 0px 2px 0px;-fx-border-color:black;");
header.setMinHeight(30);
header.setAlignment(Pos.CENTER_RIGHT);
Label lbl = new Label("I am draggable !!");
lbl.setStyle("-fx-text-fill:white;-fx-font-weight:bold;");
StackPane.setAlignment(lbl,Pos.CENTER_LEFT);
header.getChildren().addAll(lbl,close);
root.getChildren().add(header);
}
layout.getChildren().addAll(sizeToScene, btnLayout, tp);
root.getChildren().add(layout);
window.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Edit :: This is different from what I already found in other questions or with the link in the comment. The solution that is specified is what I am aware of and I already mentioned that in the question. I have a heavy screen where it contain many nodes in differnt hirerchy. I am checking for any generic solution to include in one place rather that calling from every node add/remove scenario.
I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:
VBox root = new VBox() {
private boolean needsResize = false;
#Override
protected void layoutChildren()
{
super.layoutChildren();
if (!needsResize) {
needsResize = true;
Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};
Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.

Fade Effect with JavaFX

Im trying to realize a special FadeTransition effect. But I have no idea how I can manage it. For some node I would like to increase the opacity from left to right (for example in Powerpoint, you can change the slides with such an effect). Here is an easy example for rectangles. But the second one should fadeIn from left to right (the opacity should increase on the left side earlier as on the right side). With timeline and KeyValues/KeyFrames I found also no solution.
Thanks in advance.
Rectangle rec2;
#Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.BLACK);
stage.setTitle("JavaFX Scene Graph Demo");
Pane pane = new Pane();
Button btnForward = new Button();
btnForward.setText(">");
btnForward.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FadeTransition ft = new FadeTransition(Duration.millis(2000), rec2);
ft.setFromValue(0.);
ft.setToValue(1.);
ft.play();
}
});
Rectangle rec1 = new Rectangle(0, 0, 300,200);
rec1.setFill(Color.RED);
rec2 = new Rectangle(100, 50, 100,100);
rec2.setFill(Color.GREEN);
rec2.setOpacity(0.);
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(pane);
root.getChildren().add(btnForward);
stage.setScene(scene);
stage.show();
}
Define the fill of the rectangle using css with a linear gradient which references looked-up colors for the left and right edges of the rectangle. (This can be inline or in an external style sheet.)
Define a couple of DoublePropertys representing the opacities of the left and right edge.
Define the looked-up colors on the rectangle or one of its parents using an inline style bound to the two double properties.
Use a timeline to change the values of the opacity properties.
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FadeInRectangle extends Application {
#Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.BLACK);
primaryStage.setTitle("JavaFX Scene Graph Demo");
Pane pane = new Pane();
Rectangle rec1 = new Rectangle(0, 0, 300,200);
rec1.setFill(Color.RED);
Rectangle rec2 = new Rectangle(100, 50, 100,100);
rec2.setStyle("-fx-fill: linear-gradient(to right, left-col, right-col);");
final DoubleProperty leftEdgeOpacity = new SimpleDoubleProperty(0);
final DoubleProperty rightEdgeOpacity = new SimpleDoubleProperty(0);
root.styleProperty().bind(
Bindings.format("left-col: rgba(0,128,0,%f); right-col: rgba(0,128,0,%f);", leftEdgeOpacity, rightEdgeOpacity)
);
Button btnForward = new Button();
btnForward.setText(">");
btnForward.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(leftEdgeOpacity, 0)),
new KeyFrame(Duration.ZERO, new KeyValue(rightEdgeOpacity, 0)),
new KeyFrame(Duration.millis(500), new KeyValue(rightEdgeOpacity, 0)),
new KeyFrame(Duration.millis(1500), new KeyValue(leftEdgeOpacity, 1)),
new KeyFrame(Duration.millis(2000), new KeyValue(rightEdgeOpacity, 1)),
new KeyFrame(Duration.millis(2000), new KeyValue(leftEdgeOpacity, 1))
);
timeline.play();
}
});
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(pane);
root.getChildren().add(btnForward);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

usrpagestage.initStyle(StageStyle.UTILITY) does not displaying contents fully

Am in attempt to design a pop up window. I designed it and it works, but with a small problem.
This is a part of code of the pop up window :
public class Warning extends BorderPane {
public Warning() {
setCenter(addVBox2());
}
private VBox addVBox2() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(15,10,15,10));
vbox.setSpacing(10);
Label l1 = new Label("WARNING");
l1.setFont(Font.font("Calibri", FontWeight.BOLD, 20));
l1.setTextFill(Color.BLACK);
l1.setUnderline(true);
Label l2 = new Label("Try other User Name..!");
l2.setFont(Font.font("Calibri", FontWeight.BOLD, 18));
l2.setTextFill(Color.RED);
vbox.getChildren().addAll(l1, l2);
return vbox;
}
And this is how I call it :
setEffect(new BoxBlur(5, 10, 10));
Stage usrpagestage = new Stage();
usrpagestage.setMaxHeight(100);
usrpagestage.setMaxWidth(300);
usrpagestage.initStyle(StageStyle.UTILITY);
usrpagestage.setScene(new Scene(new Warning()));
usrpagestage.show();
usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent t) {
setEffect(new BoxBlur(0, 0, 0));
}
});
The pop up windows works when it is supposed to. But the contents in it is not fully displayed. This is the screen shot :
How to solve this issue ?
Remove the two lines:
usrpagestage.setMaxHeight(100);
usrpagestage.setMaxWidth(300);
After that, the stage will revert to its default behavior of automatically sizing to fit the initial scene content.
No, it didn't worked.
Yep, it didn't worked at all :-)
It should have worked (letting the stage autosize itself should be the correct solution).
It didn't work because of a bug in the JavaFX layout libraries:
RT-31665 Size of stage with style UTILITY is not correctly calculated.
You can workaround the bug by manually calling stage.sizeToScene().
Sample Code
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.web.WebView;
import javafx.stage.*;
public class WarningSample extends Application {
#Override public void start(Stage stage) throws Exception {
final WebView view = new WebView();
view.getEngine().load("http://www.google.com");
stage.setScene(new Scene(view));
stage.show();
Stage warningStage = new Stage();
warningStage.initStyle(StageStyle.UTILITY);
warningStage.setScene(new Scene(new Warning()));
// this workaround allows the stage to be sized correctly.
warningStage.sizeToScene();
warningStage.show();
}
public static void main(String[] args) { launch(); }
private class Warning extends VBox {
public Warning() {
setPadding(new Insets(15, 10, 15, 10));
setSpacing(10);
Label heading = new Label("WARNING");
heading.setFont(Font.font("Calibri", FontWeight.BOLD, 20));
heading.setTextFill(Color.BLACK);
heading.setUnderline(true);
Label body = new Label("Try other User Name..!");
body.setFont(Font.font("Calibri", FontWeight.BOLD, 18));
body.setTextFill(Color.RED);
getChildren().addAll(heading, body);
}
}
}

Resources