Before anything, I am a newbie in Java (I worked in C++)
I want to set the size and pos of Dialog Window
and then
I want to set the size and pos of each JComonent inside Dialog Window
Is this so difficult? Why I must shoot my head with Layouts?
Layouts are friently but not with many objects with different sizes and
different positions.
I only want a Dialog (this) with Pos 100, 150 and Size 500, 400
and then I want a JComponent (AboutBox_Image) with Pos 20, 30 and Size 100,
200 inside Dialog
Borland J Builder gives a library with XYLayout class but it's not part of
original Java.
Why the code below does not work?
-------------------
AboutBox is JPanel
this is JDialog
-------------------
AboutBox_Image.setSize(100, 200);
AboutBox_Image.setLocation(20, 30);
AboutBox.add(AboutBox_Image);
AboutBox.setLocation(0, 0);
AboutBox.setSize(500, 400);
this.getContentPane().add(AboutBox);
this.setSize(500,400);
--------------------
Chris Smith - 11 Jul 2004 14:35 GMT
> Before anything, I am a newbie in Java (I worked in C++)
>
> I want to set the size and pos of Dialog Window
> and then I want to set the size and pos of each JComonent
> inside Dialog Window
I'll give you an answer below. For the moment, though, consider very
hard whether you really want to do this. When you do this, you are
assuming that your controls will always be the same size that they were
on the platform where you first designed the GUI. There are a number of
reasons that may not be the case:
1. People may have increased default font sizes for accessibility
reasons.
2. Your program may have been localized to a different language, and
the new text on buttons is too long to fit in the space you left.
3. The program may be run on a different operating system that does
some GUI thing differently, such as a different-sized title bar
on the window, or different font sizes.
4. Someone may resize the window. (Or if you prevent resizing the
window, one of the people who curse your name as a result might
end up as a maintenance programmer on the project and decide to
fix your mistake.)
> Is this so difficult? Why I must shoot my head with Layouts?
It's not naively difficult, no. The part you are missing is that you
need to get rid of the default layout managers first.
AboutBox.setLayout(null);
getContentPane().setLayout(null);
You'll need to do that to any container that you add components to, if
you plan to manually set their location and size. Incidentally, you
might find setBounds a little more convenient for setting location and
size at once.
> AboutBox_Image.setSize(100, 200);
> AboutBox_Image.setLocation(20, 30);
> AboutBox.add(AboutBox_Image);
One more comment. Your own code is your business, but when you're
posting to newsgroups it is far less confusing if you use standard Java
naming conventions. Reading code like the above can be difficult
without context, just because 99% of your readers will assume that
anything called AboutBox or AboutBox_Image must be a class, not a
variable.
The universal naming conventions for Java say:
Classes: capitalized, intermediate caps, no underscores
MyClassName
Methods: camel caps, no underscores
myMethodName
Fields: camel caps, no underscores
myFieldName
Constant Fields: All caps, intermediate underscores
MY_CONSTANT_NAME
Local variables: not capitalized, uses vary between camel caps and
underscores, with strong bias to camel caps
myLocalVariable (or rarely, my_local_variable)

Signature
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Thomas Weidenfeller - 12 Jul 2004 08:35 GMT
> Before anything, I am a newbie in Java (I worked in C++)
I highly suggest you stop comparing Java with C++ (which doesn't even
have a standard GUI toolkit) and instead start learning the language and
then principles behind it. E.g. Sun's GUI tutorial explains why there is
a need for layout managers, and so does this group's FAQ, and many
previous discussions - which you could have looked up in an archive of
this group.
> I want to set the size and pos of Dialog Window
> and then
> I want to set the size and pos of each JComonent inside Dialog Window
In short, you want to write non-portable GUIs. Go ahead. Do it, but
please don't come back here later and whine about your GUI not working
on another OS or VM.
> Why I must shoot my head with Layouts?
You must not. You are free to write non-portable GUIs all the way you
want. I don't see a reason why you need Java for this, but go ahead.
/Thomas
Tor Iver Wilhelmsen - 12 Jul 2004 09:21 GMT
> Is this so difficult? Why I must shoot my head with Layouts?
yourContainer.setLayout(null);
> Borland J Builder gives a library with XYLayout class but it's not part of
> original Java.
No, but it was an intermediate "hack" way back when because they
didn't support null layouts in the designer at the time. Now they do.