gluongluon-desktop

Toolbar is not hidding on ParticleApplication


I have the following code:

@ParticleView(isDefault=true, name="login")
public class LoginView extends FXMLView {

   public LoginView() {
       super(LoginView.class.getResource("login.fxml"));
   }

   @Override
   public void start() {
       ((LoginController) getController()).postInit();
   }

   @Override
   public void stop() {
        ((LoginController) getController()).dispose();
 }

}

And the controller relevant code is:

public class LoginController {
  @Inject
  ParticleApplication app;

  @Inject
  private ViewManager viewManager;

  @Inject
  private StateManager stateManager;

  @Inject
  private MenuBar menuBar;
  @Inject
  private ToolBar toolBar;
  @Inject
  private StatusBar statusBar;

  @FXML
  private TextField txfUsuario;
  @FXML
  private PasswordField txfPassword;

  public void initialize() {
    ActionMap.register(this);
  }

  public void postInit() {
      app.setShowCloseConfirmation(false);
      toolBar.setVisible(false);
      menuBar.setVisible(false);
  }
}

The menubar is not visible (but the space is still there) but the toolbar is still visible.

Any sugestions?


Solution

  • If you run this short test with a regular JavaFX Application, you will notice the same behavior:

    @Override
    public void start(Stage primaryStage) {
        ToolBar toolBar = new ToolBar(new Button("Click"));
        StackPane pane = new StackPane(new Label("A label"));
        pane.setStyle("-fx-background-color: yellow");
        BorderPane root = new BorderPane(pane);
        root.setTop(toolBar);
    
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();
    
        toolBar.setVisible(false);
    }
    

    toolbar

    According to JavaDoc for a node's managed property:

    Defines whether or not this node's layout will be managed by it's parent. If the node is managed, it's parent will factor the node's geometry into its own preferred size and layoutBounds calculations and will lay it out during the scene's layout pass. If a managed node's layoutBounds changes, it will automatically trigger relayout up the scene-graph to the nearest layout root (which is typically the scene's root node).

    This means the borderpane's top region keeps the preferred size given by the toolbar (which by default is managed), regardless its visibility.

    When you set the visibility of a ToolBar to false, you need as well to release its space, by setting managed to false:

    @Override
    public void start(Stage primaryStage) {
        ...       
        toolBar.setVisible(false);
        toolBar.setManaged(false);
    }
    

    EDIT

    In your case, you have also a MenuBar, and the ToolBar managed property is already bound to its visibility, so you only need to set:

    public void postInit() {
        app.setShowCloseConfirmation(false);
        Platform.runLater(() -> {
            toolBar.setVisible(false);
    
            menuBar.setVisible(false);
            menuBar.setManaged(false);
        });
    }
    

    Note: I've added Platform.runLater(): The postInit() method is called before the stage is shown, so that's a way to delay it a bit and let the controls be rendered properly before dealing with their visibility.

    In case you want to restore their visibility, this should work as well:

    public void setTopVisible() {
        toolBar.setVisible(true);
    
        menuBar.setVisible(true);
        menuBar.setManaged(true;
    }