problem adding panes to JPanel

Discussion in 'Mac Programming' started by bhargava, Aug 1, 2007.

  1. macrumors newbie

    Joined:
    Aug 1, 2007
    #1
    Hi,
    The code i mentioned below is not working for me.
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
        public static void main(String[] args) throws Exception{
            JFrame frame = new JFrame();
            JPanel panel = new JPanel(new BorderLayout());
    
            JScrollPane sp = new JScrollPane(new JEditorPane("text/html", "Hello Worlddddddddddddd"), ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    
            panel.add(sp, BorderLayout.CENTER);
            panel.add(new JEditorPane("text/html", "Hello Wrold"), BorderLayout.SOUTH);
    
            frame.setSize(255, 255);
            frame.getContentPane().add(panel);
            frame.show();
        }
    }
    
    Problem is, I can only see JScrollPane component of the panel, where I want to see both the scroll pane and editor pane. Is there something wrong in my code?
     
  2. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #2
    You need to replace:

    Code:
    frame.setSize(255, 255);
            frame.getContentPane().add(panel);
            frame.show();
    
    with:

    Code:
    frame.getContentPane().add(panel);
            frame.pack();
            frame.setSize(255, 255);
            frame.show();
    
     

Share This Page