Getting proper size of the window component

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting proper size of the window component

Piotr Kołaczkowski-2
Hi,

How to get the size of a window or frame?
Should be trivial - just use getSize or getWidth or getHeight. Actually
it is not :(

class MainWindow extends Window {

    void openSubFrame() {
       Frame frame = .... // load frame from BXML or whatever
       int width = frame.getWidth();   // returns 0
       frame.open(this);
       width = frame.getWidth();  // still 0
    }
}


After some time, when the frame is rendered on the screen, the getWidth
returns the actual width.
So, how to get the size of the frame before it is displayed? I want to
apply some custom positioning of frames and I have to know their size.

Regards,
Piotr


--
Piotr Kołaczkowski
senior consultant
Sages Sp. z o.o.
ul. Wąwozowa 11
02-796 Warszawa
t: +48 22 2035600
f: +48 22 2035601
m: +48 609618449

Reply | Threaded
Open this post in threaded view
|

Re: Getting proper size of the window component

gk_brown@verizon.net
The size is 0 because the window has not been laid out yet. Can you perform your logic based on the preferred size of the window? That is generally the size it will be given when layout occurs.

On Dec 4, 2011, at 7:02 AM, Piotr Kołaczkowski <[hidden email]> wrote:

> Hi,
>
> How to get the size of a window or frame?
> Should be trivial - just use getSize or getWidth or getHeight. Actually it is not :(
>
> class MainWindow extends Window {
>
>   void openSubFrame() {
>      Frame frame = .... // load frame from BXML or whatever
>      int width = frame.getWidth();   // returns 0
>      frame.open(this);
>      width = frame.getWidth();  // still 0
>   }
> }
>
>
> After some time, when the frame is rendered on the screen, the getWidth returns the actual width.
> So, how to get the size of the frame before it is displayed? I want to apply some custom positioning of frames and I have to know their size.
>
> Regards,
> Piotr
>
>
> --
> Piotr Kołaczkowski
> senior consultant
> Sages Sp. z o.o.
> ul. Wąwozowa 11
> 02-796 Warszawa
> t: +48 22 2035600
> f: +48 22 2035601
> m: +48 609618449
>
Reply | Threaded
Open this post in threaded view
|

Re: Getting proper size of the window component

Piotr Kołaczkowski-2
W dniu 2011-12-04 14:05, Greg Brown pisze:
> The size is 0 because the window has not been laid out yet. Can you perform your logic based on the preferred size of the window? That is generally the size it will be given when layout occurs.

Some of the windows don't have their preferred size set - the size
depends on their contents.
Can I trust getPreferredWidth will always return the width that will be
given to the window in such cases?

Regards,
Piotr


--
Piotr Kołaczkowski
senior consultant
Sages Sp. z o.o.
ul. Wąwozowa 11
02-796 Warszawa
t: +48 22 2035600
f: +48 22 2035601
m: +48 609618449

Reply | Threaded
Open this post in threaded view
|

Re: Getting proper size of the window component

gk_brown@verizon.net
> Some of the windows don't have their preferred size set - the size depends on their contents.

A component does not require a caller to set a preferred size. All components have a default preferred size. setPreferredWidth() and setPreferredHeight() allow a caller to override this default size.

> Can I trust getPreferredWidth will always return the width that will be given to the window in such cases?

It has been a while since I looked at that code, but I believe that you can reasonably make this assumption.

G

Reply | Threaded
Open this post in threaded view
|

Re: Getting proper size of the window component

JLEIngenieros
Hello, Javier from Seville (Spain). It's my first post, and at first, apologize for my english.

I understand that the post is quite old, but i think that this response can help to other users:

Recently, I had the same problem, and I thought in a timer. Here is the result:


        //notice: miPanel is an ImageViewer
        //notice: import java.util.Timer;import java.util.TimerTask;

        //Loading the image, via exception (null, etc.)
        try{
            miPanel.setImage("blah/blah../Sample.jpg");
        } catch(Exception ex) {
           
        } finally {
            //Waiting for finish loading
            //
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run()
                {                    
                    System.out.println(miPanel.getParent().getWidth());
                           //Here you can write if...then, per sample:
                          if(miPanel.getParent().getWidth()!=0) {  
                                   timer.cancel();
                                   other methods...
                                   //This is only for ensure:
                                   System.out.println(miPanel.getParent().getWidth());
                          }
                }
             };    

            // Here, the timer:
           Timer timer = new Timer();
           // in 0 milliseconds look at run each 1000 milliseconds
           timer.scheduleAtFixedRate(timerTask, 0, 1000);              

Hope this helps
Regards
Javier