Java / JavaFx / listView:将图像列表显示为网格?

时间:2017-12-19 13:16:38

标签: java image listview javafx

我正在尝试为我的项目添加图形显示。我有公司的物品,里面装满了列车的arraylist,里面装满了货车的arraylist。

我想垂直展示我的公司,我的火车/货车横向如下:
[图片] Company1,[图片],Train1,[图片] Wagon1,[图片] Wagon2
[图片] Company1,[图片],Train1,[图片] Wagon1,[图片] Wagon2
[图片] Company1,[图片],Train1,[图片] Wagon1,[图片] Wagon2

目前它全部显示在1个巨大的垂直列表中,如下所示:
[图像]公司1
[图像]公司1
[图像]公司1
[图像] TRAIN1
[图像] Wagon1

我已经用这个stackoverflow来到这里了 JavaFx : How to put ImageView inside ListView

public class ListViewWithImages extends Application {

    private final Image IMAGE_RUBY = new Image("image.png");

    private Image[] listOfImages = { IMAGE_RUBY, IMAGE_APPLE, IMAGE_VISTA, IMAGE_TWITTER };

    @Override
    public void start(Stage primaryStage) throws Exception {
        ArrayList<String> companies = new ArrayList<String>();
        ArrayList<String> wagons = new ArrayList<String>();
        ArrayList<String> trains = new ArrayList<String>();

        ListView<String> listView = new ListView<String>();
        ObservableList<String> items = FXCollections.observableArrayList();
        listView.setItems(items);               
        }

        listView.setCellFactory(param -> new ListCell<String>() {
            private ImageView imageView = new ImageView();

            @Override
            public void updateItem(String name, boolean empty) {
                super.updateItem(name, empty);
                if (empty) {
                    setText(null);
                    setGraphic(null);
                } else {
                     if(companies.contains(name)){
                     imageView.setImage(listOfImages[0]);
                     setGraphic(imageView);
                     setText(name);
                     }
                     setText(name);
                }
            }
        });
        HBox box = new HBox(listView);
        box.setAlignment(Pos.CENTER);
        Scene scene = new Scene(box, 2000, 2000);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案