How to open new javafx fxml window under if condtition? - java

I create a simple login form to checking username and password.
I did it and my problem is while checking the username and password is correct open new fxml window.
Can anyone drop some simple code to solve it.

So, below is just an example to get you started but what I usually do is I create some sort of a stage factory so that I don't have a lot of duplicate code.
Let me know if you have any questions.
My example:
if(/*your if condition*/){
YourController controller = new YourController();
FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource(fxmlPath));
loader.setController(controller);
Parent root = loader.load();
Scene scene = new Scene(root, width, height, Color.WHITE);
Stage stage = new Stage();
stage.setX(xPos);
stage.setY(yPos);
stage.setScene(scene);
stage.show();
}
Edit, I felt nice today but I do agree with the first comment that you received. Remember to read through the guide lines next time.

Related

Setting stage icon

My first intention was to set TextInputDialog icon. But I started from setting stage icon. I saw couple SO questions with marvelous answers containing usually 2 lines of code.
First I tried to put this icon to /resources/icons but exception "Invalid URL or resource not found" appeared. To be sure I don't make any mistake writing file path I moved this icon to /source/sample directory. I use code (I will post whole code):
public void start(Stage stage) throws Exception {
FXMLLoader loaderModyfikacjaKonfiguracji = new FXMLLoader(getClass().getResource("FXMLModyfikacjaKonfiguracji.fxml"));
Parent root = loaderModyfikacjaKonfiguracji.load();
stage.setTitle("Modyfikacja konfiguracji");
Image image = new Image("file:icon.png");
//stage.getIcons().removeAll();
stage.getIcons().add(image);
ControllerModyfikacjaKonfiguracji controllerModyfikacjaKonfiguracji = loaderModyfikacjaKonfiguracji.getController();
stage.setScene(new Scene(root, 510, 700));
stage.show();
}
Everywhere it looks so simple to set icon. I also tried .jpg. not using file: throws exception, using file: compiles but I see no effect of changed icon. What am I doing wrong or where is the problem?
I've successfully used this to set an icon before
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("AppIcon.png")));
In my case, the application fxml file and AppIcon.png are in the same directory.
If you dont want to go that route, i would suggest trying
Image image = new Image("file:./icon.png");
But that's a guess.

Can I change Scene using JavaFX automatically?

I am trying to make a game as my term final project. So when life level of one player is less or equal zero then I am trying to load another fxml file on the existing stage automatically. But I can't . I am not sure that if-else logic can change one scene to another. It will be helpful if anyone can give a way to change a scene automatically.
enter image description here
Thanks in advance.
if(Controller.lifeDekhao2<=0){
Parent is = FXMLLoader.load(getClass().getResource("/FXMLPack/First1.fxml"));
Scene isScene = new Scene(is);
Stage window = (Stage) ((Node) (event.getSource())).getScene().getWindow();
window.setScene(isScene);
window.show();
Controller.time_end = System.currentTimeMillis();
}

Jar executable wouldn't open new Scene in javaFx

When I run my code in Eclipse every window opens with no problems.
First Window leads to the second, and the second leads to the third.
When I made my jar executable and started the application again, the transition from the first to the second windows went the same way as in the eclipse, however, when I clicked Start on a second window, the third window never showed up.
This is what Start Button is responsible for:
((Stage)Start.getScene().getWindow()).close();
Stage primaryStage = new Stage();
primaryStage.setTitle("Board");
primaryStage.getIcons().add(new Image("Media/logo.png"));
FXMLLoader loader = new FXMLLoader();
loader.setLocation(GameModeController.class.getResource("Board.fxml"));
BorderPane mainLayout = loader.load();
Scene scene = new Scene(mainLayout);
primaryStage.setScene(scene);
controller.RedSpyMasterTerm();
primaryStage.setOnCloseRequest(e->System.exit(0));
primaryStage.show();
First Window: https://i.stack.imgur.com/Z0rf4.png
Second Window: https://i.stack.imgur.com/LLOZm.png
Third Window: https://i.stack.imgur.com/SxBPg.png
Location of the files: https://i.stack.imgur.com/LeEn3.png
https://i.stack.imgur.com/hPU9p.png

Switching Scenes with several FXML files

I am new to Java FX and trying to build up an "easy" application which consists of a header button bar and a "content area" below.
I've managed a big part like that:
MainWindow.fxml with a Borderpane: MenuBar in the Top Area
And a Pane with fx:id: content in the center area.
Several X.fxml files for the content ()
One Controller which creates the obj content:
#FXML
private Pane content;
and switches the content:
content.getChildren().clear();
content.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));`
Main file which initializes the parent and scene:
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Stage Window = primaryStage;
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Window.setTitle("ICIS - In Car Interactive System");
Window.setScene(scene);
Window.show();`
So far everything works fine!
But now I want to apply a SplitPane and do the "content change" in each side of the Split Pane (TwoWindows.fxml): So I've extended the
Controller with obj. for every Pane of the Split Pane, assigned the fx:id to that pane and want to control them analog to the example before.
#FXML
private Pane SecondWindow1;
SecondWindow1.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));
Well, during compilation everything is fine, but while running I get the Null error exception, so that I assume SecondWindow1 (left half of SlitPane) is not known to the controller.
I also assume, it is because I initialize at the beginning only MainWindow.fxml (which includes the content area) but not the TwoWindows.fxml (SplitPane) which inlcude the SecondWindow1 Object.
Well I've tried since hours now to solve it, but apparently I am overlooking sth. Somebody knows how to fix that problem? Do I need one Controller for every FXML File?

How to get the IDs of nodes inside HTMLEditor, JavaFX

I wish to remove some of the control buttons from HTMLEditor, since I do not need them. for that I need to reach the desired node. How can I know the IDs of nodes inside HTMLEditor? Please see the following. Thank you!
public class myApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("myApp.fxml")); //this fxml has HTMLEditor named htmlEditor.
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
Node someControlInsideHtmlEditor = root.lookup("#htmlEditor").lookup("#what_Is_The_ID_of_This_someControlInsideHtmlEditor")
}
}
Download Scenic View from here
Add this to your application's class path
Add the following line to your start() method's end:
ScenicView.show(scene);
Run the application
Two windows will pop up: the primaryStage with the HTMLEditor and Scenic View's Stage
Now you can visit every node of the Scene Graph. Open the tree at the left pane, and select a Node from the HTMLEditor. You can access the controls by their CSS class.
For example, open HTMLEditor -> ToolBar -> HBox, and select the first Button. Look at "styleClass" in the "Node Details" at the right side. You will need "html-editor-cut". It can be used with this code:
Button cutButton = (Button) root.lookup(".html-editor-cut");
don't know if you're still looking for this answer. In Java 8, and HTMLEditor only has one child, which is a GridPane. The first two children of that are the ToolBars, the third is a WebView. Remove the first two children from the gridpane to do the formatting you want. Does that help?

Categories