Search code examples
javafxvisibility

JavaFX Fill empty space when component is not visible?


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


Solution

  • 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.