Custom cell factory for my JfxListView returns raw objects

156 Views Asked by At

So my Custom cell factory should return two fields from the Message object, instead I get raw object in ListView Output from ListView.

There is a code responsible for formatting two Text objects which gets values from fields in Message object.

   messageJFXListView.setCellFactory(new Callback<ListView<Message>, ListCell<Message>>() {
            @Override
            public ListCell<Message> call(ListView<Message> param) {
                return new JFXListCell<Message>(){
                    @Override
                    public void updateItem(Message item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty || item == null || item.getMessage() == null) {
                            setText(null);
                        } else if(item.getNickname() == null) {
                            setText(item.getMessage()); // no formatting here yet
                        }
                        else{
                            Text nickname = new Text();
                            nickname.setStyle("-fx-fill: #424242;-fx-font-weight:bold;");
                            nickname.setText("<" + item.getNickname() + "> ");
                            Text message = new Text();
                            message.setText(item.getMessage());
                            getChildren().addAll(nickname,message);
                        }
                    }
                };
            }
        });
    }

After changing getChildren().addAll(nickname,message); to getChildren().addAll(new TextFlow(nickname,message)); it almost works but the style is not set on the nickname part and yeah raw object is still printed The output from TextFlow change.

EDIT: Instead getChildren().addAll() I've used setGraphic(new TextFlow(nickname,message)); and now formatting works. It really almost works like it should but raw object is still present

EDIT2: I've added setText(null); at the begin of last else and it clears out the raw object and things works like they should right now but I won't mark this as answer because I feel like this is some kind of hack instead of proper solution.

EDIT3: Message class, Observable list and JFXListView to make this case reproducible.

public class Message {
    private String nickname;
    private String message;

    public String getNickname() {
        return nickname;
    }

    public Message(String nickname, String message) {
        this.nickname = nickname;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
    public Message() {
    }
    public Message(String message) {
        this.message = message;
    }
    
}

public class MainWindowController   {
    @FXML
    public ObservableList<Message> messageObservableList = FXCollections.observableArrayList();
    @FXML
    public JFXListView<Message> messageJFXListView;
}
1

There are 1 best solutions below

0
Maki Nishikino On

This is a bug in JFoenix and seems like setText(null) is the only way to deal with it until they will fix it Here is a ticket