• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to place Panel into Frame

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
vivek modi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maneesh !! It worked
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic