0

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?

José Pereda
  • 44,311
  • 7
  • 104
  • 132

1 Answers1

0

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;
}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • That did it, BUT... making that will not allow me to set it visible when a proper login to the app is created because the toolbar will not be bounded to the scene/stage/application/whatever_it_was_bounded_at. – Carlos de Luna Saenz Apr 28 '16 at 22:19
  • I've edited my answer, so you can set visible again both controls. – José Pereda Apr 28 '16 at 22:38
  • that will throw an exception, seems like the managed property is initialized After postinit...: Can not load view 'inventario.views.LoginView' java.lang.RuntimeException: ToolBar.managed : A bound value cannot be set. at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:140) at javafx.scene.Node.setManaged(Node.java:2389) at mx.gob.scjn.inventario.controllers.LoginController.postInit(LoginController.java:79) at mx.gob.scjn.inventario.views.LoginView.start(LoginView.java:16) at com.gluonhq.particle.view.ViewManager$1.invalidated(ViewManager.java:105) – Carlos de Luna Saenz Apr 29 '16 at 15:33
  • Can you post how and when do you get `toolBar` and `menuBar`? – José Pereda Apr 29 '16 at 15:38
  • spaces after @ on annotations are left to prevent the "post notifying" warning of stackoverflow public class LoginController { @ Inject ParticleApplication app; @ Inject private ViewManager viewManager; @ Inject private StateManager stateManager; @Inject private ToolBar toolBar; @ Inject private MenuBar menuBar; @ Inject private StatusBar statusBar; @ FXML private TextField txfUsuario; @ FXML private PasswordField txfPassword; – Carlos de Luna Saenz Apr 29 '16 at 15:45
  • Ok, you could edit your question and clarify it there. I'll review it later. – José Pereda Apr 29 '16 at 15:55
  • done, the question is updated to show the injection annotations – Carlos de Luna Saenz Apr 29 '16 at 16:12
  • After looking carefully my code and yours i put the: public void postInit() { app.setShowCloseConfirmation(false); Platform.runLater(() -> { toolBar.setVisible(false); menuBar.setVisible(false); // menuBar.setManaged(false); }); } And worked... Thanks a lot. – Carlos de Luna Saenz May 02 '16 at 15:12