簡體   English   中英

如何在 JavaFX 中的 org.controlsfx.control.CheckComboBox 上設置下拉菜單的寬度

[英]How to set Width of dropdown on a org.​controlsfx.​control.CheckComboBox in JavaFX

問題

我的CheckComboBox下拉列表擴展到其中輸入的最大元素的大小,但我想限制它的寬度,使其不會大於屏幕寬度。


圖書館信息

org.controlsfx.control.CheckComboBox

---------------------
| CheckComboBox | V |
--------------------------------------------
| "Long item 1 Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. (Out of screen bounds)|
--------------------------------------------
| "Long item 2"                              |
 --------------------------------------------
| "Long item 3"                              |
 --------------------------------------------

如您所見,第 1 項文本的長度確實很長。 在這個 StackOverflow 主題中,它保持寬度並添加一個新行。 在 JavaFX 中,它超出了范圍:寬度大於屏幕寬度尺寸。

我該如何解決? 如何將寬度限制為 window 寬度或舞台寬度或至少屏幕寬度? 甚至到ComboBox寬度,如下圖所示:

“CheckComboBox Dropdown”與“CheckComboBox”的寬度相同

可重現的例子

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.controlsfx.control.CheckComboBox;

public class Main2 extends Application {

    @Override
    public void start(Stage stage) {

        Pane root = new Pane();

        CheckComboBox<String> box = new CheckComboBox<>();
        double width = 200;
        box.setMinWidth(width);
        box.setPrefWidth(width);
        box.setMaxWidth(width);

        String item1 = new String("short name");
        String item2 = new String("Pellentesque habitant morbi tristique senectus et netus et " +
                "malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, " +
                "ultricies eget, tempor sit amet, ante.Pellentesque habitant morbi tristique senectus et netus et " +
                "malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, " +
                "ultricies eget, tempor sit amet, ante.");
        String item3 = new String("short name again");

        box.getItems().setAll(item1, item2, item3);
        root.getChildren().add(box);

        stage.setScene(new Scene(root, 300, 50));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

參考

主題中,這個問題得到了解決,但有一些不同之處。 主要區別在於,就我而言,我使用ControlsFxCheckComboBox而不是 JavaFX 的“簡單” ComboBox 第二個區別是 CheckComboBox 沒有 setCellFactory 方法。

PS:我不會為這樣簡單的東西重新編譯庫。 如果問題只能通過重新編譯庫來解決,那么它不是我想要的解決方案。 你可以繼續給-1,祝你好運。

必須有更好的方法,但也許您的項目只需覆蓋項目toString()方法(“快速而骯臟”)就足夠了?:這是一個小例子,我的意思是:

應用程序:

package test;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.controlsfx.control.CheckComboBox;


public class App extends Application {

    @Override
    public void start(Stage stage) {

        Pane root = new Pane();

        CheckComboBox<Item> box = new CheckComboBox<>();
        double width = 200;
        box.setMinWidth(width);
        box.setPrefWidth(width);
        box.setMaxWidth(width);

        Item item1 = new Item("short name");
        Item item2 = new Item("Pellentesque habitant morbi tristique senectus et netus et " +
                "malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, " +
                "ultricies eget, tempor sit amet, ante.");
        Item item3 = new Item("short name again");

        box.getItems().setAll(item1, item2, item3);
        root.getChildren().add(box);

        stage.setScene(new Scene(root, 300, 50));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

項目 class:

package test;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Item {

    private final StringProperty name = new SimpleStringProperty("");

    public Item(String name) {
        this.name.set(name);
    }

    @Override
    public String toString() {
        // Show max 20 characters:
        int maxLength = 20;
        if (getName().length() > maxLength)
            return getName().substring(0, maxLength - 3).concat("...");
        return getName();
    }

    public String getName() {
        return name.get();
    }

    public StringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }
}

預習:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM