1

I'm trying to add a graphical display to my project. I have company objects, filled with arraylist of trains, filled with arraylist of wagons.

I want to display my companies vertical, and my trains/wagons horizontal like this:
[Image]Company1, [image], Train1,[image]Wagon1,[image]Wagon2
[Image]Company1, [image], Train1,[image]Wagon1,[image]Wagon2
[Image]Company1, [image], Train1,[image]Wagon1,[image]Wagon2

Currently it's all displayed in 1 giant vertical list like this:
[Image]Company1
[Image]Company1
[Image]Company1
[Image]Train1
[Image]Wagon1

I've used this stackoverflow to get here
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);
    }
}
Edo
  • 85
  • 8
  • Why not a `ListView`, since you already have a `Company` class? Then just display everything you need in the graphic for the list cell. – James_D Dec 19 '17 at 13:41
  • 1
    When you have a list of data objects, and you want to show multiple attributes of each object on each row, you want a [TableView](https://docs.oracle.com/javase/9/docs/api/javafx/scene/control/TableView.html), not a ListView. – VGR Dec 19 '17 at 14:45

0 Answers0