This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes A challenge in components layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "A challenge in components layout" Watch "A challenge in components layout" New topic
Author

A challenge in components layout

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear All,
In my application I'm using GUI components inside a window & when I maximize the window , the GUI components does not respond to resizing the window.
What I should do ?
Any useful links or ideas or generally how they will solve this is suffice?
Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html

More suitable for Swing forum.
Moving.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Are you using any layout manager? If yes, which one?
If you have set the layout manager as null, and placed, the components using setBounds(), you need to switch to one of the many layout managers
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
First of all thanks maneesh for your understanding of the challenge.
I'm using flowlayout & adding components using setPreferredsize methods.
Note I'm using Jframe & Jpanel.
My panel is contained in JFrame & when I maximize the window the component is not resizing properly.

Consider




Since I'm working on a support project, I cannot change the classes i.e I mean JFrame & JPanel.
Regards



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Your problem is the flow layout which respects the preferred size.

What exactly are you trying to achieve? What is the reason for the preferred size?

A combination of BorderLayout and no explicitly defined preferred size usually gives the desired results.

As a side not, you should be calling getContentPane().setLayout() or getContentPane().add() instead of setLayout() directly for the JFrame.
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
JPanel is a Container too.. so you can use the class who extends JPanel you have and add it multiple JPanels to layout the frame.

On my applications i use the FormLayour from JGoodies.

It's not so hard to learn and i preffer it instead the code who Matisse Netbeans throw in my class


Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

True.
But that won't solve the OP's problem of resizing, as the parent will still have a FlowLayout and the child will be specifying the preferred size.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
A combination of BorderLayout and no explicitly defined preferred size usually gives the desired results.

That's correct maneesh & thanks for that.

In my case I'm a painting a rectangle bar along a line(using g.drawLine) inside the panel, whose size should also change when I do
mouse drag or mouse click, which is not happening now(which was not happening earlier also & this is the crux of the challenge) but the panel's size is resizing when I maximize the window(with your advice). Note my panel implements MouseListener & MouseMotionListener.


Now we need to find out how the bars(components) size can be resized dynamically once we resize the window or drag the window to a new size.

MyBestRegards


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

You are almost there.
Instead if hard coding the values (210), pick out the width of the panel at runtime by calling getWidth() in your paint method.

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Maneesh,
First of all I thank you for sharing your valuable expertise & understanding at this time.
You are almost there.
Instead if hard coding the values (210), pick out the width of the panel at runtime by calling getWidth() in your paint method.

So now this is my updated paint() method.


So after this, whenever I maximize or drag the window size, the width of the bar gets adjusted. That's part of my requirement & I thank you maneesh for this.

But still three challenges are there to surmount:

1) When I increase or decrease the width(by clicking & dragging the window right hand side), the width of the bar is re-sized properly. But when I increase or decrease the height(by clicking & dragging the window up), the width of the bar is not re-sizing.

2)The line along which the bar is positioned
In my case I'm a painting a rectangle bar along a line(using g.drawLine) inside the panel....

is not re-sizing when i resize the window. I mean I'm not able to find the end of the line when I re-size the window & I need to manually once again drag down the window to see other bars & end of this line.
Suppose when I reduce the size of the panel, by dragging up the window, this line & other bars are not aligning to the new size whereas the width is getting reduced.

3) I've a navigation rectangle which is white in color , which is used to cloud over the bars so as to help in navigation.
The code for which is written inside paint() method itself.

Consider there are some 12 bars totally. These navigation-rectangle will be implemented to cloud or cover over 6 bars at any time.
So when we are re-sizing ,the size of these navigation-rectangle should also change & they should be clouding or covering over the bars, which is not happening under current circumstances

As usual, eagerly looking forward for your valuable piece of information in harnessing this challenge.

MyBestRegards

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

1) You are increasing the height and expect the width to be affected? Is there a typo or a misunderstanding here?
2) Check out the API for Graphics#drawLine, especially the part what the 4 parameters mean ;)
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Maneesh,
Warm good morning & thanks for your reply.
I would like to clarify certain points you've made.
1) You are increasing the height and expect the width to be affected? Is there a typo or a misunderstanding here?


No there is no type or misunderstanding here maneesh.
What the challenge is
1)Initially there will be rectangle bars along a line in a panel in a default window size.
2) Now I'm reducing the size of the panel by clicking & dragging up the window. (through the window bottom )
3) Ideally the bar & the line, should understand that the height of the panel is reduced & should align itself in congruence with the new size of the panel which is not happening now!

To put it succinctly, when I re-size the window width, the bar & the line understands it & respond accordingly but whereas when I re-size the window height (suppose reduce the window height), the bar & line is not responding according to the new size.

Check out the API for Graphics#drawLine, especially the part what the 4 parameters mean ;)




so


will draw a line between the points(10,10) and (10,width) here width is the new panel width size.

Maneesh, I also tried with
where height=this.getHeight() of the panel but that keeps on increasing the height of the line when we increase or decrease the height.

Also we cannot do
as that would draw a slanting line between the width & height which we don't need.

How can we proceed further Maneesh?

MyBestRegards

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

I am getting confused.
Can you post your sscce code for the panel?
Also if possible, post an image of what you want the panel to look like.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Maneesh,
I've sent you a private message.
Regards
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I've sent you the mail maneesh & please acknowledge.
Regards
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Maneesh,
Now one of the challenge is surmounted.
Actually the requirement is
when we increase the panel width, the size of the bar ideally should not change(in which I confused myself & you. Sorry for that)

The barsize should change only when we re-size the height of the panel. So for that to happen I modified a previous line .



Now whenever I decrease or increase the height, the bar & the line gets aligned properly.

1) Now the other challenge to surmount is
When I'm maximizing the window height, the navigation-rectangle(white-color) cannot propagate further upto some default height i.e. it is not able to propagate over all the bars in this new height.
We need to enhance this code in paint method



I tried it out with

But no result.

Anyhow we'll see!

Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

1) Get your requirements straight
2) Post SSCCE code like I mentioned in my previous post
2) Post an image (drawn in MSPaint) or screen shot of what you want to achieve. The bar terminology is extremely confusing.

To draw some analogies:
A bar is horizontal. Text in a line is horizontal.
A pole is vertical. The line numbers in the code are vertical.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Post SSCCE code like I mentioned in my previous post


SSCCE code means the code-snippets right(after going through the link you've given)

Regards
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Sorry.
I cannot make it more easier than the link.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
It's ok Maneesh.
I'm able to manage.
Meanwhile thanks for your help upto this stage.
It was very useful.
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A challenge in components layout
 
Similar Threads
Resizing problem in Applet
My GUI.
swing doubt
Scroll around main JFrame?
public vs. private modifier