如何将行显示为手风琴

时间:2013-04-23 13:19:25

标签: 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");
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;
}

}

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

enter image description here

P.S案例。我有一个将用作列表的java对象:

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

    public class dataObj {

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

        public dataObj() {
        }

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

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

P.S 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插入文字不正确的错误。什么是正确的方法?

2 个答案:

答案 0 :(得分:1)

如果使用"\n",输出字符串将分成多行文本。 例如:

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

在您的更新中,假设您有getterssetters

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

答案 1 :(得分: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