Question

I have created an on the fly search field in Java Swing. I have a JTextField where you enter what to search for, and a JList inside a JScrollPane that contains the matches on your input. When you write in the JTextField, it controls the JScrollPane: The contents of it, and if it should be visible or not.

It works fine when I create a sample window using it, but not in my big GUI. I can't explain the GUI, so I don't expect you to solve this entirely, but maybe answer a question about it.

The thing is, that when the JTextField is empty or we find no hits, the list is hidden (setVisible(false)). When you start typing, and it found at least one item, it's shown (setVisible(true)). When I start up my GUI, and I start typing in the search field, the list isn't shown as it should. It simply doesn't appear. I have debugged the code, and the scrollpane IS set to visible = true. It is visible according to the running code, but I can't see it and I can't interact with it. It's not hidden behind something else.

Now to the funny thing. If I type something that should generate a hit, and then hover over the JTextField, activating its' tooltip, the JScrollBar appears! After that has happened, the search field acts normally, until I close the window. The visible flag is not changed, but somehow it decided that now we can paint it.

Now I want to know, what happens when a tooltip is shown? Is there some special painting going on that I could call myself?

Was it helpful?

Solution

You should call revalidate() on the component that contains the scroll pane and the text field after you call setVisible. The general rule is that if you change the layout of a container component after the container has been laid out and displayed to the user, you should call revalidate on it.

Let me know if that helps!

OTHER TIPS

maybe look at AutoComplete JComboBox / JTextField, for AutoComplete JComboBox isn't neccesary to bothering with JToolTip

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top