I'm currently writing a GUI using the Lanterna library. Currently, I'm developing a GUI which must contain several components that display info of a given type Note. The code renders the GUI, but, the removeNoteFromList method doesn't seem to be working. I'm currently trying to use the invalidate method for the panels that might need to be redrawn, but nothing is really happening. Can I get some advice on how to solve it?
Here's the Code responsible for the drawing.
EDIT: I updated the code a little bit. Now the user interface gets updated, but after the update, the GUI just freezes, and I cannot move the cursor or do anything else inside it
package com.github.gtvb.stns.application.gui.notesList;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import com.github.gtvb.stns.domain.model.Note;
import com.googlecode.lanterna.gui2.BasicWindow;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.Direction;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.Label;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.TextBox;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
public class NotesListWindow extends BasicWindow {
private ArrayList<Note> notes;
private Panel notesPanel;
public NotesListWindow(ArrayList<Note> notes) {
this.notes = notes;
Panel mainPanel = new Panel();
mainPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
notesPanel = new Panel();
notesPanel.setLayoutManager(new GridLayout(2).setHorizontalSpacing(5));
refreshNotesPanel();
mainPanel.addComponent(notesPanel);
setComponent(mainPanel);
}
private void refreshNotesPanel() {
notesPanel.removeAllComponents(); // Clear the panel
for (Note note : notes) {
Panel notePanel = createNotePanel(note);
notesPanel.addComponent(notePanel);
}
}
private Panel createNotePanel(Note note) {
Panel panel = new Panel();
panel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
panel.addComponent(new Label(note.getCreatedAt()));
panel.addComponent(new Label(note.getTitle()));
TextBox contentsTextBox = new TextBox(note.getContents())
.setLayoutData(LinearLayout.createLayoutData(LinearLayout.Alignment.Fill));
panel.addComponent(contentsTextBox);
Button removeButton = new Button("Remove", new RemoveButtonAction(note));
panel.addComponent(removeButton);
Button saveButton = new Button("Save", new SaveButtonAction(note, contentsTextBox));
panel.addComponent(saveButton);
return panel;
}
private class RemoveButtonAction implements Runnable {
private Note note;
public RemoveButtonAction(Note note) {
this.note = note;
}
@Override
public void run() {
notes.remove(note);
refreshNotesPanel();
}
}
private class SaveButtonAction implements Runnable {
private Note note;
private TextBox contentsTextBox;
public SaveButtonAction(Note note, TextBox contentsTextBox) {
this.note = note;
this.contentsTextBox = contentsTextBox;
}
@Override
public void run() {
String newContents = contentsTextBox.getText();
note.setContents(newContents);
// Perform any additional saving operations here
MessageDialog.showMessageDialog(getTextGUI(), "Note Saved", "Contents saved successfully!");
refreshNotesPanel();
}
}
}