Java: Scrollbar doesn't appear, even though `setVerticalScrollBarPolicy` and other options are set

StackOverflow https://stackoverflow.com/questions/19742543

  •  03-07-2022
  •  | 
  •  

문제

I got an issue with my JScrollpane. I am adding Labels to it, out from a list. The adding is working and I see the labels. The amount of Labels added is unknown to me, so it can appear that the border of the scrollpane wont be enough. Thats the advantage of using a Scrollpane, so that I can actually scroll down if needed. But the scrollbar doesnt appear. I know there are many questions like that asked, but I tried almost every proposed suggestion. I tried setPreferredSize(), setLayout(), scrollPaneApps.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS), but nothing of it worked.

public DisplayProperties() {

   setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   setBounds(100, 100, 450, 300);
   contentPane = new JPanel();
   setContentPane(contentPane);
   contentPane.setLayout(null);
   contentPane.setPreferredSize(new Dimension(450,300));

   JScrollPane scrollPane1 = new JScrollPane();
   scrollPane1.setBounds(15, 54, 195, 202);
   scrollPane1.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
   scrollPane1.setPreferredSize(new Dimension(185,195));
   scrollPane1.setLayout(null);   
   scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   contentPane.add(scrollPane1);


    int b = 0;

    for(String s : XMLParser.ApplicationsListGUI)
    {
        b = b + 20;
        JLabel lbl = new JLabel("lbl"+s);
        lbl.setText(s);
        lbl.setBounds(10,b,100,15);
        scrollPane1.add(lbl);
        scrollPane1.revalidate();   
        lbl.setVisible(true);
    }   
}

So why doesnt this Scrollbar dont appear?

도움이 되었습니까?

해결책

You appear to have two JScrollPanes involved, scrollPaneApps, which you set a vertical scrollbar policy, which you try to add a component to, but which you never add to the GUI, and scrollPane1, which you don't set a policy, never add components to, but do add to the GUI. Sorry, but this is totally crazy. You need to fix this so that your code makes sense:

  • Add the actual JScrollPane that has its vertical scrollbar policy set to the GUI. If it's not added to the GUI, it makes sense that it will never be seen.
  • Don't add components directly to the JScrollPane but rather to its JViewport via the setViewportView(...) method. Or you can add a component to the JScrollPane constructor which is little more than syntactic sugar for adding it to the viewport.
  • Avoid null layouts and absolute positioning (avoid setBounds(...)). Using these will make your GUI's rigid, ugly, and almost impossible to improve upon later. Never set a JScrollPane's layout as null, for if you do, it will stop working. Completely.

다른 팁

It doesn't show since you have no layout for the scrollPaneApps panel. So remove this line and it should show.

scrollPane1.setLayout(null);

Hope it helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top