如何在组合框中组合字符串和 SVG?
How to combine Strings and SVG within a Combobox?
我必须知道如何在组合框中组合字符串和 SVG。
例如,SVG 应该位于字符串的末尾,以显示字符串是否已在某处设置。
所以已经出现的项目应该是这样的:
"This string already appears somewhere else" + mySVG.
如果字符串没有出现在其他地方,则项目值就是字符串本身。
显示字符串没问题,我的问题从添加 SVG 开始。
我的 SVG 是这样的:
SVGPath mySVG = new SVGPath();
mySVG.setContent("M0,4.2l1.4,-0.3L3,5.7A13,13 0 0,1 7.4,0H8.5A15,15 0 0,0 4,7.2L2.5,7.5Z");
我希望你明白我想做什么。
您可以通过将细胞工厂设置为 ComboBox
:
public class JavaFxTest2 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("apple", "banana", "orange");
comboBox.setCellFactory(l -> new ListCell<String>() {
private SVGPath mySVG;
{
setContentDisplay(ContentDisplay.RIGHT);
mySVG = new SVGPath();
mySVG.setContent("M0,4.2l1.4,-0.3L3,5.7A13,13 0 0,1 7.4,0H8.5A15,15 0 0,0 4,7.2L2.5,7.5Z");
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
if ("banana".equals(item)) {
setGraphic(mySVG);
} else {
setGraphic(null);
}
setText(item);
} else {
setGraphic(null);
setText(null);
}
}
});
primaryStage.setScene(new Scene(comboBox));
primaryStage.show();
}
public static final void main(String[] args) {
launch(args);
}
}
我必须知道如何在组合框中组合字符串和 SVG。
例如,SVG 应该位于字符串的末尾,以显示字符串是否已在某处设置。
所以已经出现的项目应该是这样的:
"This string already appears somewhere else" + mySVG.
如果字符串没有出现在其他地方,则项目值就是字符串本身。
显示字符串没问题,我的问题从添加 SVG 开始。
我的 SVG 是这样的:
SVGPath mySVG = new SVGPath();
mySVG.setContent("M0,4.2l1.4,-0.3L3,5.7A13,13 0 0,1 7.4,0H8.5A15,15 0 0,0 4,7.2L2.5,7.5Z");
我希望你明白我想做什么。
您可以通过将细胞工厂设置为 ComboBox
:
public class JavaFxTest2 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ComboBox<String> comboBox = new ComboBox<>();
comboBox.getItems().addAll("apple", "banana", "orange");
comboBox.setCellFactory(l -> new ListCell<String>() {
private SVGPath mySVG;
{
setContentDisplay(ContentDisplay.RIGHT);
mySVG = new SVGPath();
mySVG.setContent("M0,4.2l1.4,-0.3L3,5.7A13,13 0 0,1 7.4,0H8.5A15,15 0 0,0 4,7.2L2.5,7.5Z");
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
if ("banana".equals(item)) {
setGraphic(mySVG);
} else {
setGraphic(null);
}
setText(item);
} else {
setGraphic(null);
setText(null);
}
}
});
primaryStage.setScene(new Scene(comboBox));
primaryStage.show();
}
public static final void main(String[] args) {
launch(args);
}
}