How to show ProgressIndicator in center of Pane in Javafx - javafx

I have a Pane with some controls and a button. When I click on the button, I want show ProgressIndicator in center of pane without removing any controls.
When I am adding a ProgressIndicator to pane during onAction of button, it adds it below the button. I want it to overlay on the pane.
The picture below explains what I want.
Code
package fx;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage arg0) throws Exception {
final VBox bx = new VBox();
bx.setAlignment(Pos.CENTER);
TextField userName = new TextField("User Name");
userName.setMaxWidth(200);
TextField email = new TextField("Email");
email.setMaxWidth(200);
Button submit = new Button("Submit");
submit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
ProgressIndicator pi = new ProgressIndicator();
//adding here but it is adding at below of button
//how to do here
bx.getChildren().add(pi);
//Further process
}
});
bx.getChildren().addAll(userName, email, submit);
Scene c = new Scene(bx);
arg0.setScene(c);
arg0.setMinWidth(500);
arg0.setMinHeight(500);
arg0.show();
}
public static void main(String[] args) {
Main h = new Main();
h.launch(args);
}
}

You need to use a StackPane as your root layout instead of using a VBox. StackPane allows you to stack nodes on top of each other (z-order).
On the button's action you can create a new ProgressIndicator and add it to your StackPane. I have introduced another VBox as the parent to the indicator, because I did not want the indicator to capture all the available space. You can disable the already present VBox to get the greying effect on button's action after the process is done you can enable the VBox again.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage arg0) throws Exception {
StackPane root = new StackPane();
VBox bx = new VBox();
bx.setAlignment(Pos.CENTER);
TextField userName = new TextField("User Name");
userName.setMaxWidth(200);
TextField email = new TextField("Email");
email.setMaxWidth(200);
Button submit = new Button("Submit");
submit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
ProgressIndicator pi = new ProgressIndicator();
VBox box = new VBox(pi);
box.setAlignment(Pos.CENTER);
// Grey Background
bx.setDisable(true);
root.getChildren().add(box);
}
});
bx.getChildren().addAll(userName, email, submit);
root.getChildren().add(bx);
Scene c = new Scene(root);
arg0.setScene(c);
arg0.setMinWidth(500);
arg0.setMinHeight(500);
arg0.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

Javafx label not updating after input from TextField in another scene

I have a label called test that is supposed to display the input value from TextField in the textInput class, from another scene. the value is being sent over to the main class but the label is not updating unless i click the button to go to the dialog box from textInput.
package javafx11;
import application.textInput;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
Stage window = new Stage();
Scene s1;
Scene s2;
Label test;
String input;
Button btn;
#Override
public void start(Stage primaryStage)throws Exception {
window=primaryStage;
VBox layout = new VBox();
s1= new Scene(layout,500,500);
test = new Label("This is where your text will appear");
btn = new Button("Click me");
window.setTitle("Dummy program");
layout.setAlignment(Pos.CENTER);
window.setScene(s1);
window.show();
btn.setOnAction(e -> {
input = textInput.textInput("title", "mnessage");
test.setText(input);
window.setScene(s1);
System.out.println(input);
});
layout.getChildren().addAll(test, btn);
}
public static void main(String[] args) {
launch(args);
}
}
the text input class:
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class textInput {
static String input;
public static TextField userField;
public static String textInput(String title, String message) {
Stage window = new Stage();
VBox layout = new VBox();
window.setTitle(title);
window.initModality(Modality.APPLICATION_MODAL);
Button btn = new Button("Click me to go back");
userField = new TextField();
Scene s1 = new Scene(layout, 500, 500);
window.setScene(s1);
layout.getChildren().add(btn);
layout.getChildren().add(userField);
btn.setOnAction(e ->{
input = userField.getText();
window.close();
System.out.println(input);
});
layout.setAlignment(Pos.CENTER);
window.show();
return input;
}
}
I tried googling it but i cant really seem to understand the solution provided by others.
Its all good, figured it out after tinkering with it a little longer.
in the main class, i had to change window.show(); to window.showAndWait();

I want my JavaFX program to play a video when a button is pressed but only audio plays

I'm new to JavaFX and I need to play a video when a button is pressed, but for some reason I can't quite understand, only the sound is played. Here's my code:
package trailer;
import javafx.event.ActionEvent;
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.Label;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Trailer extends Application implements EventHandler<ActionEvent>{
Button button;
BackgroundImage image;
MediaPlayer player = new MediaPlayer(new Media(getClass().getResource("videonamehere.mp4").toExternalForm()));
MediaView mediaView = new MediaView(player);
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("title content");
button = new Button("button name");
button.setOnAction(this);
Label label = new Label("label content");
label.setTextFill(Color.WHITE);
VBox layout = new VBox();
layout.setPadding(new Insets(200));
layout.setSpacing(20);
layout.getChildren().add(button);
layout.getChildren().add(label);
Scene scene = new Scene(layout, 1920, 1080);
primaryStage.setScene(scene);
scene.getStylesheets().add(Trailer.class.getResource("cssfilename.css").toExternalForm();
primaryStage.show();
}
#Override
public void handle(ActionEvent event){
if(event.getSource() == button){
player.play();
}
}
}
To clarify, the interface with the button, the label, and the CSS effects does show up, but when I press the button only the sound from the video plays.

JavaFX, full screen: app blinks when alert is shown

I have a very simple application in JavaFX: full screen stage with one exit button. Problem is that from time to time (no idea why not always) after clicking on "Exit" button my application blinks (seems that is minimized and maximized in miliseconds).
Any ideas?
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.util.Optional;
public class FullScreenExample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button button = new Button("Exit");
button.setOnAction(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initOwner(stage);
alert.initStyle(StageStyle.UNDECORATED);
alert.initModality(Modality.WINDOW_MODAL);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
Platform.exit();
}
});
VBox box = new VBox();
box.getChildren().add(button);
final Scene scene = new Scene(box);
scene.setFill(null);
stage.setScene(scene);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.setFullScreen(true);
stage.show();
}
}
You are using the Stage style Undecorated:
alert.initStyle(StageStyle.UNDECORATED);
Try to use
alert.initStyle(StageStyle.TRANSPARENT);
This fixes your blinking.
Its a known Issue with the Undecorated Frame in Windows.

JavaFX How to 'hide' TitledPane back after expanding

I have TitledPane, which i want to hide back (after expanding - un-expand it) after pressing a button. Is there any way to do it? I didn't find any way :( Thanks!
Just do
titledPane.setExpanded(false);
Complete example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TitledPaneExample extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Some content");
Button button = new Button("OK");
VBox content = new VBox(10, label, button);
TitledPane titledPane = new TitledPane("Titled Pane", content);
button.setOnAction(e -> titledPane.setExpanded(false));
VBox root = new VBox(titledPane);
Scene scene = new Scene(root, 250, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Label text position

I have a Label with an image and text
final Label label = new Label(labelText);
label.setTextAlignment(TextAlignment.CENTER);
ImageView livePerformIcon = new ImageView(MainApp.class.getResource("/images/Folder-icon.png").toExternalForm());
label.setGraphic(livePerformIcon);
I get this as a visual result:
How I can change the text position? I want to set the text below the Image?
label.setContentDisplay(ContentDisplay.TOP);
Play with this to see the effect of the different alignment settings:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class LabelGraphicAlignmentTest extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Label label = new Label("Some\ntext");
label.setGraphic(new ImageView(getClass().getResource("/images/Folder-icon.png").toExternalForm()));
label.setMaxWidth(Double.POSITIVE_INFINITY);
label.setMaxHeight(Double.POSITIVE_INFINITY);
label.setStyle("-fx-border-color: blue;");
root.setCenter(label);
ComboBox<ContentDisplay> contentDisplayBox = new ComboBox<>();
contentDisplayBox.getItems().addAll(ContentDisplay.values());
contentDisplayBox.getSelectionModel().select(ContentDisplay.LEFT);
label.contentDisplayProperty().bind(contentDisplayBox.valueProperty());
ComboBox<Pos> alignmentBox = new ComboBox<>();
alignmentBox.getItems().addAll(Pos.values());
alignmentBox.getSelectionModel().select(Pos.CENTER);
label.alignmentProperty().bind(alignmentBox.valueProperty());
ComboBox<TextAlignment> textAlignmentBox = new ComboBox<>();
textAlignmentBox.getItems().addAll(TextAlignment.values());
textAlignmentBox.getSelectionModel().select(TextAlignment.LEFT);
label.textAlignmentProperty().bind(textAlignmentBox.valueProperty());
GridPane ctrls = new GridPane();
ctrls.setHgap(5);
ctrls.setVgap(5);
ctrls.setPadding(new Insets(10));
ctrls.addRow(0, new Label("Content display:"), new Label("Alignment:"), new Label("Text Alignment:"));
ctrls.addRow(1, contentDisplayBox, alignmentBox, textAlignmentBox);
root.setTop(ctrls);
Scene scene = new Scene(root, 600, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I had to center the text of a label which acted like a title. The following code snippet did the trick.
final Label title = new Label("Some text");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
Good programming :-)
I was able to grab the button from the fxml file:
#FXML Label countDownClockLabel;
Then when the controller was initialized i set the text position:
#FXML
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
countDownClockLabel.setAlignment(Pos.CENTER);
}
You have to import:
import javafx.geometry.Pos;

Resources