Link to home
Start Free TrialLog in
Avatar of Taurus
Taurus

asked on

New to Java; GUI?s

I'm new to Java and have a couple questions and RFC regarding laying out components.  I'm currently using JBuilder 3.5.  

My questions:

1)  What is the preferred method (or approach) for laying out components in a JFrame assuming one has a complex layout?  For clarity I'll offer a simple example:  

Suppose I want to construct a layout with four components: two JLables  and two JTextAreas.  Each JLable is to be followed by a JTextArea on its own line and centered similar to the following:
            JLable1  JtextArea1
            JLable2  JtextArea2

Hand coding this I could get something remotely similar to this by creating a content pane with a 2X1 GridLayout, insert two JPanels and add JLable1/JtextArea1 to JPanel1 and add JLable2/JtextArea2 to JPanel2. However the line spacing is going to change if the user sizes the JFrame.  Not ideal.  

2) JBuilder doesn't facilitate (in design mode) creating a content pane and putting two pannels inside it.  Rather it always creates the first panel then puts all subsequent panels inside the first.  I'm wondering if there is a reason for this?

RFC:
It doesn't seem to me that Swing has any easy method to layout  components having positional & aspect integrity that is independent of scale.  It seems what is needed is a vector based approach similar to that which Macromedia flash uses.

Finally I'm interested in finding someone who knows JBuilder to tutor me remotely via real time web meeting/desktop sharing.  Please add comment if interested.
Avatar of Nevyn
Nevyn

Use 2 GridLayouts(0, 1) and a BorderLayout.
Place the 2 JLabels into one GridLayout and the 2 JTextFields into the other.
Add the labels layout to the BorderLayout in the West position and the text fields layout to the Center position.
To keep the fields at reasonable heights, add the BorderLayout to another BorderLayout in the North position.

JPanel base = new JPanel( new BorderLayout() );
JPanel p = new JPanel( new GridLayout(0, 1) );
p.add( label1 );
p.add( label2 );
base.add(p, "West");
p = new JPanel( new GridLayout(0, 1) );
p.add( field1 );
p.add( field2 );
base.add(p, "Center");
with JBuilder you have the option of using an free layout
similar to the one on VC++

The thing is you set the Layout to null

ie. getContentPane().setLayout(null);

It is one of the options in design view set Layout to null
and you can freely place your controls where ever you like

alternatively you can use setBounds(x,y,w,h) and place the components i.e

lbl.setBounds(0,0,10,10);

getContentPane().add(lbl, null);

If you are using JBuilder change the layout to null and off you go
ASKER CERTIFIED SOLUTION
Avatar of Vernstump
Vernstump

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Taurus,

PLEASE UPDATE AND FINALIZE all your open questions.  I will be adding this comment to them so that you will receive Email notification of them all.

If more is needed, please advise the experts; if you need my help to split points or take some other action, just comment and I'll return as soon as I can.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
Thank you for returning and updating/finalizing your questions, it is appreciated.  I'd like to ask if you've assigned "B" grades because of the quality of the information or just because that is the default setting?  I've been asking our Engineering department to consider adjusting this.  If you feel any of the "B" grades you've given were actually "A" level responses, just comment and I'll correct it.

The WHAT'S NEW link on the left has been updated to reflect some of the transitions we're undergoing, and will be updated again shortly, so highly recommend checking that for updates.

Moondancer
Community Support Moderator @ Experts Exchange