|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-24-2008, 09:29 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Adding Multiple Panels and Single Scroll bar on the JFrame
I need to add multiple panels on the Jframe one after another(Vertically). To visualize the Problem let us take an example. In the website, there are multiple panels on the Frame and in the right most Part of Frame, it has a Scrollbar to view items present at the bottom. But I am confused how I can do this.
JPanel p=new JPanel();
p.setLayout(null);
JPanel p1=new JPanel();
p1.setLayout(null);
JPanel p2=new JPanel();
p2.setLayout(null);
JPanel p3=new JPanel();
p3.setLayout(null);
these panels contains different components. and finally added to JFrame which has null Layout.
But dont know which parameter I should pass in JScrollPane
JScrollPane js=new JScrollPane(???);
so that my purpose get solved.
Secondly How I can add ScrollBar to any Panel in the above mentioned.It means two scroll bar on the same Frame;
Best Example to visualize the problem is : See the WebPage that appears on pressing Post New Thread.
|
|

07-24-2008, 10:26 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,266
|
|
import java.awt.*;
import javax.swing.*;
public class MultiPanels {
private JScrollPane getContent() {
Dimension d = new Dimension(300,200);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getPanel(d, 6, Color.red), gbc);
panel.add(getPanel(d, 4, Color.green.darker()), gbc);
panel.add(getPanel(d, 12, Color.blue), gbc);
panel.add(getEmptyPanel(d), gbc);
return new JScrollPane(panel);
}
private JScrollPane getPanel(Dimension d, int rows, Color color) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(color);
GridBagConstraints gbc= new GridBagConstraints();
gbc.insets = new Insets(10,5,10,5);
gbc.weightx = 1.0;
for(int i = 0, j = 1; i < rows; i++) {
gbc.gridwidth = GridBagConstraints.RELATIVE;
panel.add(new JButton(String.valueOf(j++)), gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(new JButton(String.valueOf(j++)), gbc);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
private JScrollPane getEmptyPanel(Dimension d) {
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0,0,Color.red,
0,h,Color.cyan);
((Graphics2D)g).setPaint(gp);
g.fillRect(0,0,w,h);
}
};
// Default size for a JPanel is (10,10). Its layout
// manager computes its preferredSize while laying
// out the child components. If there are no children
// the panel reports its default size to its parent.
// Use the setPreferredSize method to provide
// a size hint to the parent of this component.
panel.setPreferredSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MultiPanels().getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|

07-25-2008, 10:48 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
import java.awt.*;
import javax.swing.*;
public class MultiPanels {
JButton b1;
private JScrollPane getContent() {
Dimension d = new Dimension(300,200);
JPanel panel = new JPanel(null);
// GridBagConstraints gbc= new GridBagConstraints();
// gbc.weightx = 1.0;
// gbc.fill = GridBagConstraints.HORIZONTAL;
// gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(getPanel(d, 6, Color.red));
panel.add(getPanel(d, 4, Color.green.brighter()));
panel.add(getPanel(d, 12, Color.blue));
// panel.add(getEmptyPanel(d), gbc);
return new JScrollPane(panel);
}
private JScrollPane getPanel(Dimension d, int rows, Color color) {
JPanel panel = new JPanel(null);
panel.setBackground(color);
b1=new JButton("ok");
b1.setLocation(100, 100);
b1.setSize(70,20);
panel.add(b1);
b1=new JButton("ok");
b1.setLocation(100, 300);
b1.setSize(70,20);
panel.add(b1);
b1=new JButton("ok");
b1.setLocation(100, 500);
b1.setSize(70,20);
panel.add(b1);
b1=new JButton("ok");
b1.setLocation(100, 700);
b1.setSize(70,20);
panel.add(b1);
// GridBagConstraints gbc= new GridBagConstraints();
// gbc.insets = new Insets(10,5,10,5);
// gbc.weightx = 1.0;
// for(int i = 0, j = 1; i < rows; i++) {
// gbc.gridwidth = GridBagConstraints.RELATIVE;
// panel.add(new JButton(String.valueOf(j++)), gbc);
// gbc.gridwidth = GridBagConstraints.REMAINDER;
// panel.add(new JButton(String.valueOf(j++)), gbc);
// }
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
/*private JScrollPane getEmptyPanel(Dimension d) {
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(0,0,Color.red,
0,h,Color.cyan);
((Graphics2D)g).setPaint(gp);
g.fillRect(0,0,w,h);
}
};
// Default size for a JPanel is (10,10). Its layout
// manager computes its preferredSize while laying
// out the child components. If there are no children
// the panel reports its default size to its parent.
// Use the setPreferredSize method to provide
// a size hint to the parent of this component.
panel.setPreferredSize(new Dimension(300,400));
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}*/
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MultiPanels().getContent());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
|
|

07-25-2008, 10:51 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Thanks for this code. But I Have a problem in NULL Layout.
I made following changes in your code. Their is no error in this code but a blank screen is displayed.
|
|

07-25-2008, 11:11 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,266
|
|
b1=new JButton("ok");
b1.setLocation(100, 700);
b1.setSize(70,20);
panel.add(b1);
// The scrollPane will show this panel at its
// preferredSize which is:
Dimension dim = panel.getPreferredSize();
System.out.printf("panel preferredSize = [%d, %d]%n",
dim.width, dim.height);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(d);
return scrollPane;
}
|
|

07-28-2008, 11:28 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Still the problem is not solved. Blank screen is Displayed. The output of S.o.p is
panel preferredSize = [0, 0]
panel preferredSize = [0, 0]
panel preferredSize = [0, 0]
Can you tell me the solution????????
even I pass [0,0] as parameter but the o/p is same
|
|

07-28-2008, 08:04 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,266
|
|
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|