Monday, May 19, 2014

Decorating a Stage in JavaFX

We need a start to build our UI. The top most level container is the stage. It will contain your window utility buttons like close, minimize, restore. It forms the basis of the start of your layout. After a stage has been created, you put a scene over it. 

Creating a stage is simple. You do not create it, but the javafx engine will do it for you. This is the main stage which is created for you on the main javafx thread. You can have multiple stages in an application after the first one and these can be created by you.

A simple code to show how you use the default stage.

public void start(Stage stage) throws Exception {
      stage.setWidth(1024);
      stage.setHeight(768);
      stage.setTitle("First");
      stage.show();
 }


This simply displays a window with the given dimensions and title when run. Now, this is the simplest form of a stage. I state this because this is the default stage javafx creates. 

 

Looking at the window that is created above, we observe that this has all the functions of a window. It can be closed, minimized, dragged, restored etc
Although, we should be happy by this, what we are getting here is a full featured window with minimum lines of code for us. Still, there are situations that call for different requirements. 
Let's say we wanted to create our own design or we do not want the window to be dragged by the users. The requirements are endless. Thankfully, a lot was thought about these and hence we have been given certain ways of doing the same.

A very simple property of the stage is "style". This will control how your stage will appear once you apply the same. 

You can apply these using 
stage.initStyle(StageStyle.DECORATED);

1. StageStyle.DECORATED
    The default style with all the functions available.


2. StageStyle.UNDECORATED
    A stage with a solid white background and no decorations.  

3. StageStyle.TRANSPARENT
    A stage with a transparent background and no decorations.

4. StageStyle.UTILITY
    A stage with a solid white background and minimal platform decorations.

Out of the above Utility will only have a closing button. Undecorated and transparent as mentioned are very self explanatory. However, I do see much of a difference between the two and hence I always use undecorated if required. 
 
Sometimes, we need an app to be full screen. Javafx also provides this functionality. 
stage.setFullScreen(true);
stage.setFullScreenExitHint("Press Exc to exit full screen");


Stage can be lot fun. All, we need is to start working on it and exploring it's capabilities.

No comments:

Post a Comment