Java ScrollPane Overlay Content

I have what I'm sure is a beginner problem with my JScrollPanes. The problem is that the vertical scroll bar overlaps the components inside the closed panel (on the right side). It gets a little painful when the scrollbar overlaps the JComboBoxes popup bit.

I swallowed the problem to this small fragment - I hope this illustrates the problem.

public class ScrollTest extends JFrame { public ScrollTest() { super("Overlap issues!"); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(100,0)); for(int b=0;b<100;++b) { panel.add(new JButton("Small overlap here ->")); } JScrollPane scrollpane = new JScrollPane(panel); add(scrollpane); pack(); setVisible(true); } public static void main(String[] args) { new ScrollTest(); } } 

I had a first glance, but I could not understand if anyone else was addressing this issue. Sorry if this is a duplicate and thank you very much for any help anyone can offer java-newb like me!

+4
source share
1 answer

The problem is that the default value for JScrollPane is to link the components with the default value of JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, which in turn adds a scroll bar without re-linking the components.

In your example, you know that you will need a scroll bar to change it to always display the scroll bar

 public class ScrollTest extends JFrame { public ScrollTest() { super("Overlap issues!"); JPanel panel = new JPanel(); //Insets insets = panel.getInsets(); //insets.set(5, 5, 5, 25); //insets.set(top, left, bottom, right); panel.setLayout(new GridLayout(100,0)); for(int b=0;b<100;++b) { panel.add(new JButton("Small overlap here ->")); } JScrollPane scrollpane = new JScrollPane(panel); scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); add(scrollpane); pack(); setVisible(true); } public static void main(String[] args) { new ScrollTest(); } } 
+4
source

All Articles