Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to place Panel into Frame

 
vivek modi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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 !!

 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Maneesh !! It worked
 
akshay gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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..
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
pratik maru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Hitesh P Patel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic