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;
}
> 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.
>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.
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?
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.
}
}
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));
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.
}
}