Rajesh Kumar Dash
Rajesh Kumar Dash

Reputation: 2277

How to grow progress bar horizontally in javafx?

I have an application where I have a table and ProgressBar. I want to expand my progress bar when user resizes the window. For TextBox in JavaFX I can able to set HBox priority and achieve it as intended. But for ProgressBar it is not working.

Can any one tell me where I am doing wrong ?

HBox root = new HBox();           
final ProgressBar browser = new ProgressBar();

//  final WebEngine webEngine = browser.getEngine();
// browser.setText("JJJ");
HBox.setHgrow(browser, Priority.ALWAYS);         
//  webEngine.loadContent("<b>asdf</b>");              
root.getChildren().add(browser);
scene.setRoot(root);

Upvotes: 8

Views: 9494

Answers (2)

user458577
user458577

Reputation:

The best way is to set the maximum width of the progress bar.

browser.setMaxWidth(Double.MAX_VALUE);

and of course to tell the VBox to fill its children up to the full width (which is the default, so maybe not worth mentioning).

vbox.setFillWidth(true);

The answer given by Uluk might work in simple examples. However, it might lead to an uncontrolled growth of the parent, if the parent in response to a child changing its width decides to change its own width as well. Even in not overly complex layout such behavior can readily be observed.

Upvotes: 15

Uluk Biy
Uluk Biy

Reputation: 49215

Bind the widths of progress bar and Vbox:

VBox root = new VBox();
root.setAlignment(Pos.TOP_CENTER);
final ProgressBar browser = new ProgressBar();
browser.prefWidthProperty().bind(root.widthProperty().subtract(20));  //  -20 is for 
   // padding from right and left, since we aligned it to TOP_CENTER.
root.getChildren().add(browser);

Upvotes: 14

Related Questions