In JavaFX 2 you can use a ProgressInidicator to show that some background task is in progress.
I would like to show ProgressIndicator centered on top of a GridPane, so that it overlays the GridPane. In Swing you can use GlassPane for this. How do I do it in JavaFX 2?
You can use StackPane. For info about layouts see Built-in Layout Panes.
Related
I have an AncharPane in which all components are placed/designed using Fxml but the problem is, when the frame resize all components are stick with its current position.
With AnchorPane, you can't get automatic resizing unless you have to resize your views one by one in the controller/code.
If you want autoresize, use BorderPane, VBox or HBox.
If you're using scene builder you can achieve this by selecting the component you'd like to make resizable, then go under layout and set the constraints.
You can also achieve this in your Controller class like this:
AnchorPane.setTopAnchor(yourTextField, 35);
AnchorPane.setLeftAnchor(yourTextField, 27);
AnchorPane.setRightAnchor(yourTextField, 120);
I used a TextField for this example, but you can do it with other components.
If the AnchorPane has a border and padding set, the offsets will be measured from the inside edge of those insets.
To autoresize I recommend BorderPane. You can also use VBox and HBox to organize you Scene.
I'm just starting with Scene Builder and java. I've laid out my main GUI but when I preview it, it has wide borders for all my h/v boxes, anchor panels, etc. How do I hide the borders when I actually run (preview) my UI? Is there a way in scene builder (preferred) or will I have to code this instead?
Default border on layouts
There is NO default-border on layouts which can be visible. In case, you need to remove border from layouts you can set css code using setStyle() method,
yourPane.setStyle("-fx-border-width: 0px");
You can also use external css document for styling components.
Wide border on preview
Actually, the fxml preview means that showing your design in a window. So you might get confused with window frame border as shown in the below preview,
But you can make borderles- window by styling your stage using initStyle(),
primaryStage.initStyle(StageStyle.UNDECORATED);
You can also use TRANSPARENT style as well but you have to manage your title bar for customized-window.
(source: makery.ch)
I used the drag and drop interface to put the components where I want them to be, but when the window is resized they lose their relative position. I have attached a screen shot of my hierarchy and of two windows to show how the components lose their position.
Hierarchy
Fullscreen
If you want to use the AnchorPane to layout your components, you can set the Anchor Pane Constraints, like in the image below:
This way it doesn't matter if you resize the screen, the button will always stay 10px far from the AnchorPane's right border.
When you use the AnchorPane to place components on screen you are not going to have a relative positioning, You should use others containers to layout your application. Read more about how to use Layout Panes here: Using Layout Panes
You need to study how to use Layout Panes. Dragging and dropping components results in absolute positioning, which really bad practice.
I am trying to create a splash screen like the example I've provded.
It seems that AnchorPane does not allow transparent background, I've tried setting the css of the AnchorPane to -fx-background-color: rgba(255,0,255,0.1) ; but the white background still shows up.
All I have in my fxml file is a AnchorPane with ImageView with contain the png image
I've looked everywhere but can't find any solution, any help would be appreciated. Thanks
Try this JavaFX splash sample created for the Stackoverflow question: Designing a splash screen (java). And a follow up sample which also provides application initialization progress feedback.
JavaFX does offer the Preloader interface for smooth transfer from splash to application, but the above samples don't make use of it.
The splash samples above also don't do the transparent effect, but this dialog sample shows you how to do that and you can combine it with the previous splash samples to get the effect you want.
The transparent effect is created by:
stage.initStyle(StageStyle.TRANSPARENT).
scene.setFill(Color.TRANSPARENT).
Ensuring your root node is not an opaque square rectangle.
Which is all demonstrated in Sergey's sample.
Related question:
How to use javaFX Preloader with stand-alone application in Eclipse?
Update Apr 2016 based on additional questions
the preloader image isnt in the foreground. I have tried stage.toFront(), but doesnt help.
A new API was created in Java 8u20 stage.setAlwaysOnTop(true). I updated the linked sample to use this on the initial splash screen, which helps aid in a smoother transition to the main screen.
For Java8+
For modena.css (the default JavaFX look and feel definition in Java 8), a slight shaded background was introduced for all controls (and also to panes if a control is loaded).
You can remove this by specifying that the default background is transparent. This can be done by adding the following line to your application's CSS file:
.root { -fx-background-color: transparent; }
If you wish, you can use CSS style classes and rules or a setStyle call (as demonstrated in Sergey's answer) to ensure that the setting only applies to the root of your splash screen rather than all of your app screens.
See related:
how to make transparent scene and stage in javafx?
You need to have transparent Stage and Scene for that. Pane itself doesn't have a color.
public void start(Stage primaryStage) {
Button btn = new Button("Say 'Hello World'");
AnchorPane root = new AnchorPane();
root.getChildren().add(btn);
// Java 8: requires setting the layout pane background style to transparent
// https://bugs.openjdk.java.net/browse/JDK-8092764
// "Modena uses a non-transparent background by default"
root.setStyle("-fx-background-color: transparent;");
Scene scene = new Scene(root, 300, 250, Color.TRANSPARENT);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
}
I have removed the default borders around the primary stage:
stage.initStyle(StageStyle.UNDECORATED)
This removes the window borders which is what I want, but now I'd like to have a drop shadow under the window.
The top level BorderPane object has a dropshadow effect applied, but the shadow is cut off:
You can see the shadow slightly on the bottom right, but as soon as the main application area ends the shadow is cut off.
I created an example for this earlier. Use the specific revision linked as in later revisions I dropped the shadow effect from the dialog.
The sample places the stage content in a StackPane containing two panes. The shadow is only applied to the background Pane and the dialog content is placed in a top pane. The background of the top pane is slightly inset from the bottom pane so that the background and shadow can show through.
Further discussion is in a thread on displaying a shadow around an undecorated/transparent stage.