1

The code snippet below has the problem, that it will not work, if the reload button is pressed in the browser containing the applet window. It works on the first start of the applet, but not on a reload. The same thing happens in the AppletViewer.

The reason is, that the Text.setText(...) call crashes with a NullPointerException deeply inside the HTMLParser. I already tried to place the setText call into start(), but that did not help.

Do you know any workaround? Thanks for your help. RG

@Override
public void init()
{
    //Execute a job on the event-dispatching thread:
    //creating this applet's GUI.
    try
    {
        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.err.println("createGUI didn't successfully complete");
    }
}

private void createGUI()
{
    ((JComponent)this.getContentPane()).setBorder(new CompoundBorder
            (BorderFactory.createRaisedBevelBorder(),
                    new EmptyBorder(5,5,5,5)));

    BorderLayout bl=new BorderLayout();
    bl.setVgap(5);
    setLayout(bl);

    Input=new JTextField();
    Input.setFont(new Font("arial",Font.PLAIN,14));
    add("North",Input);
    Input.addActionListener(this);

    HTMLEditorKit kit=new HTMLEditorKit();
    Text=new JTextPane();
    Text.setFont(new Font("arial",Font.PLAIN,14));
    Text.setEditorKit(kit);
    Text.setText("<p>Test</p>");
    Text.setEditable(false);
    Text.setBackground(Color.white);
    add("Center",new JScrollPane(Text));

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Rene
  • 3,746
  • 9
  • 29
  • 33

1 Answers1

1

Not sure where you copied that code from but it looks awfully old.

add("North",Input); 
add("Center",new JScrollPane(Text)); 

That is not the preferred way to specify constraints when adding components to a container. Read the API for the recommended approach. Or read the Swing tutorial on "How to Use a Border Layout" for examples.

Not sure why you are creating an editor kit. Also your text isn't proper HTML (don't know if it makes a difference).

I've just used code like the following in the past:

String text = "<html><body>Some text><body></html>";
JEditorPane editor = new JEditorPane("text/html", text);

I also find it much easier to use a JTextPane and then use attributes if you need to stylize the text.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for the answer! Yes the layout code is old fashioned, but it still works, and it is not the problem. I tried your JEditorPane method without a HTMLEditorKit, and that seems to be the same, but easier. But the applet still crashes after a reload, with the same kind of trouble in the HTML parser. If I use ordinary text, I can reload as often as I like. – Rene Nov 08 '10 at 20:42
  • And no, the missing html and body tags make no difference. – Rene Nov 08 '10 at 20:43
  • Thanks to your help I found it: It is a bug See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691 – Rene Nov 08 '10 at 20:44
  • I can't find the documentation for using attributes to style text in a JTextPane. Where could I find it? – Azmisov Nov 09 '13 at 06:32