Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JComponent preferred size question

85 views
Skip to first unread message

Robert Parlett

unread,
Nov 4, 2002, 8:58:45 AM11/4/02
to
Can anyone tell me how to get a swing component to compute its
preferred size dimensions, given one dimension as a constraint? What
I am trying to do is to get a bit of html into a dialog with a
sensible width specified, but the height worked out automatically. If
I specify no preferred size, then the JEditorPane's preferred size is
far too wide. But if I give my own dimensions, I am forced to guess
what the height is, given my preferred width. This is obviously
dependent on the look and feel and the font being used.

Hopefully my problem is illustrated by the following small program.

Many thanks for any suggestions.


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

public class Text extends JFrame
{
public Text()
{
super("Text problem");

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container cp = getContentPane();

JEditorPane ep = new JEditorPane();
ep.setContentType("text/html");
ep.setText("<html>The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog.<ul><li>One<li>Two<li>Three<li>Four<li>Five" +
"</ul>End</html>");

// If this is taken out the preferred size returned by
// JEditorPane is much too wide; it doesn't have any
// linebreaks.
ep.setPreferredSize(new Dimension(200, 300));

cp.add(ep, BorderLayout.CENTER);

pack();

}

public static void main(String a[])
{
new Text().show();
}
}

A. Bolmarcich

unread,
Nov 4, 2002, 10:23:59 AM11/4/02
to
In article <ac6d49ac.02110...@posting.google.com>, Robert Parlett wrote:
> Can anyone tell me how to get a swing component to compute its
> preferred size dimensions, given one dimension as a constraint? What
> I am trying to do is to get a bit of html into a dialog with a
> sensible width specified, but the height worked out automatically. If
> I specify no preferred size, then the JEditorPane's preferred size is
> far too wide. But if I give my own dimensions, I am forced to guess
> what the height is, given my preferred width. This is obviously
> dependent on the look and feel and the font being used.

Write your own class that extends the component and override the
getPreferredSize method so that it

- invokes super.getPreferredSize() to get the standard preferred size

- returns a Dimension whose height is the standard preferred height and
whose width is whatever you want it to be

Robert Parlett

unread,
Nov 4, 2002, 1:37:59 PM11/4/02
to
A. Bolmarcich wrote:

This doesn't work, unfortunately.

What you get as a result is the width you want, but the wrong height, ie the
height the component wanted at its own preferred width.

To see this, try running the following amended program. The bottom of the
custom JEditorPane is not shown.


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

class MyEditorPane extends JEditorPane
{
public Dimension getPreferredSize()
{
Dimension d = super.getPreferredSize();
d.width = 200;
return d;
}
}

public class Text extends JFrame
{
public Text()
{
super("Text problem");

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container cp = getContentPane();

JEditorPane ep = new MyEditorPane();


ep.setContentType("text/html");
ep.setText("<html>The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog. The quick brown fox jumps over the lazy " +
"dog.<ul><li>One<li>Two<li>Three<li>Four<li>Five" +
"</ul>End</html>");

cp.add(ep, BorderLayout.CENTER);

A. Bolmarcich

unread,
Nov 4, 2002, 3:38:48 PM11/4/02
to
In article <3dc6be88$0$28318$fa0f...@lovejoy.zen.co.uk>, Robert Parlett wrote:
> What you get as a result is the width you want, but the wrong height, ie the
> height the component wanted at its own preferred width.

As far as I know, there is no easy way to do what you want. A Java
component has a preferred size that is independent of the size that a
layout manager will set it to. After its size is set, a component will
layout itself as best it can at that size.

A general approach is to have the getPreferredSize() method of a
component set the width of the component to the width that you want,
invoke validate on the component, determine the maximum height value
used by the component when it laidout itself in the validate method.
How the last step is done depends on the component.

Another approach is to put the component in a scroll pane and have the
user scroll over the preferred size of the component. With this
approach if the component implements Scrollable, you can override the
getScrollableTracksViewportWidth() method of the component to return
true so that only the height of the component is scrollable.

Christian Kaufhold

unread,
Nov 5, 2002, 11:09:57 AM11/5/02
to
Hello!

Robert Parlett <parl...@cpw.co.uk> wrote:

> Can anyone tell me how to get a swing component to compute its
> preferred size dimensions, given one dimension as a constraint? What

The text components typically will base their preferred size on their
current width. This has been discussed recently.

Christian

0 new messages