Results 1 to 2 of 2
- 03-23-2011, 11:42 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Two calls to JFrame.pack() required to get desired effect?
Hi,
some sample code :
Java Code:public class TestFrame extends JFrame { private JEditorPane editorPane; public TestFrame() { initialise(); } private void initialise() { Container contentPane = this.getContentPane(); editorPane = new JEditorPane(); editorPane.setContentType("text/html"); contentPane.add(editorPane); this.setVisible(true); this.setDefaultCloseOperation(HIDE_ON_CLOSE); } public void loadResourcePage(String resourcePage) { InputStream input = this.getClass().getResourceAsStream(resourcePage); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); StringBuffer buffer = new StringBuffer(); String line, newLineStr; newLineStr = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { buffer.append(line); buffer.append(newLineStr); } reader.close(); } catch (IOException e) { e.printStackTrace(); } editorPane.setText(buffer.toString()); this.pack(); } public static void main (String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { TestFrame frame = new TestFrame (); frame.loadResourcePage("page1.html"); } }); }
When i run this code with about half a page of HTML in the page.html file, the JFrame, on calling pack(), resizes itself to fit horizontally around the JEditorPane with the loaded HTML. However, it doesn't seem to resize vertically. It only expands about 20 pixels downwards. Having said that, if I change :
to :Java Code:editorPane.setText(buffer.toString()); this.pack();
Java Code:editorPane.setText(buffer.toString()); this.pack(); this.pack();
then it works as I expected it to, wrapping around the JEditorPane which in turn is wrapped around my HTML. Can anyone explain why this is, and if this is normal behaviour?
Thanks,
Nick
- 03-24-2011, 01:03 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Similar Threads
-
displaying a desired out put
By Cubswin in forum New To JavaReplies: 3Last Post: 03-13-2011, 06:05 AM -
Using frame.pack() for resizing JFrame
By LianaN in forum AWT / SwingReplies: 0Last Post: 10-30-2010, 12:12 PM -
Can we move a text to a desired place in JTextArea
By ronaldrandon in forum New To JavaReplies: 7Last Post: 08-09-2010, 05:32 AM -
Integer negation does not give the desired result.
By XdaX in forum Advanced JavaReplies: 20Last Post: 10-21-2009, 03:59 PM -
visual web pack
By Jack in forum NetBeansReplies: 2Last Post: 07-02-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks