Component preferred size dilemma

Discussion in 'Java' started by Kerry Shetline, Nov 13, 2003.

  1. If I've not yet laid out a component, say a JTextField, and use
    getPreferredSize() to find the preferred width, I'll get what I'd
    expect to get: a width which is just wide enough for the text in the
    field.

    Now, suppose I set the width to something wider than this preferred
    width, via setBounds() or setSize(). If I again use getPreferredSize()
    to find the preferred width, I'll now get the width I just set, rather
    than what I originally got from getPreferredSize().

    How, if it's at all possible, do I ask a component "If I'd never
    changed your size before, what size would you now prefer to be?"?

    What I'm aiming at is a layout where excess window width is
    distributed among expandable columns. When the window is reduced in
    width, I don't want the colums which have been expanded to be "stuck"
    at the expanded width -- I want to be able to shrink those columns
    back down to the widest *original* preferred width of any component in
    each column.

    So far I've only created layouts such as this by ignoring a
    component's preferred width, and letting the width go as low as 1 if
    the containing window is shrunk sufficiently.

    As a hack, I can save the first preferred width I get, but that not's
    only inelegant, but wouldn't work well if the component's content
    changed dynamically.

    In the case of a JTextField, using getMinumumSize() doesn't seem to be
    helpful... it returns a width less than that needed to display the
    full text.
     
    Kerry Shetline, Nov 13, 2003
    #1
    1. Advertising

  2. Kerry Shetline wrote:
    > If I've not yet laid out a component, say a JTextField, and use
    > getPreferredSize() to find the preferred width, I'll get what I'd
    > expect to get: a width which is just wide enough for the text in the
    > field.
    >
    > Now, suppose I set the width to something wider than this preferred
    > width, via setBounds() or setSize(). If I again use getPreferredSize()
    > to find the preferred width, I'll now get the width I just set, rather
    > than what I originally got from getPreferredSize().


    Are you sure this happens with a JTextField ? I thought this was the
    behaviour of only multi-line text components such as JTextArea.

    >
    > How, if it's at all possible, do I ask a component "If I'd never
    > changed your size before, what size would you now prefer to be?"?
    >


    You can try set the size to 0,0 using a setSize call and then asking for
    the getPreferredSize. IIRC, in such a case it assumes a size of
    Integer.MAX_VALUE on either axis and computes the preferredSize.

    For components that can wrap text (such as the JTextArea), the more
    meaningful number appears when you have set at least of one of the
    dimensions. In which case the more appropriate question would be : "What
    would be your preferred Size if the preferred width was "n" pixels ? "


    BK
     
    Babu Kalakrishnan, Nov 14, 2003
    #2
    1. Advertising

  3. Kerry Shetline

    Tony Dahlman Guest

    Kerry Shetline wrote:
    >
    > If I've not yet laid out a component, say a JTextField, and use
    > getPreferredSize() to find the preferred width, I'll get what I'd
    > expect to get: a width which is just wide enough for the text in the
    > field.
    >
    > Now, suppose I set the width to something wider than this preferred
    > width, via setBounds() or setSize(). If I again use getPreferredSize()
    > to find the preferred width, I'll now get the width I just set, rather
    > than what I originally got from getPreferredSize().
    >
    > How, if it's at all possible, do I ask a component "If I'd never
    > changed your size before, what size would you now prefer to be?"?
    >
    > What I'm aiming at is a layout where excess window width is
    > distributed among expandable columns. When the window is reduced in
    > width, I don't want the colums which have been expanded to be "stuck"
    > at the expanded width -- I want to be able to shrink those columns
    > back down to the widest *original* preferred width of any component in
    > each column.
    >
    > So far I've only created layouts such as this by ignoring a
    > component's preferred width, and letting the width go as low as 1 if
    > the containing window is shrunk sufficiently.
    >
    > As a hack, I can save the first preferred width I get, but that not's
    > only inelegant, but wouldn't work well if the component's content
    > changed dynamically.
    >
    > In the case of a JTextField, using getMinumumSize() doesn't seem to be
    > helpful... it returns a width less than that needed to display the
    > full text.


    Hi! Have you tried overriding the getPreferredSize() method to return
    the Size you want? Usually with Java this is not such a difficult
    problem.... Regards, Tony Dahlman
    ---------------------------------------
    a(nospace)dahlman(at)attglobal(dot)net
     
    Tony Dahlman, Nov 23, 2003
    #3
  4. Tony Dahlman <> wrote in message news:<>...
    > Hi! Have you tried overriding the getPreferredSize() method to return
    > the Size you want? Usually with Java this is not such a difficult
    > problem.... Regards, Tony Dahlman


    I don't know what size I want... I want the component to tell me.

    Imagine a JTextField. Only the JTextField itself, along with its
    underlying look-and-feel (LAF), knows just how wide and tall it should
    be for the text it contains, for the font that is being used, the
    particular thickness of the borders (if any) used by the LAF, the
    clearance desired between the text and the borders, etc. There's a
    perfect size where everything fits snuggly with no more or no less
    room than is needed.

    The component can tell me this size, but only if I've never explicitly
    set the size of the component myself. Once I do so, the component
    wrongly assumes that I don't want to have its own opinion of its
    preferred size any more. I need that underlying preferred size,
    because while I might want to go dynamically larger than that
    preferred size to arrange things in neat rows and columns, I don't
    want to go smaller than that size and clip any of the text.

    Yes, I can save the component's own preferred size before I change it
    myself (that's what I'm doing now to get around this problem), but
    once I change the components size explicitly, I can't find out what
    size the component might prefer to be if I change the text content of
    the component.
     
    Kerry Shetline, Dec 1, 2003
    #4
  5. Kerry Shetline wrote:
    > The component can tell me this size, but only if I've never explicitly
    > set the size of the component myself. Once I do so, the component
    > wrongly assumes that I don't want to have its own opinion of its
    > preferred size any more. I need that underlying preferred size,
    > because while I might want to go dynamically larger than that
    > preferred size to arrange things in neat rows and columns, I don't
    > want to go smaller than that size and clip any of the text.
    >


    I am unable to reproduce this behaviour in any of the JDK versions or
    platforms that I have tried it on. Merely calling setSize / setBounds on
    a *JTextField* does not seem to affect the value returned by
    getPreferredSize. (As I said in a previous message, it can affect the
    values in a JTextArea whth wrapping enabled).

    Can you post some sample code that exhibits this behaviour ?

    Try running the following program, and resize the frame manually. In my
    tests, the W and H paramaters vary (when the layout manager calls
    setBounds on the JTextField), but the preferredSize returned remains the
    same.

    import java.awt.*;
    import javax.swing.*;

    public class TextfieldTest extends JFrame
    {
    JLabel sizeLabel = new JLabel();

    JTextField test = new JTextField("Hello world")
    {
    public void reshape(int x,int y, int w, int h)
    {
    // Called from within a setSize or setBounds call
    super.reshape(x,y,w,h);
    updateLabel(w,h);
    }
    };

    public TextfieldTest()
    {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = getContentPane();
    cp.add(test,BorderLayout.CENTER);
    cp.add(sizeLabel,BorderLayout.SOUTH);
    setSize(300,100);
    }

    private void updateLabel(int w, int h)
    {
    Dimension pref = test.getPreferredSize();
    sizeLabel.setText("Size= "+w+" X "+h+
    " Pref= "+pref.getWidth()+" X "+pref.getHeight());
    }

    public static void main(String[] args)
    {
    new TextfieldTest().show();
    }
    }


    BK
     
    Babu Kalakrishnan, Dec 5, 2003
    #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. Shiperton Henethe
    Replies:
    117
    Views:
    2,195
    Andy Dingley
    Dec 22, 2004
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,661
    Michael Jung
    May 25, 2008
  3. Chris F.A. Johnson

    Re: Preferred way to size fonts

    Chris F.A. Johnson, Nov 5, 2008, in forum: HTML
    Replies:
    19
    Views:
    769
    Sherm Pendley
    Nov 8, 2008
  4. dorayme

    Re: Preferred way to size fonts

    dorayme, Nov 5, 2008, in forum: HTML
    Replies:
    0
    Views:
    435
    dorayme
    Nov 5, 2008
  5. richard

    Re: Preferred way to size fonts

    richard, Nov 6, 2008, in forum: HTML
    Replies:
    6
    Views:
    480
    Acmeous
    Nov 10, 2008
Loading...

Share This Page