0

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?

4

1 回答 1

1

问题出在,为 List 中的每个项目调用ConnectionsCellFactory该方法。updateItem因此,在您的代码中,对于每个单元格,您都在为列表中的每个项目设置文本

你应该试试:

static class ConnectionsCellFactory extends ListCell<dataObj> {

    @Override
    public void updateItem(dataObj item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            setText(item.getConenctionname());
        }
    }
}
于 2013-04-24T13:16:45.453 回答