Question
My dropdown list of the CheckComboBox
expands to the size of the largest element entered in it, but I want to limit its width so it doesn't get larger than the screen width.
Library Information
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" |
--------------------------------------------
As you can notice, the item 1 text is really long in length. In this StackOverflow topic, it maintains the width and adds a new line. In JavaFX, it goes out of bounds: the width is bigger than the screen width size.
How do I fix this? How Do I limit the width to either the window width or the stage width or at least the screen width? Or even to the ComboBox
width, like in this image below:
Reproducible example
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();
}
}
References
In the topic, this issue is addressed, but with a few differences. The main difference is that, in my case, I use a CheckComboBox
from ControlsFx
instead of a 'simple' ComboBox
from JavaFX. The second difference is that CheckComboBox doesn't have a setCellFactory method.
PS: Im not recompiling a library for something simple as this. If the problem can onlu be solved by recompiling the library, then its not a solution I want. You can keep giving -1, good luck with it.