Java 如何将行显示为手风琴

Java 如何将行显示为手风琴,java,javafx-2,javafx,Java,Javafx 2,Javafx,我有一个显示图像的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"); pr

我有一个显示图像的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;
}
使用
setText
插入文本时出错。正确的方法是什么?

如果使用
“\n”
则输出字符串将被分成多行文本。 例如:

component.setText("This part will be the first line.\n This part the second.");
从更新中,假设您有
getter
setter

component.setText(String.valueOf(dataObj.getConnectionId()) + "\n" + dataObj.getConnectionname() + "\n" + dataObj.getConnectionDescription());

您只需使用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());
        }
    }
}
private void hello(){
ListView lv=新建ListView();
//你的名单就是你的名单
lv.itemsProperty().set(yourList);
lv.setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView p){
返回新的youCellFactory();
}
});
锚烷含量=新锚烷();
content.getChildren().add(lv);
//添加到TitelPane中
标题窗格=新标题窗格(标题、内容);
}
静态类youCellFactory扩展了ListCell{
@凌驾
public void updateItem(对象项,布尔值为空){
super.updateItem(项,空);
如果(项!=null){
setText(item.getConenctionname());
}
}
}
我还没有测试过这段代码,但它应该可以工作

这也是一个很好的例子,但没有对象:

是的,但在我的例子中,我想用数据显示对象。我不明白你的意思,这有什么不同?为什么不使用listview?@Kalaschni请举个例子?我是JavaFX新手。在我的例子中,我使用的是作为对象返回的
标题窗格
。我如何将我的代码与你的例子结合起来?我能问你如何才能理解我的意思吗操作对象
列表
,并插入一些可以在手风琴中看到的数据。您已就此打开了另一个问题:
component.setText(String.valueOf(dataObj.getConnectionId()) + "\n" + dataObj.getConnectionname() + "\n" + dataObj.getConnectionDescription());
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());
        }
    }
}