Results 1 to 2 of 2
  1. #1
    dark_perfect is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default 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 :

    Java Code:
    		editorPane.setText(buffer.toString());
    		this.pack();
    to :

    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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    I"ve used code like that before on a JTextArea, so I would say its normal behaviour. Not sure the exact reason why it behaves that way.

Similar Threads

  1. displaying a desired out put
    By Cubswin in forum New To Java
    Replies: 3
    Last Post: 03-13-2011, 06:05 AM
  2. Using frame.pack() for resizing JFrame
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 10-30-2010, 12:12 PM
  3. Can we move a text to a desired place in JTextArea
    By ronaldrandon in forum New To Java
    Replies: 7
    Last Post: 08-09-2010, 05:32 AM
  4. Replies: 20
    Last Post: 10-21-2009, 03:59 PM
  5. visual web pack
    By Jack in forum NetBeans
    Replies: 2
    Last Post: 07-02-2007, 05:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •