3 Replies Latest reply on Sep 18, 2007 6:14 AM by 843806

    Long last line in wrappable textarea hangs GUI (bug in java?)

    843806
      If I have a JTextarea or JEditorPane with a very long last line the GUI hangs.
      I think it is a bug in java. I am really searching fo a workaround.
      You can test it with the following program.
      Just start the program and delete the last linebreaks (blank lines) one by one.
      The gui will hang.

      import java.awt.BorderLayout;
      import java.awt.Container;

      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;

      public class TextEdit extends JFrame
      {

           public static void main(String[] args)
           {
                TextEdit editor = new TextEdit();
                editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                editor.setVisible(true);
           }

           // Create an editor.
           public TextEdit()
           {
                super("Swing Editor");
                JTextArea area = new JTextArea();

                int length = 1024 * 1024;

                StringBuffer sb = new StringBuffer(length);
                for (int i = 0; i < length; i++)
                {
                     sb.append("A");
                }
                sb.append("\n");//append one line break
                sb.append("\n");//append secondline break

                area.setText(sb.toString());

                area.setLineWrap(true);
                Container content = getContentPane();
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setViewportView(area);
                content.add(scrollPane, BorderLayout.CENTER);
                setSize(320, 240);
           }

      }
        • 1. Re: Long last line in wrappable textarea hangs GUI (bug in java?)
          843806
          I did a few performance test.
          I commented out the extra linebreaks and timed how long it took before the text appearded on the screen
          100K - 004 seconds
          200K - 023 seconds
          300K - 405 seconds

          So it not surprising that with 1 MB you will wait forever.
          As said, there is no problem when last line is not a long line.
          • 2. Re: Long last line in wrappable textarea hangs GUI (bug in java?)
            843806
            Would you realistically want such a huge String displayed in a GUI? Why? Why not display part of it, then fetch more as needed. Isn't that what most real-world programs would do? This doesn't seem to be a realistic problem to me; maybe I'm missing something.
            • 3. Re: Long last line in wrappable textarea hangs GUI (bug in java?)
              843806
              Would you realistically want such a huge String displayed in a GUI
              I have a built an editor for plain text and xml and then it this is a very realistic scenario (300K is not that big and you may expext form a textcomponent that it will be handled correctly).

              And in fact, when the long line is not the last one, there is no problem and one can load much larger content.
              Why not display part of it, then fetch more as needed.
              Yes this really make sense but I was hoping that the java textcomponents would do this. Because else, you have to code a lot by yourself.

              It looks like this is really a code prolem in some gui class. And my feeling is that it can solved. Maybe by "fooling" the calculation with the extra lineberak at the end and then correct it just for displaying. Any suggetion, workaround is very welcome.