How to change Scenes in JavaFX without FXML - css

I'm trying to change Scenes in my Java FX program with resetting the Scene like
stage.setScene(new Scene(new StartupPane(stage),Toolkit.getDefaultToolkit().getScreenSize().getWidth(),Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
Is this a proper way to do it?
I also have the problem that the CSS style doesn't apply on the new Scene after the change of a Scene. Is there a proper solution for it or do you recommend me to use FXML (actually I have no experience with FXML)?

Why do you want to change the scene? In general you can just change the root node of the scene and there is no need to change the scene itself.
Changing the scene can be done but this is independent of the question whether to use FXML or not.

thanks a lot, it's working fine now for me. I only found answers with changing the XML controller or something like that, therefore I asked for XML.
Thanks

Related

What is the main role of the scene in javafx?

A stage in javafx requires exactly one scene, and a scene requires exactly one root node.
So I want to know what's the main role of the scene? It seems like a scene connects two sides that can be connected directly without an intermediate.
I want juste to understand the logic.
Thank you.
Stage represents a native system window. Scene is the content of this window with JavaFX controls, nodes, stylesheets, etc.
You can change Scenes inside a Stage during your UI workflow without a need to create a new system window each time.

Alternatives for paintComponent in javafx

I have a question but maybe it was an old question.
I saw a class that extends JPanel and an override function called paintComponent for updating panel.
I use javafx, so i want to know is there any alternative for paintComponent?
I want to update a scene in my game loop, or when i use setScene for my stage.
Thanks.

How to create a new image from a GroupLayer

I added a couple of images into a GroupLayer. Does anyone know how to create an image from that GroupLayer?
Thanks.
Just a quick idea:
Surface.drawLayer(Layer layer);
Renders the supplied scene graph into this surface. This renders the scene graph into the surface's texture using the main render pipeline, so custom shaders (on platforms that support them) are handled properly. NOTE: if there is a loop in the scene graph (like you add an ImmediateLayer that calls surf.drawLayer(graphics().rootLayer())) you will get the infinite loop that you deserve. Caveat renderer.

how to bind scene with stage in javafx2

Actually I want to switch from one scene to another scene on particular event. So i can do that by setting scene of the stage without binding but i want to bind my stage with scene as soon as a flag changes my scene will change automatically....pls help
The JavaFX Demos and Samples Downloads contains an application called FXML-LoginDemo. It demonstrates how to change scenes in a stage. The important part happens in the method replaceSceneContent. As for automatically changing the stage when a flag in your application is updated, there are several ways to do that. I recommend using a PropertyChangeListener or using JavaFX withCDI and then using CDI events. I used the latter approach in my application and it works really well.

Make a javafx stage the owner of a JDialog?

Display a swings JDialog containing a JRViewer for Jasper Report, from within a javafx application menu item click. BUT the JDialog is not MODAL even after setModal(true) as it is not owned by javafx stage. How to make a javafx stage the owner of a JDialog? Alternatively how to display a Jasper report inside a javafx stage, scene?
Don't use a JDialog. In JavaFX you use Stages. Put your Jasper report inside a Scene, put the Scene in a Stage. I think you can't put Swing components inside JavaFX components, so you must check this link: Integrating JavaFX into Swing Applications
Code to make a JavaFX dialog:
Stage stage = new Stage();
stage.setTitle("Title");
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(primaryStage);
stage.initStyle(StageStyle.UTILITY);
(...)
Scene scene = new Scene(...);
stage.setScene(scene);
In the code above the Stage will behave exactly like a dialog.
Also check this:
How to create a JavaFX dialog?
Hello World, JavaFX Style
Back to the first (base) question: It's not possible to make a Stage the owner of a JDialog. But, after setting the content of the SwingNode
swingNode.setContent(xxx);
you can get the parent of xxx (may be the parent of the parent of xxx too...) until you find a (AWT-) Frame or (AWT-) Dialog. In the case of SwingNode it's a JLightweightFrame. This object you can use as the owner to create your JDialog.
But, I think there is a bug in the JavaFX SwingNode mechanism. The JDialog appears functionally modal, if you set it modal (setModal(true)), but it may appear behind the main (JavaFX) window. It's still possible to move the main window, to bring it in front and so on, what should not be possible if there is a modal dialog. But this is another topic.
To view a Jasper Report in javafx ( jdk 1.8> ) stage
JasperPrint jp = JasperFillManager.fillReport(reportsource, params,getCon());
SwingNode swingNode = new SwingNode();
swingNode.setContent(new JRViewer(jp));
AnchorPane anchorPane = new AnchorPane();
AnchorPane.setTopAnchor(swingNode,0.0);
AnchorPane.setBottomAnchor(swingNode,0.0);
AnchorPane.setLeftAnchor(swingNode,0.0);
AnchorPane.setRightAnchor(swingNode,0.0);
anchorPane.getChildren().add(swingNode);
Scene scene = new Scene(anchorPane);
Stage stage = new Stage();
stage.setHeight(550);
stage.setWidth(600);
stage.setAlwaysOnTop(true);
stage.setScene(scene);
stage.showAndWait();
Alternatively how to display a Jasper report inside a javafx stage, scene?
Output HTML from your Jasper report and load the report in a JavaFX WebView.
Make a javafx stage the owner of a JDialog?
I don't think this is possible. What should work instead is to make your application a Swing application and host your JavaFX content inside a JFXPanel inside a Swing JFrame. Then you have no Stage and the owner of the JDialog can be the JFrame hosting the JFXPanel.
now it's possible to do that
what you need now is install jdk 8
because swing content in javafx is introduced in jdk8
#FXML
private SwingNode swingNode;
...
JasperPrint jasperPrint = JasperFillManager.fillReport(FileName, hash, connect);
swingNode.setContent(new JRViewer(jasperPrint));
oracle just add tutorial for this too
for the next you can follow this link
oracle tutorial embed swing in javaFX
I have written a JasperReports print preview stage class in JavaFX 8. It is a pure JavaFX code and there is no need for use of SwingNode class.
In comparison to the JRViewer it does not support links and parts, but print preview window does not need it anyway. On the other side, code is much, much shorter and therefore it is easy to understand and to adopt further to everyone's needs.
Source code for this class is freely available at GitHub repository.

Resources