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 / December 2003

Tip: Looking for answers? Try searching our database.

BorderLayout.CENTER: get size?

Thread view: 
Huub - 05 Dec 2003 18:10 GMT
Hi,

I created an interface with BorderLayout.CENTER and used g.drawString to
 put info in it. However, on different computers, different OS'es, the
interface itself is ok (using getScreenSize), but the filling of CENTER
is screwed up. Is there any way to measure the size of CENTER so I can
get the filling ok?

Thanks,

Huub
Chris Smith - 05 Dec 2003 19:32 GMT
> I created an interface with BorderLayout.CENTER and used g.drawString to
>   put info in it. However, on different computers, different OS'es, the
> interface itself is ok (using getScreenSize), but the filling of CENTER
> is screwed up. Is there any way to measure the size of CENTER so I can
> get the filling ok?

Call getWidth and getHeight on the component that you've added there.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Huub - 05 Dec 2003 22:51 GMT
>>I created an interface with BorderLayout.CENTER and used g.drawString to
>>  put info in it. However, on different computers, different OS'es, the
[quoted text clipped - 3 lines]
>
> Call getWidth and getHeight on the component that you've added there.

Do you mean: g.getHeight(), since I used g.drawString to place the info?
Chris Smith - 05 Dec 2003 21:43 GMT
> >>I created an interface with BorderLayout.CENTER and used g.drawString to
> >>  put info in it. However, on different computers, different OS'es, the
[quoted text clipped - 5 lines]
> >
> Do you mean: g.getHeight(), since I used g.drawString to place the info?

Presumably, you intend for 'g' to be a Graphics object.  Since Graphics
has no getWidth or getHeight methods, you can't do that.  You wanted the
size of the component you're drawing on, so asking that Component for
its width and height solves your problem.  It doesn't matter what you
may have used to draw text onto the component, as this won't affect the
size of the component at all.

Or am I not understanding the question?

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Huub - 06 Dec 2003 08:31 GMT
>>>>I created an interface with BorderLayout.CENTER and used g.drawString to
>>>> put info in it. However, on different computers, different OS'es, the
[quoted text clipped - 14 lines]
>
> Or am I not understanding the question?

Yes, you understand the question. These are 2 snippets of code I use:
>       center = new Scherm();
>       container.add(center, BorderLayout.CENTER);
>       center.repaint();

and
>   class Scherm extends JPanel
>   {
[quoted text clipped - 9 lines]
>           else if (scherm == 2)
>    
As I understand I have to add .getWidth and .getHeight to center. so it
gets like:

int col_l = center.getWidth() / 50;

I just get an exception-error with this, so I guess I do it wrong.
Chris Smith - 06 Dec 2003 14:36 GMT
> Yes, you understand the question. These are 2 snippets of code I use:
> >       center = new Scherm();
[quoted text clipped - 14 lines]
> >           }
> >           else if (scherm == 2)

> As I understand I have to add .getWidth and .getHeight to center. so it
> gets like:
>
> int col_l = center.getWidth() / 50;

Something like that, yeah.  It really depends on where you want this
information.  You've given a line of code above, but not told me where
it's supposed to go.  Is it used in Scherm's paintComponent method?  Or
somewhere else?  If you're getting an exception from that line of code,
then that's almost certainly because the reference 'center' is null when
it's executed... that is, you haven't created the Scherm yet, or you
didn't store it in that reference.  If you're getting the exception on a
future line, it could be a lot of things.

(Incidentally, that center.repaint at the end shouldnot be necessary.  
Simply displaying it will cause it to be painted.)

> I just get an exception-error with this, so I guess I do it wrong.

So please post the exception message.  That's important information
given to you by the compiler about what's going wrong, and it's rather
important to know when trying to diagnose your problem.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Huub - 06 Dec 2003 19:39 GMT
>>Yes, you understand the question. These are 2 snippets of code I use:
>>
[quoted text clipped - 39 lines]
> given to you by the compiler about what's going wrong, and it's rather
> important to know when trying to diagnose your problem.

In global declaration I put:
>   Scherm center;
>   int col_l = center.getWidth() / 50;

The 2nd line is instead of this line:
>   int col_l = 75;

The 1st errormessage is for the second line.
> Exception in thread "main" java.lang.NullPointerException
>         at Standaardscherm.<init>(Standaardscherm.java:24)
>         at Standaardscherm.main(Standaardscherm.java:394)

The 2nd message is for the line where the constructor is being created:
> Standaardscherm frame = new Standaardscherm();

Hope it's not getting messy..
Chris Smith - 06 Dec 2003 18:02 GMT
> In global declaration I put:
> >   Scherm center;
> >   int col_l = center.getWidth() / 50;

That's your problem, then.  At that point, there is no Scherm at all.  
All you've done is declare a reference variable that is capable of
keeping track of a Scherm in the future.  You can't do anything through
that variable until you give it a Scherm object to keep track of.

You earlier posted the following code, which does exactly that:

> >>>      center = new Scherm();

So what you need to do, first of all, is make sure that the
"center.getWidth()" call doesn't happen until after the line directly
above.  The NullPointerException means exactly that: you're attempting
to communicate with the object that the 'center' variable is referring
to (or in other words, keeping track of), but 'center' isn't referring
to any object yet.

Unfortunately, that's not all.  When you first create a Scherm, it will
have a size of zero by zero pixels.  It won't be resized to be larger
until after it added to some container, and that container has the
chance to lay it out.  That requires either a setSize or pack method
call on the container it's added to.  So you don't want to ask for the
size the instant you create an object, either, because that size will
just be zero.

It's really best to just wait until you actually *need* the size, and
figure it out then, rather than trying to ask once and store the value
away somewhere.  If you wait until the last possible minute to get the
information, then as long as what you're trying to do makes some
rudimentary sense in the first place, the information you need will be
available.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Huub - 06 Dec 2003 22:23 GMT
>>In global declaration I put:
>>
[quoted text clipped - 31 lines]
> rudimentary sense in the first place, the information you need will be
> available.

Thank you very much. Using center.getWidth() and center.getHeight() as
the (x,y)-coordinates now. Working ok now.


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



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