-2

I have an application that is receiving text from the user then putting it into a jLabel. It does some processing on the text so I thought that it was a problem with that but after some troubleshooting I have isolated the most time consuming part of the program.

text1.setText( arg2 );

Where arg2 is a long string. In testing I have been using 9000 lines. It is also formatted in HTML. Where I would think it may take some time, a few seconds, it is taking a huge amount of time, 3 minutes and 35 seconds. I have found some questions here that have similar problems with jTextArea:

https://stackoverflow.com/questions/23951118/jtextarea-settextverylongstring-is-taking-too-much-time

But I cannot find a way to apply that solution to this problem. Is there a solution for this?

EDIT - My code is below. Note I have cut down the middle part of the string for brevity.

import java.io.*;
import java.lang.*;
import javax.swing.*;
public class jLabelIssue {
    public static void main( String[] args ) {
        final JFrame frame = new JFrame( "Comparinger use this to compare things and stuff" );
        frame.setSize(268, 150);
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible( true );
        JLabel text1 = new JLabel( );
        frame.add( text1 );
        arg2 = 
        "<HTML><font color=black>" + 
        "a<br/>" +
        "a<br/>" +
        "a<br/>" +
        //... 9000 more lines of this ...
        "a<br/>" +
        "a<br/>" +
        "a<br/>" +
        "</font></HTML>";
        text1.setText( arg2 );
        frame.repaint();
    }
}
Community
  • 1
  • 1
Goff
  • 343
  • 1
  • 3
  • 14
  • 6
    For the best chance at decent help, please create and post a decent [mcve]. Please read this important link to learn the details. – Hovercraft Full Of Eels Mar 23 '16 at 20:22
  • 1
    Also note that the link that **you** provided has an example of just what I mean, also known as a [sscce](http://sscce.org/). – Hovercraft Full Of Eels Mar 23 '16 at 20:24
  • 4
    `In testing I have been using 9000 lines. It is also formatted in HTML` - a JLabel was not designed to display 9000 lines of text. What is the point of creating a tremendously long string all at once? Maybe you should use something like a JLIst to render one line at a time. Or maybe try using a more appropriate component that is designed for HTML, like a JEditorPane. – camickr Mar 23 '16 at 20:48
  • I have tried several other components but I have not been able to get a JEditorPane to work correctly. I need the text to be colored when it is processed. I tried several things but I was not able to the RTFEditorKit to give me the correct formatting. – Goff Mar 23 '16 at 21:05
  • "What is the point of creating a tremendously long string all at once?" The reason is a jTextArea does not do colors. My program is similar to kdiff3. – Goff Mar 23 '16 at 21:07
  • @Hovercraft Full Of Eels I do not understand your comment. I gave the exact code. It is a jLabel and arg2 is a string. How could I have put that differently? – Goff Mar 23 '16 at 21:09
  • @camickr I'm not certain but can a jList have HTML formatting? This does not seem to fit the scenario. – Goff Mar 23 '16 at 21:15
  • 1
    My comment is clarified *precisely* by the two links that I gave. Have you read them yet? Please read them and then comment back with specific concerns if you're still confused. – Hovercraft Full Of Eels Mar 23 '16 at 21:15
  • Yes, I did read them. Creating a JLabel is done exactly how I posted it. arg2 is literally a String with 9000 lines. Should I post a String with 9000 lines so you can look at a test String like that? The problem is a very long delay using the setText method, the link that I posted is the same result but instead of using a jLabel that person used a jTextArea. – Goff Mar 23 '16 at 21:22
  • 3
    @Goff is MCVE means something for you? Do you seriously think that the code you gave is a MCVE? Or SSCCE? This is generally considered as a requirement on SO. Even if your question is clearly understandable, you cannot complain if you violate the rules. – Jean-Baptiste Yunès Mar 23 '16 at 21:42
  • 2
    `JLabel` is NOT intended for such a use, @camickr suggested you to use a `JEditorPane`, I'm pretty sure that you will be able to find many working examples on Internet (maybe in Java tutorials, who knows?). – Jean-Baptiste Yunès Mar 23 '16 at 21:44
  • 1
    `Yes, I did read them.` - seriously??? How does one line of code demonstrated the problem? How can we compile and test one line of code? `Should I post a String with 9000 lines` - if you read the links then you would already know the answer to that. The point of a SSCCE is to demonstrate the problem in the minimal amount of the code that will demonstrate the problem. – camickr Mar 24 '16 at 00:03
  • `I'm not certain but can a jList have HTML formatting?` yes it can. But still have no idea why you think you need to have 9000 lines in a single JLabel. As a user that would be a terrible UI. We care trying to suggest alternatives but you are not helping by providing any actual requirement. All you have given us is your attempted solution, which is not working. You haven't given us a real requirement. – camickr Mar 24 '16 at 00:06
  • @camickr I have tried a jEditorPane, it is not recognizing the HTML and is showing it unformatted. I will look into this and report back. The program is like kdiff3, it reads the user input, which is a configuration file, then color codes it for easy viewing. The size is from the equipment and I cannot change that. The jLabel is inside of a jScrollPane and looks just like a jTextArea. – Goff Mar 24 '16 at 17:51
  • @Goff, once again you have NOT posted a `SSCCE`. The code you posted doesn't show us anything about the HTML you are using. You can use different components to show lines of text. The relevant code would be the complexity of your HTML. You can easily create a loop to add lines of text that use your HTML. The point of a SSCCE is to get you to think about simplifying the code so it can't be explained to use. We can't read your mind. – camickr Mar 24 '16 at 18:27

1 Answers1

1

The program is like kdiff3, it reads the user input, which is a configuration file, then color codes it for easy viewing.

So don't use HTML. All the time is spend parsing the HTML.

Just use simple text with attributes. That is use a JTextPane and color code the text however you want.

I have done syntax highlighting on a 9600 line Java source file in a couple of seconds. And that logic would be more complicated because of all the parsing of the text into tokens.

Read the section from the Swing tutorial on Text Component Features for a working example of playing with attributes.

Your basic logic would be something like:

// Define the basic colors you want to use:

SimpleAttributeSet colorCode1 = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);

SimpleAttributeSet colorCode2 = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.YELLOW);

//  Add some text

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

try
{
    doc.insertString(doc.getLength(), "\nA line of text", colorCode1);
    doc.insertString(doc.getLength(), "\nAnother line of text", colorCode2);
}
catch(Exception e) {}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Well, I was able to use this, but it did not save time. The jEditorPane took 9 minutes the JTextPane took 6 minutes. I'm closing this. – Goff Mar 25 '16 at 23:46
  • Well given that you haven't posted any code, we can't offer any more help. As I told you it only takes me a couple of seconds to highlight 9600 lines of code. You must be doing something wrong. – camickr Mar 26 '16 at 03:11