0

I try to make a scrollbar for a JPanel but when I RUN, the scrollbar not even show up.

EDITED: This is the latest code, I updated the contentPane layout.

This is main Panel:

contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setPreferredSize(new Dimension(1200, 400));
setContentPane(contentPane);

This is JPanel inside the main Panel:

bigPanel = new JPanel();
bigPanel.setPreferredSize(new Dimension(1137, 520));

bigPanel.setBackground(new Color(224, 255, 255));

JScrollPane scrollPane = new JScrollPane(bigPanel);
scrollPane.setPreferredSize(new Dimension(600, 600));

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);   
contentPane.add(scrollPane,"1, 2, 6, 1, center, default");   
bigPanel.setLayout(new FormLayout(new ColumnSpec[] {...}
bigPanel.add(BUTTON1);
bigPanel.add(BUTTON2);.....

Above code was written under:

public UI2(){...}

This is the launch application code:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {

                UI2 frame = new UI2();                      
                frame.pack();
                frame.setDefaultCloseOperation(UI2.EXIT_ON_CLOSE);
                frame.setSize(1376,788);                
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

bigPanel has many buttons. So I need a vertical and horizontal scrollbar within bigPanel.

This is the screenshot after I RUN but no scrollbar shown: bigPanel is with green background

EDITED: This is the latest screenshot, the bigPanel shows empty after I wrote the code as above. enter image description here

nani
  • 389
  • 9
  • 32
  • 1
    Change `contentPane.add(BigPanel,"1, 2, 6, 1, center, default");` to `contentPane.add(scrollPane,"1, 2, 6, 1, center, default");`? You should be adding the JScrollPane to the contentPane, if I am not wrong. – almightyGOSU Jul 21 '15 at 08:06
  • @Gosu I tried it then bigPanel shows empty(the buttons all gone) – nani Jul 21 '15 at 08:08
  • Is that your actual code? `BigPanel = new JPanel();` doesn't seem correct.. and `BigPanel.setBounds(162, 157, 1137, 520);` seems wrong too.. Provide a runnable example please, thanks! :) – almightyGOSU Jul 21 '15 at 08:10
  • @Gosu Yes I use that code, the components are shown.. Can I know why you said it seems wrong? – nani Jul 21 '15 at 08:12
  • because of the `BigPanel` and `bigPanel` which you just edited. – almightyGOSU Jul 21 '15 at 08:13
  • try to set the preferred size of bigPanel. – Garry Jul 21 '15 at 08:17
  • @Garry I tried `bigPanel.setPreferredSize(new Dimension(1137, 520));` but all components inside bigPanel gone – nani Jul 21 '15 at 08:24
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 21 '15 at 08:31
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jul 21 '15 at 08:32
  • I believe the problem is with `contentPane.setLayout(null);`, which means that the `JScrollPane` has not definable size and is not been shown because of it. – MadProgrammer Jul 21 '15 at 08:33
  • @MadProgrammer I updated the contentPane layout, is this the right as what you said? I use GroupLayout for contentPane. The bigPanel not showing up – nani Jul 21 '15 at 09:01
  • 1
    I wouldn't use GroupLayout, personally, I might use GridBagLayt, but that would depend on what you want to achieve. Remeber, you can use multiple containers with different layouts to combine together to achieve the ultimate result ;) – MadProgrammer Jul 21 '15 at 09:32

3 Answers3

1

I believe the problem is with contentPane.setLayout(null);, which means that the JScrollPane has not definable size and is not been shown because of it.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Also contentPane.add(BigPanel,"1, 2, 6, 1, center, default"); should be contentPane.add(scrollPane,"1, 2, 6, 1, center, default");. Once you correct for the layout issue, the scroll pane should become visible

See Laying Out Components Within a Container for more ideas

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Thank you so much Sir! I owe you one. I use GridBagLayout for contentPane and the scrollPane shown. Plus, it also helps me to arrange the components more easier – nani Jul 22 '15 at 07:57
0

To force a JScrollPane to always display scroll bars, set the visibility policy:

ScrollPane sp = new JScrollPane(panel);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
VirtualMichael
  • 719
  • 9
  • 18
0

Instead of bigPanel.setBounds(162, 157, 1137, 520); call bigPanel.setPreferredSize(1137, 520); then JScrollPane can use the content size

UPDATE bigPanel.setPreferredSize(new Dimension(1137, 520));

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • I tried but it shows error `The method setPreferredSize(Dimension) in the type JComponent is not applicable for the arguments (int, int)` @StanislavL – nani Jul 21 '15 at 08:19
  • I tried bigPanel.setPreferredSize(new Dimension(1137, 520)); but all components inside bigPanel gone – nani Jul 21 '15 at 08:30
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jul 21 '15 at 08:32
  • The problem is not setPreferredSize but guess null layout. Either use a LayoutManager (recommended way) or place all children components properly in the bigPanel – StanislavL Jul 21 '15 at 08:38