? dimensions of a JPanel before it's first painted?

Discussion in 'Java' started by TL, Jul 27, 2004.

  1. TL

    TL Guest

    Hi,
    How can I get the dimensions of a JPanel before it's first painted the
    first time?

    I.e, I have a Form (JDialog or JFrame) which uses a BorderLayout, and
    has 2 JPanels on it. The first JPanel has its height computed at
    runtime (eg 90 pixels), and the other takes up the remainder of the
    Form. I call getBounds() on the second JPanel (the last thing in the
    constructor) and it returns me width,height=0,0. However if I call
    getBounds() in the Forms paint method, it gives me 1024,631 (which is
    what I want to know in the constructor).

    So basically it seems that the dimensions of the second JPanel are not
    set until the Form is rendered and painted for the first time.
    I want to use getBounds because it takes into account not only my
    first JPanel, but also the Forms title bar, windows taskbar (if
    visible), etc. (which are also dependent on the active look & feel),
    so saves me lots of work.

    How do I do this...?
    TIA
    TL, Jul 27, 2004
    #1
    1. Advertising

  2. On 26 Jul 2004 18:03:34 -0700, TL wrote:

    > So basically it seems that the dimensions of the second JPanel ..


    <http://www.physci.org/codes/javafaq.jsp#cljg>

    >are not
    > set until the Form is rendered and painted for the first time.


    setVisible(true) is the critical moment
    that the entire process is called.

    > I want to use getBounds


    For what, when, why?

    >..because it takes into account not only my
    > first JPanel, but also the Forms title bar, windows taskbar (if
    > visible), etc. (which are also dependent on the active look & feel),
    > so saves me lots of work.


    So get the bounds after you JFrame
    is set visible.

    It sounds like you migh be doing some odd
    things in your code, but the only way to tell
    is to see a complet (*short*) exmaple..
    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jul 27, 2004
    #2
    1. Advertising

  3. TL

    TL Guest

    Andrew Thompson <> wrote in message news:<1xfrws6huvwnn.1o47co3kum3hg$>...
    > On 26 Jul 2004 18:03:34 -0700, TL wrote:
    >
    > > So basically it seems that the dimensions of the second JPanel ..

    >
    > <http://www.physci.org/codes/javafaq.jsp#cljg>
    >
    > >are not
    > > set until the Form is rendered and painted for the first time.

    >
    > setVisible(true) is the critical moment
    > that the entire process is called.

    Thanks, will explore that area

    > > I want to use getBounds

    >
    > For what, when, why?

    A good question that I was actually asking myself. I've written (for a
    new project) several base class 'Forms' (based on JDialog and JFrame
    classes) that have several standard controls and a general-purpose
    JPanel (on which subclasses can put controls). A couple of developers
    are already extending them to build the forms used in the program. In
    one of them the developer has used the size of the JPanel to influence
    the size of some of the controls.
    I asked myself the same question - is this necessary since the forms
    are mostly using well-behaved scaleable grid- border- box- layouts,
    etc. However I still want to ge able to provide the JPanel size
    accurately (should we ever need it).

    > >..because it takes into account not only my
    > > first JPanel, but also the Forms title bar, windows taskbar (if
    > > visible), etc. (which are also dependent on the active look & feel),
    > > so saves me lots of work.

    >
    > So get the bounds after you JFrame
    > is set visible.

    But that's ugly - don't want the Form to flash up on the screen and
    then instantly get redrawn because we're modifying the layout straight
    away. That's the reason to getBounds during construction, so we don't
    get that horrible flicker (even if it is only when first displayed).
    Actually that reminds me of another issue (will post another msg)

    > It sounds like you migh be doing some odd
    > things in your code

    if it's not odd it's no fun ;-)
    TL, Jul 27, 2004
    #3
  4. On 27 Jul 2004 03:36:11 -0700, TL wrote:

    >>> I want to use getBounds

    >>
    >> For what, when, why?

    ....
    > A good question that I was actually asking myself. I've written (for a
    > new project) several base class 'Forms' (based on JDialog and JFrame
    > classes) that have several standard controls and a general-purpose
    > JPanel (on which subclasses can put controls). A couple of developers
    > are already extending them to build the forms used in the program. In
    > one of them the developer has used the size of the JPanel to influence
    > the size of some of the controls.


    I *think* this nmight be one of those "don't
    ever do anything that stupid" type of things,
    but I am too tired to know right now (and I
    am hoping there are some GUI masters out there
    might jump in any time..)

    If you need to pursue it much furher I would
    advise you take it out of the hands of these
    ...hacks and take it over to c.l.j.gui, where
    the GUI folks actually hang out ( ducks quickly
    and starts loping away.. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jul 27, 2004
    #4
  5. TL

    TL Guest

    Andrew Thompson <> wrote in message news:<1v42snzxex1oe$.1uwjk48vp7h6f$>...
    > On 27 Jul 2004 03:36:11 -0700, TL wrote:
    >
    > >>> I want to use getBounds
    > >>
    > >> For what, when, why?

    > ...
    > > A good question that I was actually asking myself. I've written (for a
    > > new project) several base class 'Forms' (based on JDialog and JFrame
    > > classes) that have several standard controls and a general-purpose
    > > JPanel (on which subclasses can put controls). A couple of developers
    > > are already extending them to build the forms used in the program. In
    > > one of them the developer has used the size of the JPanel to influence
    > > the size of some of the controls.

    >
    > I *think* this nmight be one of those "don't
    > ever do anything that stupid" type of things,
    > but I am too tired to know right now (and I
    > am hoping there are some GUI masters out there
    > might jump in any time..)
    >
    > If you need to pursue it much furher I would
    > advise you take it out of the hands of these
    > ..hacks and take it over to c.l.j.gui, where
    > the GUI folks actually hang out ( ducks quickly
    > and starts loping away.. ;-)


    I remember now a specific case - in one help window we want to have a
    table (size 3x2) and various parts of text within this table is
    highlighted. Eg the start of a word is highlighted, the end not.
    This was very easy to do in html (putting it in a JLabel), which also
    included the table definition as html. Cool! However if the JPanel was
    300 pixels wide and the table was set to 100%, it wouldn't stretch to
    the full width if there wasn't much text in it.
    The only way we could make the html table use the full width of the
    panel was to explicitly state the size of it (in pixels).
    Hence our desire to know the true dimensions of the JPanel.
    TL, Aug 3, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    26,972
  2. Norman Khine
    Replies:
    0
    Views:
    217
    Norman Khine
    Jun 30, 2007
  3. Evan Klitzke
    Replies:
    3
    Views:
    288
    Diez B. Roggisch
    Jul 1, 2007
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,513
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,398
    t1m1976
    Nov 7, 2010
Loading...

Share This Page