Search code examples
javaswingjscrollpanejeditorpane

Problem with JEditor Pane while resizing the frame


i am new to java GUI programming. i am adding a JEditor pane (with HTML text ) inside a JScrollPane and added the scroll pane to a JFrame.The problem is when i re size the frame the JEditor pane vanishes.

Any one help me ?

Hi All ,

Following is the code :

here another problem is i set weightx and weighty for the pane .But still the JEditor pane is not resizing when the frame is maximized/mimized

public class GridBagWithJEditorPane extends javax.swing.JFrame {

public GridBagWithJEditorPane() {
    initComponents();
}

private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    Panel1 = new javax.swing.JPanel();
    Button1 = new javax.swing.JButton();
    Label = new javax.swing.JLabel();
    Panel2 = new javax.swing.JPanel();
    ScrollPane = new javax.swing.JScrollPane();
    EditorPane1 = new javax.swing.JEditorPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    Panel1.setLayout(new java.awt.GridBagLayout());

    Button1.setText("Button");
    Panel1.add(Button1, new java.awt.GridBagConstraints());

    Label.setText("Label");
    Panel1.add(Label, new java.awt.GridBagConstraints());

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    getContentPane().add(Panel1, gridBagConstraints);

    Panel2.setLayout(new java.awt.GridBagLayout());

    ScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    EditorPane1.setContentType("text/html");
    EditorPane1.setText("<html>\r\n  <head>\r\n\r\n  </head>\r\n  <body>\r\n    <p style=\"margin-top: 0\">\r\n      \rHere some html text sakdjslakdjsa dksa dsakjdklsajdklsad klsajd lksad<br>\n      asdka;slkd;laskd;sa dlksa dksa dksald;lsakd;lsakd;l ska;dl sal;dk;salkd<br>\n     asas;alks;laKSL;Kalk ALSKLAks;laSAsLAKS;Lk;slk<br>\t\n    alsdasldk;alskd;laskd;l sadksa;dlksa;ldk;saldk;alsd<br>\n    </p>\r\n  </body>\r\n</html>\r\n");
    EditorPane1.setMinimumSize(new java.awt.Dimension(15, 15));
    EditorPane1.setPreferredSize(new java.awt.Dimension(340, 220));
    ScrollPane.setViewportView(EditorPane1);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    Panel2.add(ScrollPane, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    getContentPane().add(Panel2, gridBagConstraints);

    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GridBagWithJEditorPane().setVisible(true);
        }
    });
}


private javax.swing.JButton Button1;
private javax.swing.JEditorPane EditorPane1;
private javax.swing.JLabel Label;
private javax.swing.JPanel Panel1;
private javax.swing.JPanel Panel2;
private javax.swing.JScrollPane ScrollPane;

}


Solution

  • solved the problem myself . The solution is same as in

    resize problem with JList?