简体   繁体   中英

Add JScrollPane to JPanel

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具有绿色背景

EDITED: This is the latest screenshot, the bigPanel shows empty after I wrote the code as above. 在此处输入图片说明

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

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);

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM