Я попробовал следующий код, но он не сработал:
Код: Выделить всё
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
ObservableList items = FXCollections.observableArrayList(
"Short item",
"A bit longer item",
"This is a much longer item that needs more space"
);
ListView listView = new ListView(items);
ListCell tempCell = new ListCell();
double maxWidth = 0;
for (String item : items) {
tempCell.setText(item);
double cellWidth = tempCell.prefWidth(-1);
if (cellWidth > maxWidth) {
maxWidth = cellWidth;
}
}
listView.setPrefWidth(maxWidth);
var tabPane = new TabPane(new Tab("Test"));
HBox.setHgrow(tabPane, Priority.ALWAYS);
var root = new HBox(listView, tabPane);
Scene scene = new Scene(root, 800, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("ListView Width Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... idest-cell