Can I change Scene using JavaFX automatically? - java

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();
}

Related

JavaFX setAlwaysOnTop on a fullscreen stage

I have a main stage with these properties. It is meant to be a secure "lockdown stage"
public static Stage getSecureStage(Window window) throws IOException {
Stage stage = new Stage();
//Take up the entire screen boundaries
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
stage.setHeight(visualBounds.getHeight());
stage.setWidth(visualBounds.getWidth());
//Secure the stage
stage.setFullScreen(true);
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.setFullScreenExitHint(null);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(getScene(window));
return stage;
}
With this lockdown stage, I want to be able to have popup windows within the stage stay on top
As you can see, the stage initializes on top of the lockdown stage. However, if I am to click off of that popup and back onto the main lockdown stage, the lockdown stage takes priority and puts itself over the popup. It does not minimize it, but just goes on top.
I have given the popup the properties
popup.setAlwaysOnTop(true);
popup.initOwner(lockdownStage);
however, that does not seem to do the trick. I had not previously had this issue running on Linux, with the same code. Please let me know if you need anymore information
Solved this by getting rid of the
stage.setFullScreen(true);
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.setFullScreenExitHint(null);
and setting it instead to
stage.setMaximized(true);
With the previous fullscreen setup, you could also use the stage.initModality(Modality.APPLICATION_MODAL); on the popup window. In my case, I wanted to be able to interact with both windows not singling one out with the modality feature.

How to open new javafx fxml window under if condtition?

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.

Handling different users in JavaFX

I am learning Java and JavaFX. I have an application that has some ComboBox components that are only visible if logged in as an admin My problem is that the application populates the Combobox with system variables when it starts, If a user logs in then i get a null pointer and the application doesnt start because the ComboBox doesnt exist.. When an admin logs in the application starts correctly. Here is how i am trying to get it to work.
private void loginpressed(ActionEvent event) throws IOException
{
if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashuser))
{
Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUserUI.FXML"));
Scene home_page_scene = new Scene(root);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(home_page_scene);
app_stage.show();
}
else if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashadmin))
{
Parent root = FXMLLoader.load(getClass().getResource("LaserControllerUI.FXML"));
Scene home_page_scene = new Scene(root);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(home_page_scene);
app_stage.show();
}
else{
errorMessage.setText("Login Incorrect!");
System.out.print("false");}
}
So i have two seperate FXML depending on who logs in. How do i handle this?
In general, you should avoid making your application in a way in which it will need administrative privileges, if it does need these privileges you will need to give this feedback to the user, or enforce this condition, you can google how to do this such as in this link for Microsoft: https://technet.microsoft.com/en-us/library/ff431742.aspx. Note that you can have access to the current user through the System properties in Java, and you can also save properties based on a user as well, if that helps.
That casting your doing completely unnecessary. I would look up some GUI tutorials on MVC for Java if I was you. Once you learn this pattern you will have a light bulb moment and everything will just make sense. There are alterations of this pattern you should learn the main ones.
Not following any GUI patterns will cause you immense pain and complications. I would estimate over 100,000 questions on SO would not exist if people knew this pattern. This includes your question. GUI and functionality should be completely separated. Your question would simply be: how do I get a list of all Users, which would be a duplicate and not need to be asked.
I would use one FXML Document. If they have pretty much the same components, I would set the locations and visibility of the components after a user logs in. What is set to visible should depend on the users account type.

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