Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2008, 09:29 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-24-2008, 10:26 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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 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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-25-2008, 10:48 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-25-2008, 10:51 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-25-2008, 11:11 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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; }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-28-2008, 11:28 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-28-2008, 08:04 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Doing Without a Layout Manager (Absolute Positioning)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in adding Multiple Panels at the Specific positon on frame SANDY_INDIA AWT / Swing 7 07-09-2008 02:06 AM
Adding and removing panels dynamically kbyrne AWT / Swing 1 04-12-2008 10:28 PM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 07:29 PM
Can't synchronize multiple JPanels in a JFrame vassil_zorev AWT / Swing 0 12-30-2007 06:22 PM
Jtextarea and scroll ziniestro AWT / Swing 2 06-01-2007 05:59 PM


All times are GMT +3. The time now is 12:03 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org