How to get JScrollPane to work with nested JPanels?

I am building a Swing application in Java using NetBeans and I have a layout problem. My main frame contains JScrollPane, which contains JPanel, called contentPanel, which, in turn, contains JPanel, called listPanel. When the program starts, it is listPanelempty, but when the user interacts with the program, an unpredictable number of smaller ones are added to it JPanel. I used the NetBeans Builder GUI to snap the top edge listPanelto the top contentPanel, as well as to the bottom edges.

The problem is that when adding more components to the listPanelvertical scrollbar does not appear on my scrollpane. verticalScrollBarPolicymy scrollpane is set to AS_NEEDED, and its viewportViewset to contentPanel. I think what I need to do to make it contentPanelgrow when listPanelmore elements are added.

+3
source share
2 answers

The problem is that when adding additional components to the listPanel, the vertical scrollbar does not appear on my scrollpane.

, , scrollpane, , . , , scrollpane - . , :

panel.add( subPanel );
panel.revalidate();

, , ( ):

panel.add( subPanel );
scrollPane.revalidate();   

- revalidate(), .

+3

LayoutManager. , , BoxLayout. , .

+1

Source: https://habr.com/ru/post/1796272/


All Articles