【问题标题】:How to set Width of dropdown on a org.​controlsfx.​control.CheckComboBox in JavaFX如何在 JavaFX 中的 org.​controlsfx.​control.CheckComboBox 上设置下拉菜单的宽度
【发布时间】:2020-07-28 05:51:52
【问题描述】:

问题

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 中,它超出了范围:宽度大于屏幕宽度大小。

我该如何解决这个问题?如何将宽度限制为窗口宽度或舞台宽度或至少屏幕宽度?甚至到ComboBox 宽度,如下图所示:

可重现的例子

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();
    }
}

参考文献

在the topic 中解决了这个问题,但有一些不同之处。主要区别在于,就我而言,我使用来自ControlsFx 的CheckComboBox,而不是来自JavaFX 的“简单”ComboBox。第二个区别是 CheckComboBox 没有 setCellFactory 方法。

PS:我不会为这样简单的东西重新编译库。如果问题只能通过重新编译库来解决,那么它不是我想要的解决方案。你可以继续给 -1,祝你好运。

【问题讨论】:

  • 干净的解决方案是一种自定义皮肤,可独立于按钮/文本字段的大小要求调整下拉菜单的大小。除此之外,您可能(或者不,不记得这是否真的可能)使用 onShown 处理程序来根据需要调整 dropDown 的大小(onShowing 会更好,但很可能为时过早)
  • 无论如何,它是开源的(这意味着您确实拥有源代码)- 找到调整大小的位置,更改为您自己的要求并重新编译..
  • 也许 CheckComboBoxSkin.java 中的 line 101 必须更改?!
  • @anko 我认为你是对的。我要尝试一下,我认为如果可行,您应该将其发布为解决方案,以便我可以适当地奖励您的回答:)
  • 我无法重新编译controlsfx-master,所以我无法对其进行测试。如果你设法让它工作,那么请自己写一个答案作为我的奖励。 :P

标签: javafx controlsfx


【解决方案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();
    }
}

物品类别:

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);
    }
}

预览:

【讨论】:

  • 不,因为应用程序原因覆盖 toString 总是错误的 ;)
  • 一个不错的选择,因为它不是;)这样做(并依赖它)会以多种方式破坏视图,f.i.:在不同的控件中显示,更改节点宽度,更改本地化,显示另一个属性,.. 重复:即使是快速而肮脏的方法也是完全错误的。
猜你喜欢
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 2021-04-06
  • 2017-05-11
  • 1970-01-01
相关资源
最近更新 更多