0

我有这个显示图像的 JavaFX 手风琴:

公共类导航{

private static final Image BLUE_FISH = new Image("/Blue-Fish-icon.png");
private static final Image RED_FISH = new Image("/Red-Fish-icon.png");
private static final Image YELLOW_FISH = new Image("/Yellow-Fish-icon.png");
private static final Image GREEN_FISH = new Image("/Green-Fish-icon.png");

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);

    //scene = new Scene(scroll);
    root.getChildren().add(scroll);

}

private VBox createStackedTitledPanes() {
    final VBox stackedTitledPanes = new VBox();
    stackedTitledPanes.getChildren().setAll(
            createTitledPane("Connections", GREEN_FISH),
            createTitledPane("Tables", YELLOW_FISH),
            createTitledPane("Description", RED_FISH),
            createTitledPane("Blue Fish", BLUE_FISH));
    ((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true);
    stackedTitledPanes.getStyleClass().add("stacked-titled-panes");

    return stackedTitledPanes;
}

public TitledPane createTitledPane(String title, Image... images) {
    FlowPane content = new FlowPane();
    for (Image image : images) {
        ImageView imageView = new ImageView(image);
        content.getChildren().add(imageView);

        FlowPane.setMargin(imageView, new Insets(10));
    }
    content.setAlignment(Pos.TOP_CENTER);

    TitledPane pane = new TitledPane(title, content);
    pane.getStyleClass().add("stacked-titled-pane");
    pane.setExpanded(false);

    return pane;
}

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;
}

}

我感兴趣的是可以在放置图像的位置显示数据行。像这样的东西:

在此处输入图像描述

PS案例示例。我有一个将用作列表的 java 对象:

public List<dataObj> list = new ArrayList<>();

    public class dataObj {

        private int connectionId;
        private String conenctionname;
        private String connectionDescription;

        public dataObj() {
        }

        ....................
    }

当我在 Java Array 列表中插入一些数据时,我想根据上述要求将其显示到手风琴中。

PS 2 在我的情况下,将文本插入 FlowPane 的正确方法是什么?我测试了这个:

public TitledPane createTitledPane(String title, Image... images) {
        FlowPane content = new FlowPane();
        for (Image image : images) {
            ImageView imageView = new ImageView(image);
            content.getChildren().add(imageView);

            FlowPane.setMargin(imageView, new Insets(10));
        }
        content.setAlignment(Pos.TOP_CENTER);
        content.setText("This part will be the first line.\n This part the second.");

        TitledPane pane = new TitledPane(title, content);
        pane.getStyleClass().add("stacked-titled-pane");
        pane.setExpanded(false);

        return pane;
    }

我收到插入文本使用setText不正确的错误。什么是正确的方法?

4

2 回答 2

1

如果使用"\n"输出 String 将被分隔成多行文本。例如:

component.setText("This part will be the first line.\n This part the second.");

从您的更新中,假设您有getters并且setters

component.setText(String.valueOf(dataObj.getConnectionId()) + "\n" + dataObj.getConnectionname() + "\n" + dataObj.getConnectionDescription());
于 2013-04-23T13:22:51.250 回答
1

您可以简单地使用 ListView:

private void hello() {
    ListView<Object> lv = new ListView<>();
    // yourList is you List<Object> list
    lv.itemsProperty().set(yourList);
    lv.setCellFactory(new Callback<ListView<Object>, ListCell<Object>>() {
        @Override
        public ListCell<Object> call(ListView<Object> p) {
            return new youCellFactory();
        }
    });
    AnchorPane content = new AnchorPane();
    content.getChildren().add(lv);
    // add to TitelPane
    TitledPane pane = new TitledPane(title, content);
}
static class youCellFactory extends ListCell<Object> {
    @Override
    public void updateItem(Object item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            setText(item.getConenctionname());
        }
    }
}

我没有测试过这段代码,但它应该可以工作。

这也是一个不错的示例,但没有对象:
ListViewSample.java

于 2013-04-23T14:14:09.993 回答