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"));
GridPane grid = new GridPane();
grid.add(comboBox, 0, 0);
grid.add(list, 0, 1);
grid.setVgrow(list, Priority.ALWAYS);
comboBox.setMaxWidth(Double.MAX_VALUE);
stage.setScene(new Scene(grid, 200, 300));
stage.show();
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 .