NetBeans: how to add ScrollBar to JPanel
Asked Answered
I

4

5

I am developing a small desktop application in NetBeans. On my UI, I place a JPanel and put a single JLabel on it. The content of this JLabel is generated dynamically, so if the content is very large then it goes out of screen.So is there any way in which I can specify a fixed size for the JPanel and of course ScrollBars should appear when the text exceeds the screen size.

Isma answered 1/10, 2011 at 11:26 Comment(0)
K
6

Use JScrollPane to contain Your big JPanel.

Kapok answered 1/10, 2011 at 11:27 Comment(0)
Q
8

You will have to just pass Component reference to the JScrollPane Constructor. It will work fine. You can definetely use JScrollPane The following is the sudo example of the JScrollPane for JPanel from my past project. Hope it will be useful for you.

import javax.swing.*;
import java.awt.*;

public class Frame01
{
    public static void main(String[] args){
        SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                JFrame frame = new JFrame("panel demo");
                frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

                JPanel panel = new JPanel();
                Container c = frame.getContentPane();
                panel.setSize(100,100);
                panel.setLayout(new GridLayout(1000,1));
                for(int i = 0; i<1000;i++)
                panel.add(new JLabel("JLabel "+i));

                JScrollPane jsp = new JScrollPane(panel);
                c.add(jsp);
                frame.setSize(100,100);
                frame.setVisible(true);
            }
        });
    }
}
Quick answered 1/10, 2011 at 11:52 Comment(0)
K
6

Use JScrollPane to contain Your big JPanel.

Kapok answered 1/10, 2011 at 11:27 Comment(0)
J
2

so in case when the contents are very large it goes out of screen, maybe you have to look for TextComponents as JTextArea or JEditorPane, tutorial contains example including basic usage for JScrollPane

Jennee answered 1/10, 2011 at 11:33 Comment(0)
W
2

In the Navigator, click on JPanel with the right mouse button --> Enclose In --> Scroll Pane.

Done!, You have now scrolls

Weise answered 21/12, 2013 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.