2

I'm learning API JavaFX for an application I'm working on at the moment and I am trying to use the CheckComboBox from ControlsFX.

I've made a test in order to resolve a problem about items display refresh when I add items in the ObservableList which populates the CheckComboBox.

import org.controlsfx.control.CheckComboBox;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class Testin extends Application {
    private Stage main;
    private FlowPane root;
    private ObservableList<String> l;

    public Testin(){
        root = new FlowPane();
        l = FXCollections.observableArrayList();
        l.add("bla");
        l.add("shablagoo");
        l.add("tirelipimpon");
    }

    @Override
    public void start(Stage primaryStage) {
        main = primaryStage;
        CheckComboBox<String> test = new CheckComboBox<>();
        test.getItems().addAll(l);
        Button btn = new Button("test");
        btn.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event){
                l.add("ok");
                System.out.println("ok");
            }
        });
        root.getChildren().add(test);
        root.getChildren().add(btn);
        Scene scene = new Scene(root);
        main.setScene(scene);
        main.show();

    }

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

Like a classic ComboBox, when an item is added to the list, it would refresh itself automatically but it's not the case. Do I have to create a ListChangeListener which, at each modification report this on the displayed ComboBox list, or is my code wrong ?

Additional information: I also try this test with a ComboBox, replacing the CheckComboBox and that runs well.

Robert
  • 1,286
  • 1
  • 17
  • 37

1 Answers1

1
test.getItems().addAll(someList);

copies all the elements of someList to the combo box's items list. Clearly, subsequent changes to someList won't have any effect on the combo box.

You either want to replace

test.getItems().addAll(l);

with

test.setItems(l);

or you want to replace

l.add("ok");

with

test.getItems().add("ok");
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks for your answer, but there is a problem with the CheckComboBox. It doesn't have the method .setItems() instead of basic ComboBox. In fact, in my test, the button add and updates the initial list, but as the combobox items list doesn't point to this, it cannot be updated, right ? – Mstr Jordison Jul 13 '15 at 20:21
  • So use the second solution: replace `l.add("ok");` with `test.getItems().add("ok");`. – James_D Jul 13 '15 at 20:53