aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to place Panel into Frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to place Panel into Frame" Watch "how to place Panel into Frame" New topic
Author

how to place Panel into Frame

vivek modi
Greenhorn

Joined: Nov 18, 2007
Posts: 3
Hi Everybody !! I am new to Swing and am working on a GUI based Elevator Simulation System. I am using Frame on which I am trying to put a Panel. But the panel does not set to the size I want it to even if I use setSize() method. I am posting the code .Could anyone please help me.

Thanks !!

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Originally posted by vivek modi:
Hi Everybody !! I am new to Swing and am working on a GUI based Elevator Simulation System. I am using Frame on which I am trying to put a Panel. But the panel does not set to the size I want it to even if I use setSize() method. I am posting the code .Could anyone please help me.

Thanks !!


To achieve the desired result, you need to change two things:

1)The content pane has a BorderLayout by default. So when you say the lp1 is set to the Center which ignores the panel dimension. So you need to call before adding the components, which will start respecting the panel dimensions

2)Use instead of setSize. Whenever a container lays out the components inside, it queries for the preferred size.

It is also a good practise to call the setVisible() on the frame as the last statement. Setting the frame dimensions, adding the components should be done before you call the setVisible.

Even thought your existing code works right now, what is happening is this:
The frame.setVisible() is called. Since you have not provided any dimensions, the default 0,0 dimensions are used. Then the components are set. Since the contained components are changed, the repaint is called, which again calls the code to resize the window and display the laid out components.

So in a nutshell, the frame is repainted twice unnecessarily.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
vivek modi
Greenhorn

Joined: Nov 18, 2007
Posts: 3
Hi Maneesh !! Your post solved my problem. Now my panel is getting displayed on the frame.Thanks !! But I have one more query. I want to display the panel at my desired location and using method setLocation() it does not work.
Is there any other way ?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

Originally posted by vivek modi:
I want to display the panel at my desired location and using method setLocation() it does not work.
Is there any other way ?


Why do you want to specify the exact location? Have you considered what will happen if the user resizes the frame or the user is running some different screen resolution? The LayoutManagers are designed to handle all these.
Even then, if your requirement dictates that you specify a location, this is what you need to do.

1) Set the layout of the content pane to null by calling getContentPane.setLayout(null);
2) Add the panel to the content pane.
3) Define the bounds of the panel by calling panel.setBounds(int x, int y, int width, int height) where x,y are the location and the width and height are the dimensions of the panel.

Please note the step#2 should be called before step#3 , else it wont work.

Best of luck.
[ November 19, 2007: Message edited by: Maneesh Godbole ]
vivek modi
Greenhorn

Joined: Nov 18, 2007
Posts: 3
Thanks Maneesh !! It worked
akshay gupta
Greenhorn

Joined: May 30, 2011
Posts: 1
Maneesh Godbole wrote:
Originally posted by vivek modi:
I want to display the panel at my desired location and using method setLocation() it does not work.
Is there any other way ?


Why do you want to specify the exact location? Have you considered what will happen if the user resizes the frame or the user is running some different screen resolution? The LayoutManagers are designed to handle all these.
Even then, if your requirement dictates that you specify a location, this is what you need to do.

1) Set the layout of the content pane to null by calling getContentPane.setLayout(null);
2) Add the panel to the content pane.
3) Define the bounds of the panel by calling panel.setBounds(int x, int y, int width, int height) where x,y are the location and the width and height are the dimensions of the panel.

Please note the step#2 should be called before step#3 , else it wont work.

Best of luck.
[ November 19, 2007: Message edited by: Maneesh Godbole ]



Thank you ....it helped me a lot..
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

Maneesh Godbole wrote:Please note the step#2 should be called before step#3 , else it wont work.


Not so. The order isn't critical.Note that when using a layout manager, it's important to set the layout before adding the components.


luck, db
There are no new questions, but there may be new answers.
pratik maru
Greenhorn

Joined: Jun 13, 2012
Posts: 2
Maneesh Godbole wrote:
Originally posted by vivek modi:
I want to display the panel at my desired location and using method setLocation() it does not work.
Is there any other way ?


Why do you want to specify the exact location? Have you considered what will happen if the user resizes the frame or the user is running some different screen resolution? The LayoutManagers are designed to handle all these.
Even then, if your requirement dictates that you specify a location, this is what you need to do.

1) Set the layout of the content pane to null by calling getContentPane.setLayout(null);
2) Add the panel to the content pane.
3) Define the bounds of the panel by calling panel.setBounds(int x, int y, int width, int height) where x,y are the location and the width and height are the dimensions of the panel.

Please note the step#2 should be called before step#3 , else it wont work.

Best of luck.
[ November 19, 2007: Message edited by: Maneesh Godbole ]




hey Manish it will be a great help if you can tell me how make my swing application program work in different resolution systems
thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

pratik maru wrote:how make my swing application program work in different resolution systems

Use an appropriate layout manager or combination of nested layouts and pack() the window that displays the GUI.
pratik maru
Greenhorn

Joined: Jun 13, 2012
Posts: 2
Darryl Burke wrote:
pratik maru wrote:how make my swing application program work in different resolution systems

Use an appropriate layout manager or combination of nested layouts and pack() the window that displays the GUI.


by appropriate layout manager you mean which one and can you give me an example
Hitesh P Patel
Greenhorn

Joined: May 23, 2012
Posts: 8
by appropriate layout manager you mean which one

It depends on your design to choose the appropriate layout

can you give me an example

Read the section from the Swing tutorial on How to Use Various Layout Managers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to place Panel into Frame