Nested JScrollPane does not fit

I have a problem with nested JScrollPanes. Basically I want to have one external JScrollPane that scrolls vertically, but not horizontally (think of the Netflix web interface). Inside this external JScrollPane, I want to have several JScrollPanes that scroll horizontally. My problem is that the horizontal scrollbars for internal JScrollPanes never appear, as they look like they occupy the entire preferred size of their JPanels. Here is an image to describe what I'm talking about:

enter image description here

EDIT: This code based on camickr's answer now works:

import java.awt.BorderLayout; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; public class NestedScrollPane extends JFrame { public NestedScrollPane() { ScrollablePanel outerPanel = new ScrollablePanel(); outerPanel.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT); outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS)); for (int j = 0; j < 20; j++) { ScrollablePanel innerPanel = new ScrollablePanel(); innerPanel.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE); innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS)); JScrollPane innerScrollPane = new JScrollPane(innerPanel); innerScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); for (int i = 0; i < 10; i++) { JLabel longLabel = new JLabel("asefaesfesfesfgesgersgrsgdrsgdrsgderg "); innerPanel.add(longLabel); } outerPanel.add(innerScrollPane); } JScrollPane outerPane = new JScrollPane(outerPanel); outerPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); this.setContentPane(outerPane); this.setSize(400, 400); outerPane.setSize(400, 400); this.setVisible(true); } public static void main (String[] args) { NestedScrollPane pane = new NestedScrollPane(); pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 

I took a look at How to get JScrollPanes inside JScrollPane to keep track of the resizing of the parent house , but using BoxLayout or BorderLayout on the external panel does not seem to fix anything.

+4
source share
1 answer

You need to implement the Scrollable interface of the external panel added to the viewport to make the panel fill the width of the viewport.

An easy way to do this is to use a scrollable bar . You should be able to use:

 // JPanel outerPanel = new JPanel(); ScrollablePanel outerPanel = new ScrollablePanel(); outerPanel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT ); 
+6
source

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


All Articles