Peter Penzov
Peter Penzov

Reputation: 1658

ArrayList displayed incorrectly

I'm testing to display Array List into JavaFX accordion:

public class Navigation {

    // Object for storing conenctions
    public static List<dataObj> list = new ArrayList<>();
    private ObservableList<dataObj> data;

    public class dataObj {

        private String conenctionname;

        public dataObj(String conenctionname) {
            this.conenctionname = conenctionname;
        }

        public String getConenctionname() {
            return conenctionname;
        }

        public void setConenctionname(String conenctionname) {
            this.conenctionname = conenctionname;
        }
    }

    public void initNavigation(Stage primaryStage, Group root, Scene scene) {

        VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = makeScrollable(stackedTitledPanes);
        scroll.getStyleClass().add("stacked-titled-panes-scroll-pane");
        scroll.setPrefSize(395, 580);
        scroll.setLayoutX(5);
        scroll.setLayoutY(32);

        root.getChildren().add(scroll);

    }

    private ScrollPane makeScrollable(final VBox node) {
        final ScrollPane scroll = new ScrollPane();
        scroll.setContent(node);
        scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>() {
            @Override
            public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) {
                node.setPrefWidth(bounds.getWidth());
            }
        });
        return scroll;
    }

    /////////////////////////////////////////////////////////////////////////////////////
    // Generate accordition with Connections, Tables and Description
    private VBox createStackedTitledPanes() {
        VBox stackedTitledPanes = new VBox();
        stackedTitledPanes.getChildren().setAll(
                createConnectionsList("Connections"));
        ((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true);
        stackedTitledPanes.getStyleClass().add("stacked-titled-panes");

        return stackedTitledPanes;
    }

    //////////////////////////////////////////////////////////////////////////////
    // Generate list with Connections
    public TitledPane createConnectionsList(String title) {

        initObject();

        data = FXCollections.observableArrayList(list);

        ListView<dataObj> lv = new ListView<>(data);

        lv.setCellFactory(new Callback<ListView<dataObj>, ListCell<dataObj>>() {
            @Override
            public ListCell<dataObj> call(ListView<dataObj> p) {
                return new ConnectionsCellFactory();
            }
        });
        AnchorPane content = new AnchorPane();
        content.getChildren().add(lv);
        // add to TitelPane
        TitledPane pane = new TitledPane(title, content);
        return pane;
    }

    static class ConnectionsCellFactory extends ListCell<dataObj> {

        @Override
        public void updateItem(dataObj item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null) {

                for (int i = 0; i < list.size(); i++) {
                    setText(list.get(i).getConenctionname());
                }



            }
        }
    }

    // Insert Some test data
    public void initObject() {

        dataObj test1 = new dataObj("test data 1");
        dataObj test2 = new dataObj("test data 2");

        list.add(test1);
        list.add(test2);

    }
}

But for some reason I cannot get proper list of Objects from the Array list and display them. I get this result:

enter image description here

The proper result should be test data 1 and test data 2. How I can fix this?

Upvotes: 0

Views: 268

Answers (1)

Joan
Joan

Reputation: 426

The problem is in the ConnectionsCellFactory, the method updateItem is called for every item in List. So in you code, for every cell you are setting the text for every item in the list

you should try:

static class ConnectionsCellFactory extends ListCell<dataObj> {

    @Override
    public void updateItem(dataObj item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            setText(item.getConenctionname());
        }
    }
}

Upvotes: 1

Related Questions