Making the JList scroll to the end of its elements

I am using the following code to scroll to the end of the JList. I use it when I set the Visible on the window containing the JList and when I add items to the JList.

int lastIndex = getMyList().getModel().getSize() - 1;

if (lastIndex >= 0) {
    getMyList().ensureIndexIsVisible(lastIndex);
}

      

The problem is when calling setVisible for the first time, the JList scrolls through multiple items for no apparent reason. If I then call setVisible (false), followed by a call to setVisible (true), the JList scrolls down as intended. One thing I noticed is that the moment I open the window, some of its components seem to be redone in a split second. So I guess the problem might be below.

So, I clicked "print screen" when I opened one of these windows, and you can see that the first time you open the window, the components at the bottom of it are not visible (emoji and buttons). This is what the window looks like when it is "fully open", as well as an image of what it looks like for a fraction of a second before it is fully open.

What the window looks like after opening it (you can see that the scrollbar is not at the bottom) enter image description here

What the window looks like when I take a snapshot of it, when it opens after calling setVisible () enter image description here

So, does anyone know why the layout changes when the window opens, and how can I get my JList to scroll to the bottom when it opens?

+3


source to share


1 answer


Try to bend the call ensureIndexIsVisible()

inSwingUtilities.invokeLater()



+6


source







All Articles