Component preferred size dilemma

K

Kerry Shetline

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

Babu Kalakrishnan

Kerry said:
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
 
T

Tony Dahlman

Kerry said:
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
 
K

Kerry Shetline

Tony Dahlman said:
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.
 
B

Babu Kalakrishnan

Kerry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top