Start the application window maximized in JavaFX FXML not working properly - java

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

Related

Switch between scenes in fullscreen mode JavaFX [duplicate]

I have read several questions/solutions here that are related to my problems. but nothing seems to work.
so I have a primarystage in fullscreen mode, say if i click a button it changes the scene. but the stage seems to display the taskbar. also I resolved the issue by adding this to all of the scene methods..
stage.setFullScreen(false);
stage.setFullScreen(true);
BUT, the transition in scenes is not that fluid. first it goes to desktop and back to fullscreen.. which is not the ideal solution.
here is my code for the primary stage:
public static Stage stage;
private static AnchorPane mainLayout;
#Override
public void start(Stage primaryStage) throws IOException
{
Main.stage = primaryStage;
Main.stage.setTitle("Raven App");
stage.initStyle(StageStyle.UNDECORATED);
stage.setFullScreen(true);
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Main.showMain();
}
here is my code for changing the scene:
public static void UserLogin() throws IOException
{
FXMLLoader loader=new FXMLLoader();
loader.setLocation(Main.class.getResource("page/UserHomeLogin.fxml"));
mainLayout=loader.load();
Scene scene=new Scene(mainLayout);
stage.setScene(scene);
stage.show();
}
I don't know if this is a bug or something. But i thought if you set your primary stage to full screen. and should be fullscreen all through out regardless of scene.
also, if i have a primary stage in full screen mode.. and a secondary stage NOT in full screen mode. the primary stage seems to disappear if i click a button to show the secondary stage. I wanted to show the secondary page on top of the primary stage, and the primary stage should not be clickable unless the secondary page is closed.
my code for showing the secondary stage:
public static void PasswordVerify() throws IOException
{
Stage stage = new Stage();
Parent root = FXMLLoader.load(Main.class.getResource("page/PassConfirm.fxml"));
stage.setScene(new Scene(root));
stage.setTitle("popup window");
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
stage.show();
}
Instead of creating a new scene, just change the root of the existing scene:
public static void UserLogin() throws IOException {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(Main.class.getResource("page/UserHomeLogin.fxml"));
mainLayout=loader.load();
stage.getScene().setRoot(mainLayout);
// or just
// scene.setRoot(mainLayout);
// if you already have a reference to the scene
}
The second thing you are asking is not really possible. In JavaFX, on many platforms "full screen mode" is really implemented as "exclusive screen mode"; so there is a unique window visible. So you would need another solution entirely to this, that didn't involve displaying a new window at all.

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

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

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.

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

JavaFX scene scales differently in Scene builder and run time on Windows 10

I using Scene Builder with Intelij IDEA to build a JavaFX application. The scene looks correct when I preview it in Scene Builder but when I then execute it in IntelliJ the scene is scaled up 1.5x. In Scene Builder the stage is 1280x800 but when I run the program it is 1920x1200 despite me setting the scene size 1280x800.
This seems like it might be due to Windows 10 scaling the application. If so, is there a way to stop my application from scaling?
Thank you!
#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, 1280, 800));
primaryStage.show();
}
It has nothing to do with windows scaling. all you have to do is to remove these arguments inside your Scene i.e 1280 and 800, but don't remove root.
new code will be like -
#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);
primaryStage.show();
}

Categories

Resources