Strange control width in JavaFX 2?

I'm new to JavaFX 2 and am confused about the default widths for ComboBox and ListView. I would expect the ComboBox to be wider, so that the text "tooltip" will be fully visible, since the ListView I expect it to be smaller, only as wide as necessary. Code and screenshot below.

1) How can I achieve that ComboBox and Listview are sized to the required width?

2) How to make both controls the same width (hence the maximum of both widths), is it possible to create some kind of connection between the widths of both? I am especially looking for an elegant solution for FXML.

Thanks for any hint!

@Override
public void start(Stage primaryStage) {
    ComboBox<String> comboBox = new ComboBox<String>();
    comboBox.setPromptText("the prompt text");
    ObservableList<String> items = FXCollections.observableArrayList();
    items.addAll("item 1", "item 2");
    comboBox.setItems(items);

    ListView<String> list = new ListView<String>();
    ObservableList<String> items2 =FXCollections.observableArrayList (
        "blue", "red");
    list.setItems(items2);

    VBox vBox = new VBox();
    vBox.getChildren().addAll(comboBox, list);

    primaryStage.setScene(new Scene(vBox, 200, 300));
    primaryStage.show();
}

enter image description here

+5
source share
2 answers

2) How to make both controls the same width (hence the maximum of both widths), is it possible to create some kind of connection between the widths of both? I am especially looking for an elegant solution for FXML.

, ListView maxWidth , ComboBox maxWidth, . , - ( - VBox), maxWidth comboBox.

:

comboBox.setMaxWidth(Double.MAX_VALUE);

FXML ComboBox:

maxWidth="Infinity"

John Grey, , .

1) , ComboBox Listview ?

, , combobox listview , , . , , , JavaFX .

.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.*;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class GridControlWidths extends Application {
  public static void main(String[] args) throws Exception { launch(args); }
  @Override public void start(Stage stage) {
    ComboBox<String> comboBox = new ComboBox<String>(FXCollections.observableArrayList("item 1", "item 2"));
    comboBox.setPromptText("the prompt text");

    ListView<String> list = new ListView<String>(FXCollections.observableArrayList ("blue", "red"));

    // layout the controls in a grid with appropriate constraints.
    GridPane grid = new GridPane();
    grid.add(comboBox, 0, 0);
    grid.add(list, 0, 1);
    grid.setVgrow(list, Priority.ALWAYS);  // make the list grow vertically as much as possible

    // unclamp the max width of the combo box.
    comboBox.setMaxWidth(Double.MAX_VALUE);

    // display the scene.
    stage.setScene(new Scene(grid, 200, 300));
    stage.show();

    // set the prefWidth of the listview based on the list cells (allowing 8 pixels for padding of the cells).
    double maxWidth = 0;
    for (Node n: list.lookupAll(".text")) {
      maxWidth = Math.max(maxWidth, n.getBoundsInParent().getWidth() + 8);
    }
    list.setPrefWidth(maxWidth);
  }
}

FWIW, , , , , VBox, ListView ChoiceBox, ComboBox, , . ListView , , , , , , , , . ListView prefWidth listView listView, , .

, , , , () ​​JavaFX, ComboBox, ComboBox ComboBox. ComboBox , JavaFX, , , ComboBox .

+8

1) minWidth(x), prefWidth(x), maxWidth(x). , , .

2) void bind(ObservableValue<? extends Number> observable); . . :

someProperty.bind(otherProperty);

, .

0

All Articles