本文介绍了JavaFX组合框与复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的描述



我尝试创建一个 JavaFX ComboBox 其在下拉菜单中包含复选框
ComboBox 应该是可编辑的,并且可以通过一个简单的类来调用 > CheckItem
CheckItems 的列表应为可检查的,并且在选择后不会关闭下拉菜单。



最后, ComboBox 中的文本应该可用,并且选择(所有选中的项目)



我已经解决了



(1)a ComboBox 渲染 CheckItem as CheckedBox 正确选择



(2)从 ComboBox



出现的问题



下拉框关闭&



(2)据我所知,它一次只能选择一个



这里是我测试这些东西的代码:



测试程序



public class ComboButtonSample extends应用程序{

@Override
public void start(Stage Stage){
final ObservableList< ; CheckItem> items = fetchItems();
ComboBox< CheckItem> combo = createComboBox(items);
combo.setPromptText(enter searchstring here);
combo.setEditable(true);


//垂直排列组件
VBox vBox = new VBox();
vBox.getChildren()。add(combo);

//用于写出组合框的文本和项目的按钮
Button btn = new Button();
btn.setText(combo text to console);
btn.setOnAction((event) - > {
System.out.println(Text is:+ combo.getEditor()。getText());
System.out。 println(Content is:);
for(Iterator< CheckItem> iterator = combo.getItems()。iterator(); iterator.hasNext();){
CheckItem ci = .next();
System.out.println(String.format([%s]%s - >%s,ci.selected?X:,ci.getDisplayName ci.getInternalName()));

}
});

vBox.getChildren()。add(btn);

//显示你不需要任何代码来改变框的选择。
CheckBox checkBox = new CheckBox();
checkBox.setText(test box);
vBox.getChildren()。add(checkBox);

stage.setScene(new Scene(vBox));
stage.show();
}

private ComboBox< CheckItem> createComboBox(ObservableList< CheckItem> data){
ComboBox< CheckItem> combo = new ComboBox<>();
combo.getItems()。addAll(data);
combo.setCellFactory(listView - > new CheckItemListCell());
return combo;
}

class CheckItemListCell extends ListCell< CheckItem> {
private final CheckBox btn;

CheckItemListCell(){
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
btn = new CheckBox();
}

@Override
protected void updateItem(CheckItem item,boolean empty){
super.updateItem(item,empty);

if(item == null || empty){
setGraphic(null);
} else {
btn.setText(item.getDisplayName());
btn.selectedProperty()。setValue(item.selected);
setGraphic(btn);
}
}
}

private ObservableList< CheckItem> fetchItems(){
final ObservableList< CheckItem> data = FXCollections
.observableArrayList();
for(int i = 1; i CheckItem chkItem = new CheckItem();
chkItem.selected = i%3 == 0;
chkItem.setDisplayName(DisplayName+ i);
chkItem.setInternalName(InternalName+ i);
data.add(chkItem);
}
返回数据;
}

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



CheckItem



  public class CheckItem {
boolean selected;
String displayName;
String internalName;

public boolean isChecked(){
return selected;
}

public void setChecked(boolean checked){
this.selected = checked;
}

public String getDisplayName(){
return displayName;
}

public void setDisplayName(String displayName){
this.displayName = displayName;
}

public String getInternalName(){
return internalName;
}

public void setInternalName(String internalName){
this.internalName = internalName;
}
}


解决方案

您在实施过程中遇到问题,您应该查看。


Description of the Question

I try to create a JavaFX ComboBox which holds CheckBoxes in the dropdown menu.The ComboBox shall be editable and gets fed by a simple Class lets call it CheckItem.the list of CheckItems shall be checkable - and shall not close the dropdown menu after a selection is made.

finally the Text in the ComboBox should be available and a Selection (all checked Items)

this is what i already worked out

(1) a ComboBox rendering the CheckItem as CheckedBox with correct selection

(2) gaining the Text from the ComboBox

problems coming up

(1) After clicking on one item the dropdown closes & selection state of the item does not change.

(2) As far as i noticed its only possible to have one item selected at a time?

here is my code for testing the stuff:

Test Program

public class ComboButtonSample extends Application {

    @Override
    public void start(Stage stage) {
            final ObservableList<CheckItem> items = fetchItems();
            ComboBox<CheckItem> combo = createComboBox(items);
            combo.setPromptText("enter searchstring here");
            combo.setEditable(true);


            // order the components vertically
            VBox vBox = new VBox();
            vBox.getChildren().add(combo);

            // Button to write out the text and the items of the combobox
            Button btn = new Button();
            btn.setText("combo text to console");
            btn.setOnAction((event) -> {
                    System.out.println("Text is: "+combo.getEditor().getText());
                    System.out.println("Content is: ");
                    for (Iterator<CheckItem> iterator = combo.getItems().iterator(); iterator.hasNext();) {
                            CheckItem ci = (CheckItem) iterator.next();
                            System.out.println(String.format("[%s] %s -> %s", ci.selected ? "X" : " ",ci.getDisplayName(), ci.getInternalName()));

                    }
            });

            vBox.getChildren().add(btn);

            // show you do not need any code to change the selection of the box.
            CheckBox checkBox = new CheckBox();
            checkBox.setText("test box");
            vBox.getChildren().add(checkBox);

            stage.setScene(new Scene(vBox));
            stage.show();
    }

    private ComboBox<CheckItem> createComboBox(ObservableList<CheckItem> data) {
            ComboBox<CheckItem> combo = new ComboBox<>();
            combo.getItems().addAll(data);
            combo.setCellFactory(listView -> new CheckItemListCell());
            return combo;
    }

    class CheckItemListCell extends ListCell<CheckItem> {
            private final CheckBox btn;

            CheckItemListCell() {
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    btn = new CheckBox();
            }

            @Override
            protected void updateItem(CheckItem item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {
                            setGraphic(null);
                    } else {
                            btn.setText(item.getDisplayName());
                            btn.selectedProperty().setValue(item.selected);
                            setGraphic(btn);
                    }
            }
    }

    private ObservableList<CheckItem> fetchItems() {
            final ObservableList<CheckItem> data = FXCollections
                            .observableArrayList();
            for (int i = 1; i < 15; i++) {
                    CheckItem chkItem = new CheckItem();
                    chkItem.selected = i%3==0;
                    chkItem.setDisplayName("DisplayName" + i);
                    chkItem.setInternalName("InternalName" + i);
                    data.add(chkItem);
            }
            return data;
    }

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

CheckItem

public class CheckItem {
    boolean selected;
    String displayName;
    String internalName;

    public boolean isChecked() {
        return selected;
    }

    public void setChecked(boolean checked) {
        this.selected = checked;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getInternalName() {
        return internalName;
    }

    public void setInternalName(String internalName) {
        this.internalName = internalName;
    }
}
解决方案

If you have problems in your implementation, you should have a look to the CheckComboBox control in the ControlsFX project.

Source code can be found here.

这篇关于JavaFX组合框与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:36