mabe02 / lanterna

Java library for creating text-based GUIs
GNU Lesser General Public License v3.0
2.26k stars 242 forks source link

AbstractListBox.setSelectedIndex() does not update screen #581

Closed zanonmark closed 1 year ago

zanonmark commented 1 year ago

When I use .setSelectedIndex(n) on a AbstractListBox (actually AListBox<V> extends AbstractListBox<V, AListBox<V>>), the screen is not updated, i.e.: the selected item is not highlighted. I must manually press any arrow key for the selected item to be colored.

Do I have to manually invoke any refresh method?

Thanks, MZ

avl42 commented 1 year ago

It depends on where you called the method from.

If you call it from handleKeyStroke(...) and let your component's handleKeyStroke return Result.HANDLED afterwards, then I'd expect that the gui-framework would refresh automatically. If you call it from somewhere else, or do not immediately return to the main loop, then you might need an extra call to updateScreen on the textGui, which not only does a refresh, but also makes sure, that each relevant component redraws itself for its now current content.

zanonmark commented 1 year ago

Thanks for replying.

To summarize a little:

I have a public class AListSelectionWindow extends AbstractWindow, whose relevant code looks like this:

    protected AListBox<String> listWidget = new AListBox<String>();

    protected void setListItems(LinkedList<String> listItems, String selectedListItem) {
        this.listWidget.clearItems();
        //
        for (String listItem: listItems) {
            this.listWidget.addItem(listItem);
        }
        //
        this.listWidget.setSelectedItem(selectedListItem);
    }

    public static String showModalWindow(AUiClient uiClient, String title, LinkedList<String> listItems, String selectedListItem) {
        AListSelectionWindow listSelectionWindow = new AListSelectionWindow(uiClient);
        listSelectionWindow.setTitle(title);
        listSelectionWindow.setListItems(listItems, selectedListItem);
        //
        listSelectionWindow.waitUntilClosed();
[...]

and in public class AListBox<V> extends AbstractListBox<V, AListBox<V>>:

    public void setSelectedItem(V selectedItem) {
        if (null == selectedItem) {
            return;
        }
        //
        for (int i = 0; this.getItemCount() > i; i++) {
            if (selectedItem.equals(this.getItemAt(i))) {
                this.setSelectedIndex(i);
                //
                break;
            }
        }
    }

Debugging shows that when I call AListSelectionWindow.showModalWindow() the AListBox.setSelectedItem() method works fine, i.e.: the proper item is selected internally. But the UI is not refreshed.

I tried to call getTextGUI().updateScreen(); right after the .setSelectedItem() method, both on the widget and on the window, to no avail.

I also tried to disable (temporarily) the listSelectionWindow.waitUntilClosed(); line: nothing changes.

Thanks a lot for Your help, MZ

zanonmark commented 1 year ago

Closing the ticket: the widget was simply missing the focus when creating the window.

Sorry,

MZ