6 Replies Latest reply: May 16, 2013 1:50 PM by jsmith RSS

    Limiting the width of a toolbar

    se*328623*78
      I have an horizontal toolbar with many buttons that is making look the window where it is located too wide.

      I would like that it does not grow more than the current preferred width of the panel where it is located (I mean, the width the pannel would have without the toolbar). So the toolbar should show as many buttons as can be allocated in that width, and the arrows in the right would allow me to see the remaining buttons when required.

      I tried with:
      HBox.setHgrow(toolbarPane, Priority.NEVER);
      in both the toolbar pane and the pane where the toolbar is located, but it did not help.

      Edited by: se**** on May 14, 2013 4:16 PM
        • 1. Re: Limiting the width of a toolbar
          shakir.gusaroff
          Does using setMaxWidth() work for you?
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.*;
          import javafx.scene.layout.HBox;
          import javafx.scene.layout.StackPane;
          import javafx.stage.Stage;
          
          
          public class ToolBarExample extends Application {
          
              public static void main(String[] args) {
                  launch(args);
              }
          
              @Override
              public void start(Stage primaryStage) {
          
                  ToolBar toolBar = new ToolBar(
                          new Button("New"),
                          new Button("Open"),
                          new Button("Save"),
                          new Button("Clean"),
                          new Button("Compile"),
                          new Button("Run"),
                          new Button("Debug"),
                          new Button("Profile"));
          
                  HBox root = new HBox();
                  root.getChildren().add(toolBar);
                  primaryStage.setScene(new Scene(root, 300, 250));
                  primaryStage.show();
                  toolBar.setMaxWidth(root.getWidth());
          
              }
          }
          • 2. Re: Limiting the width of a toolbar
            jsmith
            I have an horizontal toolbar with many buttons that is making look the window where it is located too wide.
            If you set an explicit size for a Scene in the scene's constructor, the window will be created that size and not expand to the preferred size of it's content.

            http://docs.oracle.com/javafx/2/api/javafx/scene/Scene.html#Scene(javafx.scene.Parent,%20double,%20double)
            • 3. Re: Limiting the width of a toolbar
              se*328623*78
              First I tried with this:
              toolbarPane.setMaxWidth(getWidth());
              But the toolbar pane appeared very small. Then I tried with:
              toolbarPane.setPrefWidth(getWidth());
              And the toolbar is having the expected size !.
              However, if I make the window wider the toolbar does not expand so I can see the remaining buttons.

              I tried with:
              HBox.setHgrow(toolbarPane, Priority.ALWAYS);
              Just in case I also did the same with the parent of the toolbar, but it did not help.

              Edited by: se**** on May 15, 2013 1:18 AM
              • 4. Re: Limiting the width of a toolbar
                jsmith
                I'm sure I'm missing something, but why set a max width at all?
                What is wrong with =>
                @Override public void start(Stage stage) {
                  ToolBar toolBar = new ToolBar(new Button("New"), new Button("Open"), new Button("Save"), new Button("Clean"), new Button("Compile"), new Button("Run"), new Button("Debug"), new Button("Profile"));
                
                  HBox root = new HBox();
                  root.getChildren().add(toolBar);
                  HBox.setHgrow(toolBar, Priority.ALWAYS);
                
                  stage.setScene(new Scene(root, 300, 250))
                  stage.show();
                }
                If that's no good, can you post an executable sample which demonstrates the issue?
                • 5. Re: Limiting the width of a toolbar
                  se*328623*78
                  Thanks for answering jsmith.

                  I am trying to avoid given a fixed size to my scene.
                  The rationale is that I think I should focus on the right size of the scene components and ideally the scene size should adapt to them. In this way, I do not have to keep synchronized the appropriate size of the components with any hardcoded given size of the scene. Is this not a good motivation ?

                  However, a toolbar is a special component. I mean, it is perfectly reasonable that it is bigger (wider, if it is a horizontal tool bar) than the space used by the rest of the components. Ideally, I would like to keep its size limited to the preferred size (width) of its parent component (without taking into consideration the toolbar), and at the same time the toolbar should grow if the window gets bigger.
                  If this is not possible or too difficult, I would have to set a fix size for the scene as you say.

                  Edited by: se**** on May 16, 2013 10:33 AM
                  • 6. Re: Limiting the width of a toolbar
                    jsmith
                    Set the preferred width of the toolbar to a pretty small value.

                    In the example below the stage ends up initially sized to the preferred width of the textArea as the preferred width of the textArea is larger than the preferred width of the toolbar and the resizable VBox root pane calculates it's width based on it's child with the largest preferred width.
                    @Override public void start(Stage stage) {
                      ToolBar toolBar = new ToolBar(new Button("New"), new Button("Open"), new Button("Save"), new Button("Clean"), new Button("Compile"), new Button("Run"), new Button("Debug"), new Button("Profile"), new Button("Navigate"), new Button("Team"), new Button("Source"), new Button("Help"), new Button("Tools"), new Button("File"), new Button("Edit"), new Button("View"));
                      TextArea textArea = new TextArea();
                      toolBar.setPrefWidth(100);
                    
                      VBox root = new VBox();
                      root.getChildren().setAll(
                        toolBar,
                        textArea
                      );
                      VBox.setVgrow(textArea, Priority.ALWAYS);
                    
                      stage.setScene(new Scene(root));
                      stage.show();
                    }
                    This works because you actually do prefer the toolbar to be smaller than the required width to display all it's buttons.

                    Because the preferred width is small, if there is nothing else in the scene, then the scene will size to the preferred size of the toolbar, which has been explicitly set in the above code sample to just enough to show a button and the default toolbar overflow handler which shows the rest of the buttons on the toolbar.

                    If there are other things on the scene and their preferred width is greater than the preferred width of the toolbar, then the toolbar window will expand just to fit the preferred width of the other controls and (as you place the toolbar in a resizable pane, and toolbar is itself resizable and no max width is set on the toolbar) the toolbar will expand to just fill the remaining visible width of the window and any tools in the toolbar which don't fit will be accessible by the default toolbar overflow handler. If you had set a maxWidth on the toolbar, you wouldn't get an auto expanding behaviour on the toolbar to fill available area beyond the max width.