"Hello World" app in Java FX with minimal Java code - java

I selected Java FX between it and Swing because I want that view (*ML) and logic will be split (impossible in Swing).
Here is the "Hello world" template in IntelliJ IDEA:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Two meddlings to View in the Logic part:
Setting title - setTitle()
Setting top-level element size: primaryStage.setScene(new Scene(root, 300, 275));.
Can I set same settings in FXML?

The FXML loader will give you the root element, which already has a prefWidth and prefHeight.
You can't set the title but you can drop the size for the scene and it will be taken from the root element automatically.
Another thing that has to be set for the Stage explicitly are size constraints. You can have an initial size but the user can drag it around however he/she wants.

You can set size using the
prefHeight="yourHeightValue" prefWidth="yourWidthValue"
sets in your scene's root element. You can create scenes using SceneBuilder and it will generate related FXML for you.

Related

Stage blink on load

I need to maximize Stage on load via code.
Here is my code:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Контрола трошкова кућног буџета");
primaryStage.setMaximized(true);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
I have all needed imports. Problem is when run it primaryStage blink for a part of second in size created in Scene Builder, then maximize as intended and I want it to show Maximized. It happend in Eclipse and when make runnable JAR and run it with *.bat file.
Is there anything I am doing wrong?
For testing I created small app in WPF it works properly, no blinking.
EDIT: When stage is in normal size and I click Maximize button, it maximizes stage as I want. Is there possibility to write code to simulate or call Maximize button on top of window? Othere way besides
stage.setMaximized(true);
because that line doesn't work well.
Here is a workaround, set the stage's opacity to 0 before showing it so the resizing happens while the stage is not visible, then change it back to 1:
primaryStage.setOpacity(0);
primaryStage.show();
primaryStage.setOpacity(1);

How do i change the javafx intellij template to my own template if i create a new javafx application?

I do not want to use the Intellij IDEA template anymore when creating javafx programs. I want to use my own template because I can work a lot better with it. Here is an example of what I mean.
Intellij template, I do not want to use this anymore:
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
My template, I want to use this template:
#Override
public void start(Stage primaryStage) throws Exception{
StackPane root = new StackPane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
You can create a user defined template with quite simple steps.
create a new project using the build-in JavaFX template
amend the Main class with your changes
in the Tools menu select Save project as template...
- save it e.g. as JavaFX own
- after that it will be available in the new project wizard in category User-defined

JavaFX permanently maximized window

As the title suggests, I want to make window in JavaFX and make it permanently maximized (i.e. fullscreen).
This is sample code that produces error... well, unexpected behavior.
public class Resize extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Scene scene = new Scene(root);
primaryStage.setMaximized(true);
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Please note setMaximized and setResizable. Everything works until... I press ⊞↓ (WinKey+DownArrow), at which point my window gets resized, and I can't get it maximized again. I'm running the code from NetBeans on Windows 10.
Here's a screenshot. .
You can force the application to be either full screen or minimized, but never windowed, as per your comment, by first doing
primaryStage.setFullScreen(true);
and then disabling the ability to exit full screen by doing
primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);

How To Use CSS For JavaFX Projects

I am quite new to JavaFX and was wondering if there is a way to assign a css file to the entire JavaFX project/scene. Currently, I have to apply the stylesheet to every element I want to be styled with css. I am working with IntelliJ and Scene Builder 2.0. I have tried adding the stylesheets in my Main class, start method as shown below but it did not seem to have the desired effect.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("JavaFXTest.fxml"));
primaryStage.setTitle("JavaFX Test");
primaryStage.setScene(new Scene(root, 300, 275));
root.getStylesheets().add("#../styling/styles.css");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
I realized that it was a problem with the path to the css file. I changed it to:
root.getStylesheets().add("styling/styles.css");
and achieved the desired results. This way I do not need to manually add the stylesheet to every element in the UI, rather it works across the entire scene now. Just declare your classes/id's as per normal css convention to add styling.
You can add a style sheet to the scene:
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(event -> System.out.println("Hello World!"));
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add("/styling/styles.css");
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
In the above example the style sheet is located at /styling/styles.css relative to the source root.

Start the application window maximized in JavaFX FXML not working properly

I want my Javafx FXML application to start maximized so I used the method setMaximized(true) in my stage.
The program opens as Maximized no problem on that, but the problem is that there is a small black area that flashes for half a second on the application startup just before the window appears.
Here's a recording (gif) of what I describe:
I figured out that the problem is with the scene as it is trying to open in its prefWidth & prefHeight then it scales up to fit the stage.
How can I fix this and make the program start as normal programs do?
here's my start() method:
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("editor.fxml"));
primaryStage.setTitle("Simple Text Editor");
primaryStage.setScene((new Scene(root)));
primaryStage.setMaximized(true);
primaryStage.show();
}
The only workaround I found is this:
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("editor.fxml"));
primaryStage.setTitle("Simple Text Editor");
primaryStage.setScene(new Scene(root));
primaryStage.setMinWidth(450);
primaryStage.setMinHeight(300);
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.setMaximized(true);
primaryStage.show();
}

Categories

Resources