Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:59:19+00:00 2026-06-12T15:59:19+00:00

Working in Java FX 2.2. The Scene has a horizontal width that is fixed

  • 0

Working in Java FX 2.2. The Scene has a horizontal width that is fixed but is unknown at compile time. I want to place 2 or more buttons in a horizontal row that completely fills the horizontal space in the Scene AND that are each exactly the same width. The number of buttons changes dynamically with the program state. What program snippet will accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:59:20+00:00Added an answer on June 12, 2026 at 3:59 pm

    This code from the HBox javadoc will almost do what you want, except that “buttons themselves are different sizes based on the text contained in the button – wider text causes wider buttons”.

    HBox hbox = new HBox();
    Button button1 = new Button("Add");
    Button button2 = new Button("Remove");
    HBox.setHgrow(button1, Priority.ALWAYS);
    HBox.setHgrow(button2, Priority.ALWAYS);
    button1.setMaxWidth(Double.MAX_VALUE);
    button2.setMaxWidth(Double.MAX_VALUE);
    hbox.getChildren().addAll(button1, button2);
    

    By creating a custom layout pane based on HBox and overriding it’s layout method, you can get exactly the behaviour you describe.

    import javafx.application.Application;
    import javafx.collections.ObservableList;
    import javafx.event.*;
    import javafx.scene.*;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    // displays equal width buttons which fill a layout region's width.
    // http://stackoverflow.com/questions/12830402/javafx-2-buttons-size-fill-width-and-are-each-same-width
    public class HorizontallyTiledButtons extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) {
        final Button addButton    = new Button("Add");
        final Button removeButton = new Button("Remove");
        final Button extraButton  = new Button("The wizard of Frobozz is watching");
    
        final ButtonBar buttonBar = new ButtonBar(5, addButton, removeButton);
    
        addButton.setOnAction(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent event) {
            buttonBar.addButton(extraButton);
          }
        });
    
        removeButton.setOnAction(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent event) {
            buttonBar.removeButton(extraButton);
          }
        });
    
        VBox layout = new VBox(10);
        layout.getChildren().addAll(buttonBar);
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    
        stage.setScene(new Scene(layout));
        stage.setWidth(800);
        stage.show();
      }
    
      class ButtonBar extends HBox {
        ButtonBar(double spacing, Button... buttons) {
          super(spacing);
          getChildren().addAll(buttons);
          for (Button b: buttons) {
            HBox.setHgrow(b, Priority.ALWAYS);
            b.setMaxWidth(Double.MAX_VALUE);
          }
        }
    
        public void addButton(Button button) {
          HBox.setHgrow(button, Priority.ALWAYS);
          button.setMaxWidth(Double.MAX_VALUE);
          ObservableList<Node> buttons = getChildren();
          if (!buttons.contains(button)) {
            buttons.add(button);
          }
        }
    
        public void removeButton(Button button) {
          getChildren().remove(button);
        }
    
        @Override protected void layoutChildren() {
          double minPrefWidth = calculatePrefChildWidth();
          for (Node n: getChildren()) {
            if (n instanceof Button) {
              ((Button) n).setMinWidth(minPrefWidth);
            }
          }
          super.layoutChildren();
        }
    
        private double calculatePrefChildWidth() {
          double minPrefWidth = 0;
          for (Node n: getChildren()) {
            minPrefWidth = Math.max(minPrefWidth, n.prefWidth(-1));
          }
          return minPrefWidth;
        }
      }
    }
    

    Sample program output:
    twobuttons
    threebuttons

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working exec(java $file) command in php, but the problem is that
I've been working with Java for a long time but never have come across
I'm new to the Java scene but currently working on an assigned assessment. I'm
I am working (in Java) on a recursive image processing algorithm that recursively traverses
I have a working java client that is communicating with Google, through ProtoBuf serialized
Simply put, I had this working: java -cp .:bin:MyJar.jar path.to.My.Main but this threw an
I'm working in Java, and I have a JPanel in a JFrame. In that
I have an existing and working java web application. I want to make this
I was working on java HashMaps and found that it adds values to the
I am working on java, spring web based project where i want to integrate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.