Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

setting size of elements

Thread view: 
Frances - 27 Aug 2005 13:45 GMT
setSiz() is ignored...

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

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... pls, how do you control size of elements?  thank you..
Andrew Thompson - 27 Aug 2005 14:04 GMT
> setSiz() is ignored...

In deference to preferredSize, mostly.

> JButton btn = new JButton("OK");
>   public void init() {
[quoted text clipped - 3 lines]
>
> 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?

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"You know you done me wrong baby, and you'll be sorry someday."
B.B.King 'The Thrill Is Gone'

Frances - 27 Aug 2005 16:21 GMT
>>setSiz() is ignored...
>
[quoted text clipped - 30 lines]
> 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...
Andrew Thompson - 27 Aug 2005 17:00 GMT
>>>setSiz() is ignored...
>>
>> In deference to preferredSize, mostly.
..
>>>getContentPane().add(btn);
>>>    btn.setSize(80,20);
...
>> Yes.  The default layout of Swing root containers is BorderLayout,
..
...
>> Have you done the layout tutorial and Swing tutorials
>> mentioned in the GUI FAQ?
>
> ...I have read up on this stuff more than you think...  

Which leads to the next question.  What in those tutorials
is unclear?  Specifically, and one thing at a time..

>..I need to do my applet in swing b/c I need a SplitPane,
> which you can't do in AWT...  

AWT does not provide a JSplitPane, no.

My point was not about AWT/Swing, it was about applet/JApplet,
it was about split SplitPane/JSplitPane*.  In the specific
instance your were mentioning, the default behaviour of an
Applet and JApplet is quite different.  For example, they
have different default layouts.

So, please avoid old habits and instead be *specific*,
and *exact*.

* And there is no such class as a SplitPane.  Why
do you make the reader guess what you are doing?
Do you think that makes it more 'fun' for them?

>..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..  

OK.. Where is your SSCCE?  

> I'm going a bit crazy here..

Yes.  I noticed.

Again you are tending to pour out a great long series
of breathless questions.  

I snipped them.

It seems you have some basic misunderstandings about
how to use layouts, and we need to sort them one at
a time.  

However, I will give you one more tip this post.

> 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...

It depends purely on the *order* in which the components
are added.  Try adding 16 (20 ..50) labels with text
"Label nn" to a GridLayout one at a time and it will
become very obvious.

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Where I'm going next, I'm goin' alone.."
Paul Kelly 'Deeper Water'

Frances - 27 Aug 2005 22:52 GMT
>>>>setSiz() is ignored...
>>>
[quoted text clipped - 67 lines]
> "Label nn" to a GridLayout one at a time and it will
> become very obvious.

I have, Andrew, I have..  I have sat down and copied A LOT of code from
books (for example (all examples SSCCE)
http://www.francesdelrio.com/java/ShowGridLayout.java)
and many more (have more examples if you don't believe me...;)
have tried converting some of them to JApplets..
(for example, http://www.francesdelrio.com/java/ShowGridLayoutA.java,
compiles fine, but get "applet not initialized" error... and these
errors in console:

hjava.lang.ClassCastException
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:617)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:546)
        at sun.applet.AppletPanel.run(AppletPanel.java:298)
        at java.lang.Thread.run(Thread.java:534)

(Thread error????)

also, elements change size as you resize window..  I don't like that..
I would like to be able to control dimensions of elements and for them
to not change dimensions if you change window size.. but oh well...

I have done A LOT of stuff from books, they all look great, when I run
into problems is when I try to adapt them to my own needs...  ;)

basically I want a swing version of this:

http://www.francesdelrio.com/java/win.html
(code: http://www.francesdelrio.com/java/win.java)
with the JTextAreas in a JSplitPane.. and with JTextAreas exact sizes as
they appear here when user opens this applet..
(yes of course I meant JSplitPane, not SplitPane, of course there's no
such thing as SplitPane...

also, I DO know default layout for Swing is different.. (BorderLayout..
 again, I HAVE been reading up a lot of stuff..)

btw, I love Duke doing cartwheels in applet here..
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html..
it's so neat...

when I say I don't understand something it's not b/c I'm too lazy to
look it up (if you're lazy you have no business trying to become a
programmer..)  but b/c I don't understand some of the explanations..

here..
http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html#contentpane

it says:
"Here's the code that the preceding example uses to get a frame's
content pane and add the yellow label to it:

frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);   "

'frame' refers to what exactly?  JApplet?  or do I create a JFrame? a
JRootPane?

ok, have more questions (for example re event-dispatching threads..),
but, like you say.. I ask too many questions at once..)
Andrew Thompson - 28 Aug 2005 07:38 GMT
...
> basically I want a swing version of this:
>
> http://www.francesdelrio.com/java/win.html
> (code: http://www.francesdelrio.com/java/win.java)

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Win extends JApplet implements ActionListener {
    JTextArea ta;
    JTextArea ta2;
    JButton btn;
 FlowLayout layout = new FlowLayout(FlowLayout.LEFT);

         public void init() {

    ta = new JTextArea("", 14, 35);
    ta2 = new JTextArea("", 6, 35);
    btn = new JButton("SEND");
    //ta.setSize(430,80);
    //ta2.setSize(430,50);
    Container c = getContentPane();
   c.setLayout(layout);
    c.add(new JScrollPane(ta, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ));
    c.add(new JScrollPane(ta2, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
    c.add(btn);
    //btn.setSize(80,20);
    btn.addActionListener(this);
        }

    public void sendMsg() {
   String msg = ta2.getText();
   //System.out.println(msg);
    ta.append(msg + "\n");
    ta2.setText("");
//   ta.transferFocus();
    ta2.requestFocus();
        }

    public void actionPerformed(ActionEvent ae) {
        sendMsg();
        }

    }
</sscce>

> with the JTextAreas in a JSplitPane..

Put the JScrollPanes in a JSplitPane.

> ...and with JTextAreas exact sizes

Not practical.  Java Layouts are intended to give your
UI a logical layout across a variety of PLAFS (including
the tweaks that Sun might do to the default PLAF's
from release to release) using a number of screen
resolutions/ fonts...

Java GUI design is not geared towards giving pixel perfect
representations of UI components, but instead presenting them
at the exact size the end user requires.

Your attempts to create pixel perfect layouts are a
pointless waste of time, and they may be what a developer
wants, but they are rarely what the end user wants.

> when I say I don't understand something it's not b/c I'm too lazy to
> look it up (if you're lazy you have no business trying to become a
> programmer..)  but b/c I don't understand some of the explanations..

You should stop at each one and ask.  You end up
a) wasting too much time trying to force Java layouts to
fit your misconceptions.
b) Tying yourself up in 'code knots'.

> here..
> http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html#contentpane
[quoted text clipped - 7 lines]
> 'frame' refers to what exactly?  JApplet?  or do I create a JFrame? a
> JRootPane?

???  What would make you think that example that mentions 'frame'
would be referring to an 'applet'?

The process of adding things to a Swing content pane is the
same for both, but I thought that would have been pretty clear
they were referring to a JFrame in that example.  (and that
is based purely on the snippets you quoted!)

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Watch TV and drink your beer."
Charles Manson 'Sick City'

Frances - 28 Aug 2005 12:29 GMT
> ...
>
[quoted text clipped - 96 lines]
> they were referring to a JFrame in that example.  (and that
> is based purely on the snippets you quoted!)

Andrew thank you so much!!!!!  :)  that was perfect!!

(I see that all the ActionListener stuff is the same for Swing as for
AWT...  (I thought you weren't supposed to mix AWT and Swing...;)
(I guess goes only for visual elements....)

again, many thanks..  Frances
Andrew Thompson - 28 Aug 2005 13:46 GMT
> (I see that all the ActionListener stuff is the same for Swing as for
> AWT...  (I thought you weren't supposed to mix AWT and Swing...;)

I hate that statement.  At least that statement unqualified.
- Every Swing component extends an AWT component
- The vast majority of layouts are AWT
- Keyboard, Window, Mouse, Action.. events all
   originate from the AWT
- D'n'D is all in the AWT..

Learners get the impression that they can ignore all
of the AWT if they are working with Swing, but you
just need to look at the imports of most Swing code
to realise how much of 'Swing' programming requires
a broad knowledge of the AWT.

> (I guess goes only for visual elements....)

Yep.

> ..again, many thanks..  Frances

No worries.   :-)

Signature

Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"I have to turn my head, until my darkness goes"
The Rolling Stones 'Paint It Black'



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.