Placing a button inside a panel with specific location

E

eshedz

....is that even possible?

I mean, if I do this (inside a class that extends Frame):

Panel p = new Panel();
Button b = new Button("Caption");
p.add(b);
this.add(p);

It places the button at the middle of the top of the window.
Can't I move it to anywhere I'd like?
 
C

Chris Smith

...is that even possible?

I mean, if I do this (inside a class that extends Frame):

Panel p = new Panel();
Button b = new Button("Caption");
p.add(b);
this.add(p);

It places the button at the middle of the top of the window.
Can't I move it to anywhere I'd like?

Look intop layout managers, which provide a variety of ways of layout
out components. The default layout for a JPanel is FlowLayout, but you
can change that by calling p.setLayout with a new instance of some
subclass of LayoutManager.

Despite thinking you want this, you almost certainly really don't want
to put your components at some fixed location. If you did that, then
you would run into problems when you translate the application to a
different language where the label is longer, or someone runs it on a
box with a larger default font size (because they are sight-impaired),
etc. Instead, layout managers allow you to lay things out logically,
and handles the pixel-perfect layout on its own.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
V

Vova Reznik

...is that even possible?

I mean, if I do this (inside a class that extends Frame):

Panel p = new Panel();

Panel p = new Panel(null);
or
p.setLayout(null);
Button b = new Button("Caption");
p.add(b);
this.add(p);

Default layout for JPanel is FlowLayout.
 
J

jan V

...is that even possible?

If you use an IDE, this will undoubtedly feature a GUI designer which lets
you use absolute component positioning. Netbeans, for example, allows this.
Though like other posters have pointed out, the normal route Java
programmers take when designing GUI layouts is to use "rule-based" layouts
which produce reasonable results on all supported Java host platforms. Your
absolute positioning may cause problems on certain platforms because of font
or other differences.
 
J

Joan

...is that even possible?

I used to do it all the time and it is simple, just set the
layout
manager to null and place the object using x and y. What you
will probably find is that as changes are made for maintanence
or minor feature tweaks you will have to come back to this
piece of code over and over.
 
E

eshedz

Extremely helpful guys :)

But maybe I should describe why I needed custom button positioning.

I want to create a simple checkers game (as a start with java graphics
and stuff) and the first step was creating the background and the
"exit" button (although the [x] button works fine). So I wanted to
place the "exit" button in a specific location. That's all.

But if anyone has another idea, go ahead :)
My intention is to draw a checkers (black and white) background with
Graphics and then place buttons or link shapes i'll create to actions.

I've created a window that extends Frame, inside I put a panel that
extends Panel, and in that panel I drew the Graphics. When I add a
button to the panel (outside the panel class) it adds a button that
floats in the middle of the top area.

Ideas? :D

Thanks btw.
 
Joined
Oct 12, 2007
Messages
1
Reaction score
0
"Panel p = new Panel(null);
or
p.setLayout(null);"

I have been trying to put a button in a different position however when i left the default layout on the panel the button didn't move. On the other hand when i tried to make it null, the button vanished...any idea y that happened or how to solve it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top