JavaFx ListView的单元格高度不适合内容

时间:2018-06-15 11:58:45

标签: javafx-2

我使用javafx制作聊天应用。 一切都很好,但当聊天消息添加到ListView中时, 像这样:

listVew.getItems.add(chatMessage);

ListView的单元格高度不适合每个单元格的内容。 像这样的图片: enter image description here

但如果我滚动ListView的滚动条,那么所有单元格会立即自动刷新到正确的高度? 只是不知道为什么。请帮帮我。

代码如下:

public class ChatCell  extends ListCell<ChatMessage>{
ListView<ChatMessage> listview;



public ChatCell(ListView<ChatMessage> listview) {
    this.listview = listview;
}

@Override
public void updateItem(ChatMessage item, boolean empty) 
{
    super.updateItem(item, empty);
    // Format text
    if (item == null || empty) {
            //setPrefHeight(45.0);
          setGraphic(null);
          setText(null);
    } 
    else 
    {
        **ChatBox chatBox=new ChatBox(item);**

         setText(null);
         setGraphic(chatBox);
    }

}
}

Real cell containner:

public class ChatBox extends HBox{
private Color DEFAULT_SENDER_COLOR = Color.GOLD;
private Color DEFAULT_RECEIVER_COLOR = Color.LIMEGREEN;
private Background DEFAULT_SENDER_BACKGROUND, DEFAULT_RECEIVER_BACKGROUND;

private ChatMessage message;
private ChatBotDirection direction;

private Label nameText;
private Label displayedText;
private SVGPath directionIndicator;

public ChatBox(ChatMessage message){
    this.message = message;
    if(message.getType().equalsIgnoreCase("L")) {
        this.direction=ChatBotDirection.LEFT;
    }else if(message.getType().equalsIgnoreCase("R")) {
        this.direction=ChatBotDirection.RIGHT;
    }else {
        this.direction=ChatBotDirection.LEFT;
    }
    initialiseDefaults();
    setupElements();
}

private void initialiseDefaults(){
    DEFAULT_SENDER_BACKGROUND = new Background(new BackgroundFill(DEFAULT_SENDER_COLOR, new CornerRadii(5,0,5,5,false), Insets.EMPTY));
    DEFAULT_RECEIVER_BACKGROUND = new Background(
            new BackgroundFill(DEFAULT_RECEIVER_COLOR, new CornerRadii(0,5,5,5,false), Insets.EMPTY));
}

private void setupElements(){
    nameText=new Label(message.getUser());
    nameText.setMinWidth(50);

    nameText.setPadding(new Insets(5));
    displayedText = new Label(message.getValue());
    displayedText.setPadding(new Insets(5));
    displayedText.setWrapText(true);
    directionIndicator = new SVGPath();

    if(direction == ChatBotDirection.LEFT){

        configureForLeft();           
    }
    else{
        configureForRight();
    }
}
private void configureForLeft(){
    displayedText.setBackground(DEFAULT_RECEIVER_BACKGROUND);
    displayedText.setAlignment(Pos.CENTER_LEFT);
    directionIndicator.setContent("M0 0 L10 0 L10 10 Z");
    directionIndicator.setFill(DEFAULT_RECEIVER_COLOR);

    HBox container = new HBox(nameText,directionIndicator, displayedText);
    //Use at most 75% of the width provided to the SpeechBox for displaying the message
    container.maxWidthProperty().bind(widthProperty().multiply(0.95));

    getChildren().setAll(container);
    setAlignment(Pos.CENTER_LEFT);
}

private void configureForRight(){
    displayedText.setBackground(DEFAULT_SENDER_BACKGROUND);
    displayedText.setAlignment(Pos.CENTER_RIGHT);
    directionIndicator.setContent("M10 0 L0 10 L0 0 Z");
    directionIndicator.setFill(DEFAULT_SENDER_COLOR);

    HBox container = new HBox(displayedText, directionIndicator,nameText);
    //Use at most 75% of the width provided to the SpeechBox for displaying the message
    container.maxWidthProperty().bind(widthProperty().multiply(0.95));

    getChildren().setAll(container);
    setAlignment(Pos.CENTER_RIGHT);
}

新发现细胞的高度取决于字符数? enter image description here

0 个答案:

没有答案