JavaFX Fill empty space when component is not visible?


Question

i use Linux Suse 12.3, JDK 1.7.0-45, JavaFX 2.2.

my Question is: why the following Code not working and how to implement a toggleShow/hide functionality?

here is my Test Code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class Test extends Application {
    public static void main(String[] args) { 
        launch(args); 
    }

    @Override
    public void start(final Stage stage) {
        AnchorPane root = new AnchorPane();


        BorderPane inner = new BorderPane();

        AnchorPane.setTopAnchor(inner, 0.0);
        AnchorPane.setRightAnchor(inner, 0.0);
        AnchorPane.setBottomAnchor(inner, 0.0);
        AnchorPane.setLeftAnchor(inner, 0.0);

        final HTMLEditor center = new HTMLEditor();

        final ToolBar top = new ToolBar();
        final Button button = new Button("hide");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                top.setVisible(false);
                //center.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE);
            }
        });     

        center.setManaged(false);
        top.getItems().add(button);
        //top.managedProperty().bind(top.visibleProperty());
        top.setManaged(false);

        inner.setTop(top);
        inner.setCenter(center);

        root.getChildren().add(inner);
        Scene scene = new Scene(root,600,400);
        stage.setScene(scene);
        stage.show();
    }
}

what i want is the same effect as the Solution of Sergey to this Question but without changing width/height!:

How to solve the overlapping of the controls each other belonging to two different panes

as i said its just a Test Code. i tried using another Layouts as BorderPane but still not working. i don't want to recalculate the size's manually ...etc. Removing the node and adding it again is not an option for me.

whats wrong in my Code? any idea is welcomed! thanks

1
4
5/23/2017 10:28:41 AM

Accepted Answer

Filling the empty space with the usage of BorderPane seems to be not an option, due to the prompt in its javadoc:

BorderPane lays out each child set in the five positions regardless of the child's visible property value; unmanaged children are ignored.

Additionally, using AnchorPane just for resizable content as:

AnchorPane.setTopAnchor(inner, 0.0);
AnchorPane.setRightAnchor(inner, 0.0);
AnchorPane.setBottomAnchor(inner, 0.0);
AnchorPane.setLeftAnchor(inner, 0.0);

seems to be an overusing. Just using the VBox will be more suitable for your layout case.

Rewritten test code:

@Override
public void start(final Stage stage) {
    final HTMLEditor center = new HTMLEditor();
    final ToolBar top = new ToolBar();

    final Button button = new Button("hide");
    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            top.setVisible(false);
            top.setManaged(false);
        }
    });
    top.getItems().add(button);

    VBox inner = new VBox();
    inner.getChildren().addAll(top, center);
    Scene scene = new Scene(inner, 600, 400);
    stage.setScene(scene);
    stage.show();
}

Now the question is;
How are you going to implement the "show" part of your "show/hide" toggle bar? Since there is no clue about it in your question.

5
11/12/2013 1:05:48 PM

Try to remove the top.setManaged(false); line and then put the toolbar into a Group object like this:

    Group g = new Group();
    Region spacer = new Region();
    spacer.setPrefWidth(10000);
    spacer.setMinWidth(100);    
    top.getItems().add(spacer);
    g.getChildren().add(top); //Toolbar here...
    inner.setTop(g); //set Group into Borderpane instead of the toolbar

After clicking the button the free space is consumed entirely by the HTML-Editor.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon