Other |
---|
5.0 tigerFixed |
Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
java -version: java version "1.3.1-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-beta-b15) Java HotSpot(TM) Client VM (build 1.3.1beta-b15, mixed mode) JTextArea returns the incorrect preferred size when line wrapping is used. The following sample program puts a line-wrapped JTextArea in a JDialog. It prints out the preferred size of the JTextArea before and after the dialog's pack() method is called. These 2 dimensions are not the same - its only correct after pack() has been called. import java.lang.*; import javax.swing.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; public class JTextAreaWrappingTest { private String testStr = "This string should be sufficiently long to demonstrate the problem."; WrapArea wrapArea; public static void main(String argv[]) { JTextAreaWrappingTest myTest = new JTextAreaWrappingTest(); } public JTextAreaWrappingTest () { JDialog dialog = new JDialog(); WrapArea wrapArea = new WrapArea(testStr); dialog.getContentPane().setLayout(new FlowLayout()); dialog.getContentPane().add(wrapArea); System.out.println("pre-pack() prefSize = " + wrapArea.getPreferredSize()); dialog.pack(); // UNCOMMENT THIS LINE AND RE-COMPILE TO DEMONSTRATE THE WORK AROUND // dialog.pack(); System.out.println("post-pack() prefSize = " + wrapArea.getPreferredSize()); dialog.show(); } // constructor public class WrapArea extends JTextArea { public WrapArea(String str) { super(str); wrapArea = this; wrapArea.setColumns(30); wrapArea.setLineWrap(true); } // constructor } // WrapArea } // JTextAreaWrappingTest
|