Results 1 to 8 of 8
Thread: Adding panels into a panel
- 12-07-2009, 10:31 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
Adding panels into a panel
I am trying to develop a specific panel which includes 3 panels and one of them also includes 5 panels but only one of them seen on the screen
the code:
Java Code:import java.awt.Color; import java.awt.Container; //import java.awt.LayoutManager; import javax.swing.JPanel; public class MiddlePanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; private JPanel stonearea1; private JPanel stonearea2; private JPanel seriarea; private JPanel seri1; private JPanel seri2; private JPanel seri3; private JPanel seri4; private JPanel seri5; public MiddlePanel(int x,int y,int w,int h) { super(); // TODO Auto-generated constructor stub this.setBounds(x, y, w, h); this.setLayout(null); this.initComp(); } private void initComp(){ System.out.println("x "+this.getX()+"y "+this.getY()+"wid "+this.getWidth()+"h "+this.getHeight()); this.stonearea1 = new JPanel(); stonearea1.setBounds(this.getX(),this.getY(),this.getWidth()/3,this.getHeight()); stonearea1.setToolTipText("stonearea1"); stonearea1.setBackground(Color.black); System.out.println("stone1 "+this.stonearea1.getHeight()); this.stonearea2 = new JPanel(); stonearea2.setBounds(this.getX()+this.stonearea1.getWidth(), this.getY(), this.getWidth()/3,this.getHeight()); stonearea2.setToolTipText("stonearea2"); stonearea2.setBackground(Color.red); System.out.println("stone2 "+this.stonearea2.getX()+" y "+this.stonearea2.getY()); this.seriarea = new JPanel(null); seriarea.setToolTipText("seriarea"); seriarea.setBackground(Color.yellow); seriarea.setBounds(this.getX()+this.stonearea1.getWidth()+this.stonearea2.getWidth(),this.getY(),this.getWidth()/3,this.getHeight()); this.seri1 = new JPanel(); seri1.setBounds(seriarea.getX(), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4); seri1.setToolTipText("seri1"); this.seri2 = new JPanel(); seri2.setBounds(seriarea.getX()+(seriarea.getWidth()/4), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4); seri1.setToolTipText("seri2"); this.seri3 = new JPanel(); seri3.setBounds(seriarea.getX()+(seriarea.getWidth()/2), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4); seri1.setToolTipText("seri3"); this.seri4 = new JPanel(); seri4.setBounds(seriarea.getX()+3*(seriarea.getWidth()/4), seriarea.getY(), seriarea.getWidth()/4, (3*seriarea.getHeight())/4); seri1.setToolTipText("seri4"); this.seri5 = new JPanel(); seri5.setBounds(seriarea.getX(),seriarea.getY()+3*(seriarea.getHeight()/4), seriarea.getWidth(), seriarea.getHeight()/4); seri5.setToolTipText("seri1"); this.seriarea.add(seri1); this.seriarea.add(seri2); this.seriarea.add(seri3); this.seriarea.add(seri4); this.seriarea.add(seri5); System.out.println("seriarea count "+this.seriarea.getComponentCount()); this.add(stonearea1); this.add(stonearea2); this.add(seriarea); this.setVisible(true); System.out.println("count "+this.getComponentCount()); } }
On the screen only stonarea1 is shown. !!!Last edited by Fubarable; 12-07-2009 at 12:17 PM. Reason: code tags added
- 12-07-2009, 10:56 AM #2
Use nested LayoutManagers.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-07-2009, 11:08 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
I want to know what is wrong in the code . if I dont want to use layout managers
- 12-07-2009, 11:28 AM #4
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
I think the problem is that you are actually using layout manayers. Each component has one by default, so maybe they are putting one in the top of the other.
- 12-07-2009, 12:32 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
But i wrote setLayout(null) in the constructor for the biggest panel and for the third panel also,
the other two panel does not have any sub components. On the other hand although i tried setlayout null for all of them it does not work.
- 12-07-2009, 12:42 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 96
- Rep Power
- 0
- 12-07-2009, 01:12 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 7
- Rep Power
- 0
Oh my god !!!
In fact it works correct but i always test my code eclipse appletviewer and it creates a html page and gives width and height values 200 automatically so i couldn't see the all screen.
I am testing from the original source now. :):):)
- 12-07-2009, 05:19 PM #8
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 284
- Rep Power
- 4
The problem was a result of a misuse of null layouts.
The bounds assigned for seri1, ser2, ... were relative to MiddlePanel,
but they have to be relative to seriarea, their parent Container.
A LayoutManager automatically does layout relative to the parent.
Without a layout manager the code also has to account for the fact
that the contentPane is not the full height of the JFrame.
That is the purpose of TITLESIZEKLUDGEFORNULLLAYOUT.
I also took pains to ensure that roundoff error did not prevent the layouts
from fully and exactly covering their parents.
In the original code, seri5 overlapped the bottom edge of the other serix-s.
Java Code:package Test; import java.awt.*; import javax.swing.*; public class TestNullLayout extends JFrame { final static int TITLESIZEKLUDGEFORNULLLAYOUT = 32; public TestNullLayout() { super("TestNullLayout"); setDefaultCloseOperation(EXIT_ON_CLOSE); setPreferredSize(new Dimension(300, 150)); add(new MiddlePanel(0, 0, 300, 150 - TITLESIZEKLUDGEFORNULLLAYOUT)); pack(); } public class MiddlePanel extends JPanel { private static final long serialVersionUID = 1L; private JPanel stonearea1; private JPanel stonearea2; private JPanel seriarea; private JPanel seri1; private JPanel seri2; private JPanel seri3; private JPanel seri4; private JPanel seri5; public MiddlePanel(int x, int y, int w, int h) { super(null); setToolTipText("MiddlePanel"); setBounds(x, y, w, h); initComp(); } private void dumpBounds(JPanel p) { System.out.println("bounds for " + p.getToolTipText() + " are " + p.getBounds()); Insets ins = p.getInsets(); if (ins.top != 0 || ins.right != 0 || ins.bottom != 0 || ins.left != 0 ) System.out.println(" insets are" + ins); int comps = p.getComponentCount(); if (comps > 0) System.out.println(" has " + comps + " components"); } private void initComp() { stonearea1 = new JPanel(); stonearea1.setBounds(getX(), getY(), getWidth() / 3, getHeight()); stonearea1.setToolTipText("stonearea1"); stonearea1.setBackground(Color.black); stonearea2 = new JPanel(); stonearea2.setBounds(getX() + stonearea1.getWidth(), getY(), getWidth() / 3, getHeight()); stonearea2.setToolTipText("stonearea2"); stonearea2.setBackground(Color.red); seriarea = new JPanel(null); seriarea.setToolTipText("seriarea"); seriarea.setBackground(Color.yellow); int x3 = stonearea2.getX() + stonearea2.getWidth(); seriarea.setBounds(x3, getY(), getWidth() - x3, getHeight()); int topH = 3 * seriarea.getHeight() / 4; int topW = seriarea.getWidth() / 4; seri1 = new JPanel(); seri1.setBounds(0, 0, topW, topH); seri1.setToolTipText("seri1"); seri1.setBackground(Color.BLUE); seri2 = new JPanel(); seri2.setBounds(topW, 0, topW, topH); seri2.setToolTipText("seri2"); seri2.setOpaque(false); seri3 = new JPanel(); seri3.setBounds(2 * topW, 0, topW, topH); seri3.setToolTipText("seri3"); seri3.setBackground(Color.GREEN); seri4 = new JPanel(); seri4.setBounds(3 * topW, 0, seriarea.getWidth() - 3 * topW, topH); seri4.setToolTipText("seri4"); seri5 = new JPanel(); seri5.setBounds(0, topH, seriarea.getWidth(), seriarea.getHeight() - topH); seri5.setToolTipText("seri5"); seriarea.add(seri1); seriarea.add(seri2); seriarea.add(seri3); seriarea.add(seri4); seriarea.add(seri5); add(stonearea1); add(stonearea2); add(seriarea); dumpBounds(this); dumpBounds(stonearea1); dumpBounds(stonearea2); dumpBounds(seriarea); dumpBounds(seri1); dumpBounds(seri2); dumpBounds(seri3); dumpBounds(seri4); dumpBounds(seri5); } } /** * @param args the command line arguments */ public static void main(String[] args) { new TestNullLayout().setVisible(true); } }
Similar Threads
-
Adding components to a panel
By jboy in forum New To JavaReplies: 1Last Post: 10-10-2009, 01:02 PM -
Spliting the Panel to three panels
By suraw in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:05 PM -
Adding Multiple Panels and Single Scroll bar on the JFrame
By SANDY_INDIA in forum AWT / SwingReplies: 6Last Post: 07-28-2008, 06:04 PM -
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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks