Reason for swing component overlapping?
Hello,
I am trying to make a GUI in Java AWT/Swing. I am having trouble pinpointing why my components overlap and hide each other. I am further paranoid after I grabbed a nice example file online, which worked fine, then made a marginal change and instantly started to get the same issue. @-)
Here is a neat online example I looked at:
Java Tips - How to align your components in horizontal or vertical layout
I then eddited thus:
Code :
// place panels on frame
c.add( boxes[ 0 ], BorderLayout.NORTH );
c.add( new JButton("myButton1"), BorderLayout.NORTH);
c.add( new JButton("myButton2"), BorderLayout.NORTH);
c.add( boxes[ 1 ], BorderLayout.EAST );
...
Original
http://www4.picturepush.com/photo/a/...40/6912552.jpg
Edited:
http://www5.picturepush.com/photo/a/...40/6912543.jpg
:-??
EDIT: of course, right after posting I notice the difference in code. Why must the components go into a "Box"? Shouldn't I be able to simply add components and have stack themselves next to each other in a FlowLayout kind of way?
Re: Reason for swing component overlapping?
Okay, when using BorderLayout any Components placed inside the 5 regions will expand to fill in weird and interesting ways. For example, when adding a textfield to WEST region it will accept the width parameter for textfield.setPrefferedSize - which by extension will become the width of that region, but ignore the height parameter, instead filling the entire height of whatever the Container for which you've defined BorderLayout is (or to the point where it meets SOUTH and NORTH regions, presumably).
Simply have to put in a Panel (or some other container) inside the region and then add the Components to this Container.