How to set JTextPane(or any component) preferred width but not the height

Hi, I try to layout a JTextPane. I want to limit the width of this component but not the height. It seems only a way to set the limit on both width and height.Is there a way to do this?Thanks!
[220 byte] By [maqiang9111a] at [2007-11-26 17:56:57]
# 1
I will explain how to do this if you pre-award me 10 dukes.
rkippena at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 2
I will pay in full when I see the solution :)Message was edited by: maqiang9111
maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 3

I can think of 2 options.

1. setMaximumSize(new Dimension(width, Integer.MAX_VALUE));

2. If option 1 does not work you can override getPreferredSize of JTextPane and set the width programmatically:

public Dimension getPreferredSize() {

Dimension d = super.getPreferredSize();

if (d != null)

d.width = width;

return d;

}

Rodney_McKaya at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 4
I already have a code sample just sitting here. It could be yours for 10 dukes.In any contract situation, there is an expectation that half the money upfront, and half on delivery.
rkippena at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 5
Now you should received the full amount. Please post the solution. (It better work :)
maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 6
Hey, Rodney_McKay, I tried both, they didn't work... thanks for the suggestion!Message was edited by: maqiang9111
maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 7

> Now you should received the full amount. Please post the solution.

> (It better work :)

Of course it will work.

A component's maximum size, preferred size, minimum size are just guidelines for the layout manager to use when calculating the true size. Once the true size is calculated the layout manager calls the setSize method of the component.

So in order to do what you ask, you have to choose a layout manager with the flexibility to fix one dimension, and expand another.

There are probably a few ways to do it. I'll show you the simplest way:

import javax.swing.*;

import java.awt.*;

public class F extends JFrame {

public F() {

JPanel p = new JPanel(new BorderLayout());

JTextArea ta = new JTextArea();

JScrollPane sp = new JScrollPane(ta);

sp.setMaximumSize(new Dimension(100, Integer.MAX_VALUE));

Box b = new Box(BoxLayout.X_AXIS);

b.add(sp);

p.add(b, BorderLayout.CENTER);

setContentPane(p);

setSize(400, 400);

setVisible(true);

}

public static void main(String[] args) {

new F();

}

}

Another option would be to use GridBagLayout, or find another layout manager.

rkippena at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 8

>Hey, Rodney_McKay, I tried both, they didn't work... thanks for the suggestion!

It depends which Layout Manager you're using, and your layout of components.

If you want help with a specific case then post a [url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url].

Some Layout Managers do not respect the Maximum, and some don't even take the preferred size as a must rule.

Rodney_McKaya at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 9

This solution is not what I am looking for.

My problem is to pop up a customized dialog (GridbagLayout) which contains a variable length message. If I let the JTextPane to decide the size, it could be too wide. If I set the max size as suggested, it doesn't seem doing anything.

I want the dialog to be nicely sized (with a certain width) to hold the message without any scrolling.

Any other ideas? My request should still be in warranty. Right?

maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 10

Rodney_McKay,

Thanks for the suggestion. Please see the code below.

-

import javax.swing.*;

import java.awt.*;

public class F extends JFrame {

JTextPane textPane = new JTextPane();

JPanel panel = new JPanel();

private static final Dimension SIZE_A = new Dimension (500, 40);

private static final Dimension SIZE_B = new Dimension (100, 40);

public F(String s) {

panel.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.BOTH;

c.weighty = 1;

c.gridx = 0;

c.gridy = 0;

c.gridwidth = 2;

c.weightx = 1;

panel.add(textPane, c);

JLabel l1 = new JLabel("something..........");

l1.setPreferredSize(SIZE_A);

c.gridx = 0;

c.gridy = 1;

c.weightx = 1;

c.gridwidth = 1;

panel.add(l1, c);

JLabel l2 = new JLabel("something else.........");

l2.setPreferredSize(SIZE_B);

c.gridx = 1;

c.gridy = 1;

c.weightx = 1;

c.gridwidth = 1;

panel.add(l2, c);

textPane.setText(s);

getContentPane().add(panel);

pack();

show();

}

public static void main(String[] args) {

new F("short");

//Short text is ok.

StringBuffer longText = new StringBuffer();

for(int i = 0; i<100; i++){

longText.append("Hello Hello ");

}

new F(longText.toString());

//long text makes the frame really wide

//Issue: how can I size the text pane to the width of SIZE_A.width + SiZE_B.width

//with a "dynamic" height.

}

}

maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 11

I tried this to match the same area size, but of course, it is not accurate.

There gotta be a better way for this!

Dimension d = textPane.getPreferredSize();

int w = SIZE_A.width + SIZE_B.width;

int h = d.width*d.height/w;

textPane.setPreferredSize(new Dimension(w, h));

maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 12

If you are happy, then you should give me 5 more dukes.

import javax.swing.*;

import java.awt.*;

public class F extends JFrame {

JTextPane textPane = new JTextPane();

JPanel panel = new JPanel();

private static final Dimension SIZE_A = new Dimension (500, 40);

private static final Dimension SIZE_B = new Dimension (100, 40);

public F(String s) {

panel.setLayout(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.VERTICAL;

c.weighty = 1;

c.weightx = 1;

c.gridx = 0;

c.gridy = 0;

c.gridwidth = 2;

textPane.setText(s);

textPane.setSize(new Dimension(SIZE_A.width + SIZE_B.width, Integer.MAX_VALUE)); // added

textPane.setPreferredSize(new Dimension(SIZE_A.width + SIZE_B.width, textPane.getPreferredSize().height)); // added

panel.add(textPane, c);

JLabel l1 = new JLabel("something..........");

l1.setPreferredSize(SIZE_A);

c.fill = GridBagConstraints.NONE; // added

c.gridx = 0;

c.gridy = 1;

c.weightx = 1;

c.weighty = 0; // added

c.gridwidth = 1;

panel.add(l1, c);

JLabel l2 = new JLabel("something else.........");

l2.setPreferredSize(SIZE_B);

c.fill = GridBagConstraints.NONE; // added

c.gridx = 1;

c.gridy = 1;

c.weightx = 1;

c.weighty = 0; // added

c.gridwidth = 1;

panel.add(l2, c);

getContentPane().add(panel);

pack();

show();

}

public static void main(String[] args) {

new F("short");

//Short text is ok.

StringBuffer longText = new StringBuffer();

for(int i = 0; i<100; i++){

longText.append("Hello Hello ");

}

new F(longText.toString());

//long text makes the frame really wide

//Issue: how can I size the text pane to the width of SIZE_A.width + SiZE_B.width

//with a "dynamic" height.

}

}

rkippena at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...
# 13
This works! Thanks!I am happy ... for the service under warranty!
maqiang9111a at 2007-7-9 5:10:12 > top of Java-index,Security,Cryptography...