JTextArea size problem.
To: comp.lang.java.gui
In a dialogue box, I want to display some text word-wrapped. I use a
JTextArea and set word wrapping on.
However, the height of the dialogue seems to be incorrectly based on the
Textarea being one line high, (it is two lines high when wrapped).
The println statements show
Pref java.awt.Dimension[width=352,height=16]
Wrap set
Pref java.awt.Dimension[width=330,height=16]
Note that the width is reduced but the height is wrong.
Packed
Size java.awt.Dimension[width=330,height=16]
No change due to pack()
Size java.awt.Dimension[width=330,height=32]
But after closing, the height is correct (32)!
Qs:
Am I doing something wrong?
Is there a better way to word-wrap some text in a dialogue?
------------------------- 8< ----------------------
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TestErrorDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestErrorDialog();
}
});
}
private JFrame f;
TestErrorDialog() {
JPanel p = new JPanel();
p.add(new JLabel("Testing ..."));
f = new JFrame("Test Error Dialog");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setVisible(true);
triggerAnError();
}
public void triggerAnError() {
System.out.println("Error triggered");
new ErrorDialog(f, "Unable to unwind flanges!",
"Please contact the help desk "
+ "and tell them absolutely everything.");
System.exit(0);
}
}
// ==================================================================
class ErrorDialog extends JDialog implements ActionListener {
JTextArea messageComponent = new JTextArea(1, 30);
static final String CLOSE = "Close";
public ErrorDialog(JFrame parent, String title, String message) {
super(parent, "Error", true);
System.out.println("Constructing dialog");
messageComponent.setText(message);
messageComponent.setEditable(false);
System.out.println("Pref "
+ messageComponent.getPreferredSize());
messageComponent.setLineWrap(true);
messageComponent.setWrapStyleWord(true);
System.out.println("Wrap set");
System.out.println("Pref "
+ messageComponent.getPreferredSize());
JButton closeButton = new JButton(CLOSE);
closeButton.addActionListener(this);
JLabel titleLabel = new JLabel("<html><body><h2>" + title
+ "</h2></body></html>");
JPanel innerPane = new JPanel();
innerPane.setLayout(
new BoxLayout(innerPane, BoxLayout.PAGE_AXIS));
innerPane.setBorder(
BorderFactory.createEmptyBorder(10,10,10,10));
for (JComponent component : new JComponent[] { titleLabel,
messageComponent, closeButton })
component.setAlignmentX(Component.LEFT_ALIGNMENT);
innerPane.add(titleLabel);
innerPane.add(Box.createRigidArea(new Dimension(0, 5)));
innerPane.add(messageComponent);
innerPane.add(Box.createRigidArea(new Dimension(0, 5)));
innerPane.add(closeButton);
add(innerPane);
getRootPane().setDefaultButton(closeButton);
setLocationRelativeTo(parent);
pack();
System.out.println("Packed");
System.out.println("Size " + messageComponent.getSize());
setVisible(true);
System.out.println("Size " + messageComponent.getSize());
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
} // ErrorDialog
---------------------------- 8< -------------------------
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24