aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Reg:Component Resize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reg:Component Resize" Watch "Reg:Component Resize" New topic
Author

Reg:Component Resize

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
hi,
i have a panel with few components. I made the component layout as null. I defined the boundaries using the setBounds methos. What i need is to resize this components automatically when the window is resized. Can any body help me through it.

Thanks & Regards
alexander
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Use a layout manager.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Yep agrees with Maneesh. Here is a link to the Sun tutorial: Laying Out Components Within a Container

My other recommendation is to learn and use the more basic layouts first such as GridLayout, BoxLayout, BorderLayout, and FlowLayout. Save the GridBagLayout for later. Also, understand that you can nest JPanels each using a different layout, and by so doing build a complex layout.

Best of luck.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Mig Layout is also being talked about.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks for replying.
But i have added components using only the setBounds method. I can do with layout managers also. But main problem is that i am unable to resize components on window resize.
Is it possible to achive component resizing without any layout managers
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
adeeb alexander wrote:Thanks for replying.
But i have added components using only the setBounds method. I can do with layout managers also. But main problem is that i am unable to resize components on window resize.
Is it possible to achive component resizing without any layout managers

This my friend is one of the reasons for the existence of layout managers. For those of us who've used them, your problem is screaming for their use. I'm curious why you seemingly insist on not using the right tool for the job?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Only use one LayoutManager for each Container; if you set bounds, then the Components might not resize correctly and might overlap other Components which do resize.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Heres the code which i used for arranging the components. this is actually very easy to arrange components. Easier than the layouts like grouplayout and gridbag layout. the only problem i face is the resizing of the components.

Isn't there a way to solve my problem with just few lines of code because i have written much code already.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
adeeb alexander wrote:Heres the code which i used for arranging the components. this is actually very easy to arrange components. Easier than the layouts like grouplayout and gridbag layout. the only problem i face is the resizing of the components.

Perhaps, but much harder to adjust and enhance. For instance, what if you want to add another JTextField to your program?

Here's an example that goes against my previous rec in that it uses GridBagLayout, but I felt that I had to use this to accommodate the label you have between the JTextFields, the one that states Enter Date in....

Please run the and you'll see that it looks somewhat similar to your program:


Yes, it's a bit complex, and I'm sorry for that, but it was created in a bit of a rush. You'll notice that it is somewhat resizeable, though not very pretty when resized. This can all be tweaked.

The real power and beauty of layout managers comes when you want to extend your code. Say you'd like to add another textfield at the bottom that allows one to enter comments. In you're program, you'd have to change the location of all components below this field. In mine, all you'd have to do would be to add one String to a String array. You'd simply change this:


to this:


Try it and see.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks very much stein
I have learned a new thing now. But i will use GroupLayout as i am comfortable with it.
Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg:Component Resize