0

I'm writing a messenger in Java and FXML and I want to show all the current chats of the user on a ListView(chatBar). My chats are in an ObservableArrayList but I still can't add the values to the ListView.

public void fillChatBar() {
    ArrayList<Chat> chats = db.getAllInvolvedChats(user.getUserID());

    ObservableList<Pane> chatHolder = FXCollections.observableArrayList();

    chats.forEach(chat -> {
        Pane chatPane = new Pane();
        Label chatName = new Label();
        chatName.setText(chat.getOpponent(user.getUserID()).getUsername());
        chatPane.getChildren().add(chatName);
        chatPane.getChildren().add(new ImageView(chat.getOpponent(user.getUserID()).getProfileImage()));

        chatHolder.add(chatPane);
    });

    chatBar.setItems(chatHolder);
}

I get no error messages or exceptions. It just won't show.

Jamith NImantha
  • 1,999
  • 2
  • 20
  • 27
MrSplifff
  • 3
  • 1
  • Probably not a good idea to have an `ObservableList` of `Pane`. – SedJ601 May 22 '19 at 15:45
  • I am guessing that you should be taking an approach similar to https://stackoverflow.com/questions/34838341/javafx-custom-cell-factory-with-custom-objects. – SedJ601 May 22 '19 at 15:49
  • 3
    Nonetheless this should work. This means the issue seems to be some part of the program you haven't included in the question. You need to post a [mcve]! – fabian May 22 '19 at 15:57

1 Answers1

0

the problem is that you misunderstood the usage of ListView ;)

The items that are stored in a ListView are no cell-views but models. Internally the ListView creates cells that represents the model items and shows them on screen. You can try this by simply creating a ListView<String> and add a list of Strings to the items. You will see labels that shows the String values omg the screen. For such basic datatypes like String your ListView automatically creates cell-views to hat makes sense. For more complexe model types (like a list of custom models) you need to write your own cell-view factory (or overwrite toString() in your model class). See this tutorial as a first example to work with Strings. A more complexe example can be found here.

Hendrik Ebbers
  • 2,570
  • 19
  • 34