Yubaraj Yubaraj - 1 month ago 20
Java Question

How to make scrollable to jPanel

I am making swing application. And there is too much height of my jPanel. So I want to make this panel as scrollable.:
Following is my description of my requirement.

I have four jpanel in one jpanel I mean:

JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();


I am adding
p2, p3, p4
inside
p1
like following output:

MyOutput

like above showing panel has more height than computer screen height. So I want to display all content of my panel on computer screen by scrolling.

I searched here and found the following questions:



However, the answers did not solve myproblem.

Answer

Without seeing your code, my guess is that you don't have a JScrollpane to provide the scrollable behaviour you want.

JPanel mainPanel = new JPanel(); //This would be the base panel of your UI
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel newPanel = new JPanel();
newPanel.add(p1);
newPanel.add(p2);
newPanel.add(p3);
newPanel.add(p4);
JScrollPane pane = new JScrollPane(newPanel);
mainPanel.add(pane);

Since you use NetBeans, add a JScrollpane from the palette in which you'll add a panel to contain the 4 others. I think you could also just add the 4 panel into the JScrollpane.

Comments