0

I'm creating a chatting application in javafx. I'm displaying messages using labels and adding them in arraylist. Now i want to remove labels what should i do so that labels will be destroyed from scrollpane.

Here is code:

CONNECTION clientConnection=null;

@FXML ScrollPane chatScrollPane;
VBox chatVBox;
@FXML AnchorPane chatAnchorPane;


private List<Label> messages = new ArrayList<>();

int index = 0;

I'm adding messages like:

messages.add(new Label(m));

Should i assign new memory to messages so that garbage collector will remove labels from it?

eg?

 public void clearall(){
    /*for(int i=0;i<index;i++){

    }*/
    messages = null;
    System.gc();


    messages = new ArrayList<>();

    index = 0;

    System.out.println("cleared chat");
}
Nouman Arshad
  • 101
  • 1
  • 2
  • 10

1 Answers1

4

List has a clear() method, which will clear it.

Don't call System.gc(), that is almost never needed.

You don't show how you actually display the labels and how they actually get into the scroll pane, so I don't know if clearing the array list will actually clear the messages from the UI. If you have further questions or issues, provide an mcve.

jewelsea
  • 150,031
  • 14
  • 366
  • 406