JavaFX BorderPane inside an AnchorPane? - java

I'm currently trying to show a BorderPane inside my AnchorPane (that is also inside my StackPane). I tried to show a new Scene but it show me 2 items in my Windows taskbar, and this is not really what I want.
For exemple, when you open the Preference in Eclipse, u get another window that is not visible in the taskbar.
So basically, the only thing I need is a window that looks like an ImageView, like anchored in the AnchorPane , not resizable and undecorated.
Expected result at end
This is what I tried:
btnDice.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent arg0) {
Scene newScene = new Scene(new BorderPane(), 230, 100);
Stage newWindow = new Stage();
newWindow.setScene(newScene);
newWindow.show();
}
});

Related

Is it possible to get the 'new color' in JavaFx ColorPicker?

I'm trying to get the 'new color' value from a ColorPicker ('nouvelle couleur' in my image because it's french).
colorPicker.setOnAction((ActionEvent e) -> {
text.setFill(colorPicker.getValue());
});
When you set up a EventHandler for a ColorPicker, it only returns the value of the ColorPicker when you close it.
So I was wondering, is it possible to get this value ?
Sorry if there are any mistake, English is not my native language.
Yes, the valueProperty() from the ColorPicker control is updated every time you modify the selection in the dialog. Only if you cancel the change, it is backed out.
So you just need to add a listener to that property or bind it as required.
#Override
public void start(Stage primaryStage) {
ColorPicker picker = new ColorPicker(Color.ALICEBLUE);
Text text = new Text("Color Picker");
VBox root = new VBox(10, text, picker);
root.setAlignment(Pos.CENTER);
text.fillProperty().bind(picker.valueProperty());
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

How to block a main window in JavaFX

There is a button in a scene of the main window. When it's clicked, the new window is created, according following code:
public static void create()
{
Stage stage = new Stage();
AnchorPane pane = new AnchorPane();
//Here i add some into pane
stage.setScene(new Scene(pane));
stage.setWidth(500);
stage.setHeight(600);
stage.show();
}
I'd like the main window will remain blocked (i.e. an user won't be able to click on buttons, type text, resize or interact with it with other ways) until the additional window is closed.
Here is a link that shows exactly what you're looking for: http://www.javafxtutorials.com/tutorials/creating-a-pop-up-window-in-javafx/
Here is the main part of the code you need to add:
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(btn1.getScene().getWindow());
stage.showAndWait(); // This forces the program to pay attention ONLY to this popup window until its closed
Hope this helps.

JAVAFX How to make a button stacked behind a Label clickable

How to make a button stacked behind a Label clickable?
Button
Button btn = new Button();
//TODO: button's main function, like onPressed, onReleased etc. here...
Custom label
CustomLabel sp = new CustomLabel("Some texts here"));
//TODO: custom label's main function, like a translucent background etc here...
//main purpose of this is to overlay the button
Main Pane
StackPane mainPane = new StackPane();
mainPane.getChildren.addAll(btn, sp);
With this, the area which the custom label overlays becomes unclickable.
Is there anyway to make the button still clickable even though overlay-ed, for example?
Or is there another way to do it? Something like setting label to not visible on click?
EDIT : To answer Itamar Green 's question..
By using the example as shown in this link: Mouse Events get Ignored on the Underlying Layer , it still does not seems to work. The button stacked under the layer is still not clickable.
sp.setPickOnBounds(false);
You can set the mouseTransparent property of the element(s) drawn on top to true. Note that this way all descendants of the node are ignored for mouse events:
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
System.out.println("Hello World!");
});
Region region = new Region();
region.setStyle("-fx-background-color: #0000ff88;");
region.setMouseTransparent(true);
StackPane root = new StackPane(btn, region);
Scene scene = new Scene(root, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
Comment out
region.setMouseTransparent(true);
and the button will no longer react to mouse events in any way...
thanks for your help. I think I have solved my own question.
All I did was set a clickable event for my label.
Label sp = new Label("some texts here")
sp.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
//copy over the button's event.
}
});
Not sure if there is any better way of doing this...
in addition with the correct answer. you can enable or disable " mouse transparent " property via fxml with scenebuilder

How can i lock a screen in a certain position (javafx)?

i have to make a game for a school project and i was wondering if it's possible to lock a screen in a certain position on the screen. I have a settings button that opens a new stage and i want to make that scene immovable is this possible?
This is the code where i display the settings screen:
Public void buttonAction(){
btnSettings.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
GridPane settingsGrid = new GridPane();
Scene scene1 = new Scene(settingsGrid,375,600);
Stage settingsStage = new Stage();
settingsGrid.setHgap(10);
settingsGrid.setVgap(10);
//settingsGrid.setGridLinesVisible(true);
settingsStage.setTitle("Settings");
settingsStage.setResizable(false);
settingsStage.initStyle(StageStyle.UTILITY);
settingsStage.initModality(Modality.APPLICATION_MODAL);
settingsStage.setScene(scene1);
settingsStage.showAndWait();
}
});
}
Thanks in advance!
you could try this:
settingsStage.initStyle(StageStyle.TRANSPARENT);
However, you could only have one initStyle if I remember correctly. So you have to settingsStage.initStyle(StageStyle.UTILITY); in order to use the above code. What it does is it make the background transparent and the user could not change the position.

JavaFX fade out stage and close

I'm having difficulty finding out if this is even possible. The common behavior that most people want is to fade out an extension of a node, which is entirely possible through a FadeTransition
However, I'm trying to fade out an entire stage, so imagine closing the running program and instead of simply killing the windows (i.e. showing -> not showing), I would like the window (stage) to fade out over 2 seconds like a toast or notification would.
Create a timeline with a KeyFrame that changes the opacity of the stage's scene's root node. Also make sure to set the Stage style and the scene fill to transparent. Then make the program exit once the timeline is finished.
Below is an application with a single big button that, when clicked, will take 2 seconds to fade away, and then the program will close.
public class StageFadeExample extends Application {
#Override
public void start(Stage arg0) throws Exception {
Stage stage = new Stage();
stage.initStyle(StageStyle.TRANSPARENT); //Removes window decorations
Button close = new Button("Fade away");
close.setOnAction((actionEvent) -> {
Timeline timeline = new Timeline();
KeyFrame key = new KeyFrame(Duration.millis(2000),
new KeyValue (stage.getScene().getRoot().opacityProperty(), 0));
timeline.getKeyFrames().add(key);
timeline.setOnFinished((ae) -> System.exit(1));
timeline.play();
});
Scene scene = new Scene(close, 300, 300);
scene.setFill(Color.TRANSPARENT); //Makes scene background transparent
stage.setScene(scene);
stage.show();
}
public static void main (String[] args) {
launch();
}
}

Categories