0

I have entity:

import javafx.scene.text.Text;

public class Message {
    private Text time;
    private Text sender;
    private Text message;

    public Message(Text time, Text sender, Text message) {
        this.time = time;
        this.sender = sender;
        this.message = message;
    }

    private void config() {
        /* Text config goes here */
    }

    public Text getTime() {
        return time;
    }

    public Text getSender() {
        return sender;
    }

    public Text getMessage() {
        return message;
    }
    @Override
    public String toString() {
        return "Message{" +
                "time=" + time +
                ", sender=" + sender +
                ", message=" + message +
                '}';
    }
}

How I can override display of item in my ListView

    @FXML
    private ListView<Message> messageList;

This is image of current result:enter image description here I want to see something like 3 Text objects in one list item. So I can change their background and look String text in them using getMethods(). This is what I'm trying to use but I don't know how:

messageList.setCellFactory(new Callback<ListView<Message>, ListCell<Message>>() {
            @Override
            public ListCell<Message> call(ListView<Message> param) {
                return /* ??? */;
            }
        });
Ilja Tarasovs
  • 181
  • 2
  • 13
  • 2
    work through a basic tutorial on how to implement custom cells and apply what you learned ;) – kleopatra Jun 25 '20 at 15:59
  • 3
    You need to return a new `ListCell` instance in which you override the `updateItem()` method. In that method, you can set the text or set the graphic as needed. It's not really clear what you want it to look like, so it's hard to give a complete answer (but there are many, many examples of list cell implementations on this site and others). I also recommend you use lambda expressions instead of explicit anonymous inner classes to implement the `Callback`, for better readability. – James_D Jun 25 '20 at 16:02
  • 4
    It also doesn't really make sense to use UI classes (`Text`) in your *data* class (`Message`). The data class should just have `String` data, and the list cell will create UI components to display them. – James_D Jun 25 '20 at 16:04
  • 1
    https://stackoverflow.com/questions/36657299/how-can-i-populate-a-listview-in-javafx-using-custom-objects – SedJ601 Jun 25 '20 at 17:30
  • 2
    The duplicate (first linked by Sedrick) explains how to create a custom cell with a custom view. If you need something more complex displayed than just some text then build a view and set the graphic of the cell. Other than that, I highly recommend you take James_D's comment to heart and stop using UI classes in you model. The model should know nothing about the view. – Slaw Jun 25 '20 at 22:30
  • Ty for response and valuable tips! – Ilja Tarasovs Jun 26 '20 at 12:49

0 Answers0