Anyone have experience with managing layout in swings in Java?? Need help.

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Allright, here's the background.

I'm making a relatively simple proxy server in Java and am currently making a GUI using swings. Now I have 3 tabs, and each tab has its own panel with its own components (text box, buttons, etc..).

Right now I'm using GridLayout to manage the layout of those individual panels. However the layout looks like crap. All the buttons/text fields fill up the entire panel. I can't seem to figure out how to correctly position them.

I also have a whole host of other problems, but this one is pissing me off right now.

Anyone know how to fix this?

Thanks

<btw I would have posted this in the programming forum, but it doesn't generate nearly the traffic this place does>
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
use combinations of layouts. eg in each panel you can use a border layout in which you put a couple of panels that use box layouts, etc... it's the best way to go about it even though you end up with a ton of panels.

:)
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
if you have 3 major areas you might want to use a grid layout and put a panel in each.

But for the actual layout of buttons and textfields and labels I've found it best to use NullLayout and just use the setBounds(X,Y,Widght,Height) method of the components.

Otherwise the components expand to fill the whole grid square.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
Originally posted by: Noriaki
if you have 3 major areas you might want to use a grid layout and put a panel in each.

But for the actual layout of buttons and textfields and labels I've found it best to use NullLayout and just use the setBounds(X,Y,Widght,Height) method of the components.

Otherwise the components expand to fill the whole grid square.


Did not know about NullLayout...thanks. Will try that. I wish JBUilding woud let me do absolute position of the items

 

manly

Lifer
Jan 25, 2000
13,462
4,170
136
No, he means null (the reference) for a layout manager, i.e. setLayout(null). JBuilder probably supports absolute positioning with no (the null) layout manager.

I more or less disagree with absolute positioning though; that's gone the way of the dinosaur. In both thick apps (client-side) and thin-apps (web-based), you should never assume any particular screen sizes.

The Java layout manager is a flexible, convenient design for solving that problem, but it does take some getting used to. As
RaynorWolfcastle said, you'll need to use a hierarchy of nested panels. For applications, you'll probably want to use BorderLayout or BoxLayout for the top-level JFrame. GridLayout is much less useful; for example, it's reserved for things like painting a spreadsheet.
 

mAdD INDIAN

Diamond Member
Oct 11, 1999
7,804
1
0
I got it to wokr..thanks for the help guys!

I realize absolute positioning is bad, but this program is a proxy server and I am going to make it so you cannot resize the window (how do you do that?).

I actually did not want to make a GUI for this, but its a final project for my course and seeing how things like GUI bring up the marks, that's why I'm doing it. Otherwise a proxy server is a service that should run in the background wihtout a GUI.
 

Noriaki

Lifer
Jun 3, 2000
13,640
1
71
Yes..sorry it's not a layout manager called NullLayout...setLayoutManager(null)
I dunno what I was thinking.

And manly makes a very good point if you are designing an application to be used on mutliple machines then it's a good idea to use non-fixed layouts.

However, it seems like you are writing a one off application to be used on a single machine.

Still if you want to follow good software design practices you should careful design things so that they aren't fixed.

But it's just such a pain in the ass to get things to look nice with non fixed.

Even Sun's IDE for Swing GUIs use fixed positiong.