Re: setting size of elements



Andrew Thompson wrote:
On Sat, 27 Aug 2005 08:46:59 -0400, Frances wrote:


setSiz() is ignored...


In deference to preferredSize, mostly.


JButton btn = new JButton("OK");
 public void init() {
getContentPane().add(btn);
   btn.setSize(80,20);
 }

applet


(J)Applet..


..is width=200 height=100, and this is size button comes out.. if I put btn.setSize(80,20); _before_ getContentPane().add(btn); line it's also ignored...


Yes. The default layout of Swing root containers is BorderLayout,
and the default constraint for adding to a BorderLayout is BorderLayout.CENTER.



..pls, how do you control size of elements?


If you need to - it is usually a warning that something is wrong.

But..
1) By setting the preferredSize (and to be safe,
the minimum/maximum sizes.
2) Adding the component to a layout that respects the component's preferred size.


Have you done the layout tutorial and Swing tutorials mentioned in the GUI FAQ?


Andrew, I have read up on this stuff more than you think... I need to do my applet in swing b/c I need a SplitPane, which you can't do in AWT... I did manage to do a split pane, but even though I specify rows and columns for each TextArea in each ScrollPane inside the SplitPane, the top half comes out tiny (less than one row..) the button I have underneath is same size as SplitPanel.. I'm going a bit crazy here..
if you use GridLayout (rows and columns) can u tell it what to put in what rows and in what columns? if so I haven't found it yet...
the hierarchies are very confusing.. if you look at
http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html


they have a diagram there that includes a "Content Pane".. but ContentPane is just a variable, a viriable of JRootPane, I havew looked at a lot of tutorials and examples, I have never seen "JRootPane" inside an Applet...

From same page:
"Each program that uses Swing components has at least one top-level container. This top-level container is the root of a containment hierarchy — the hierarchy that contains all of the Swing components that appear inside the top-level container."


so: "one top-level container" -- this being what, exactly? a JFrame? do you need JFrame also if you're doing Applets? or does JApplet obviate need for JFrame?

(I'm used to working w/css, which I use heavily for layouts (divs), which I can do im my sleep now, I can control exact position of everything, and exact dimensions of everything, and everything always looks exactly as I want it to, in all browsers... so am having a bit of a hard time adjusting here....;)

if you look here
http://java.sun.com/docs/books/tutorial/uiswing/components/components.html

they make it look so easy, just three top-level containers into which you can put everything else.. but of course it's not that simple... :(

is it......???

once again, thank you for yr response...










.