How to set width of combobox in java fx

My combo box drop-down list expands to the size of the largest element entered into it, but I want it to have a fixed size.

---------------------
| Small Combobox | V |
--------------------------------------------
| "Long item 1"                              |
--------------------------------------------
| "Long item 2"                              |
 --------------------------------------------
| "Long item 3"                              |
 --------------------------------------------
+4
source share
3 answers

Use CSS

/** file: combo-size.css */

/** Size the combo-box button. */
.combo-box {
    -fx-pref-width: 100;
}

/** Size the combo-box drop down list. */
.combo-box-popup > .list-view {
    -fx-pref-width: 100;
}

Note. I tested this example only in Java 8, and I believe that attributes -fx-*-widthand -fx-*-heightcss may be new to JavaFX 8.

Usage example

In this example, both the list and drop-down buttons were designed for the same preferred width (100 pixels).

enter image description here

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class SizedComboBoxSampleWithCss extends Application {
    public static void main(String[] args) { launch(args); }

    @Override public void start(Stage stage) {
        final ComboBox<String> combo = new ComboBox<>();

        combo.setValue(Font.getDefault().getFamily());
        combo.getItems().setAll(Font.getFamilies());
        combo.getStylesheets().add(
                getClass().getResource(
                        "combo-size.css"
                ).toExternalForm()
        );

        StackPane layout = new StackPane(combo);
        layout.setPadding(new Insets(10));

        stage.setScene(new Scene(layout));
        stage.show();
    }
}
+11
source

(jewelsea) , , - . factory, ListView maxWidth.

combo.setCellFactory(new Callback<ListView<T>, ListCell<T>>() {

        @Override
        public ListCell<T> call(ListView<T> param) {
            ListCell cell = new ListCell<T>() {
                @Override
                public void updateItem(T item, boolean empty) {
                    super.updateItem(item, empty);

                    getListView().setMaxWidth(LIST_VIEW_MAX_HEIGHT);
                    if (!empty) {
                        setText(converter.toString(item));
                    } else {
                        setText(null);
                    }
                }
            };
            return cell;
        }
    });
+4
combo.setCellFactory(new Callback<ListView<T>, ListCell<T>>() {

    @Override
    public ListCell<T> call(ListView<T> param) {
        ListCell cell = new ListCell<T>() {
            @Override
            public void updateItem(T item, boolean empty) {
                if (!empty) {
                    Label label = new Label(converter.toString(item));
                    label.setMaxWidth(comboBox.getWidth());
                    label.setWrapText(true);
                    setGraphic(label);
                    setText(null);

                } else {
                    setGraphic(null);
                    setText(null);
                }
            }
        };
        return cell;
    }
});

. , .

: getChildren() if (node instanceof Label) {} , ,

0

Source: https://habr.com/ru/post/1530175/


All Articles