Results 1 to 7 of 7
- 07-24-2008, 07:29 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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, 08:26 AM #2
Java Code: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 [i]setPreferredSize[/i] 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, 08:48 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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, 08:51 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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, 09:11 AM #5
Java Code: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, 09:28 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 20
- Rep Power
- 0
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, 06:04 PM #7
Similar Threads
-
Problem in adding Multiple Panels at the Specific positon on frame
By SANDY_INDIA in forum AWT / SwingReplies: 7Last Post: 07-09-2008, 12:06 AM -
Adding and removing panels dynamically
By kbyrne in forum AWT / SwingReplies: 1Last Post: 04-12-2008, 08:28 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Can't synchronize multiple JPanels in a JFrame
By vassil_zorev in forum AWT / SwingReplies: 0Last Post: 12-30-2007, 04:22 PM -
Jtextarea and scroll
By ziniestro in forum AWT / SwingReplies: 2Last Post: 06-01-2007, 03:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks