Question

I am using the MessageConsole class to redirect System.out and System.err to a JTextPane named jMessageConsoleTextPane.

I am configuring it in my constructor like this:

jMessageConsoleTextPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
MessageConsole mc = new MessageConsole(jMessageConsoleTextPane, true);
mc.redirectOut(null, null);
mc.redirectErr(Color.RED, null);
mc.setMessageLines(100);

and also in NetBeans I have it set to contentType text/html.

It prints everything fine with System.out and System.err but here comes my problem. I try to set the text of jMessageConsoleTextPane to an html table (which contains two other html tables) that I have created based on some variables, so I do:

String htmlTable = WordCounting.getHtmlTable(wordsArrays, hashtagsArrays);
jMessageConsoleTextPane.setText(htmlTable);

Here is how the htmlTable String can look like (taken straight from the debugger):

<html>
   <body>
      <table>
         <tr>
            <td>
               <table border="1">
                  <th colspan="3">Words used most</th>
                  <tr>
                     <td>1</td>
                     <td>day</td>
                     <td>3720</td>
                  </tr>
                  <tr>
                     <td>2</td>
                     <td>good</td>
                     <td>3354</td>
                  </tr>
                  <tr>
                     <td>3</td>
                     <td>love</td>
                     <td>2689</td>
                  </tr>
                  <tr>
                     <td>4</td>
                     <td>time</td>
                     <td>2372</td>
                  </tr>
                  <tr>
                     <td>5</td>
                     <td>got</td>
                     <td>1897</td>
                  </tr>
                  <tr>
                     <td>6</td>
                     <td>lot</td>
                     <td>1831</td>
                  </tr>
                  <tr>
                     <td>7</td>
                     <td>know</td>
                     <td>1801</td>
                  </tr>
                  <tr>
                     <td>8</td>
                     <td>photo</td>
                     <td>1772</td>
                  </tr>
                  <tr>
                     <td>9</td>
                     <td>girl</td>
                     <td>1755</td>
                  </tr>
                  <tr>
                     <td>10</td>
                     <td>life</td>
                     <td>1754</td>
                  </tr>
               </table>
            </td>
            <td>
               <table border="1">
                  <th colspan="3">Hashtags used most</th>
                  <tr>
                     <td>1</td>
                     <td>win</td>
                     <td>136</td>
                  </tr>
                  <tr>
                     <td>2</td>
                     <td>panjaforpunjab</td>
                     <td>105</td>
                  </tr>
                  <tr>
                     <td>3</td>
                     <td>aaronto600k</td>
                     <td>100</td>
                  </tr>
                  <tr>
                     <td>4</td>
                     <td>rt</td>
                     <td>89</td>
                  </tr>
                  <tr>
                     <td>5</td>
                     <td>giveaway</td>
                     <td>85</td>
                  </tr>
                  <tr>
                     <td>6</td>
                     <td>cfc</td>
                     <td>70</td>
                  </tr>
                  <tr>
                     <td>7</td>
                     <td>whybeinarelationshipwhen</td>
                     <td>65</td>
                  </tr>
                  <tr>
                     <td>8</td>
                     <td>retweet</td>
                     <td>64</td>
                  </tr>
                  <tr>
                     <td>9</td>
                     <td>gameinsight</td>
                     <td>64</td>
                  </tr>
                  <tr>
                     <td>10</td>
                     <td>rhoareunion</td>
                     <td>57</td>
                  </tr>
               </table>
            </td>
         </tr>
      </table>
   </body>
</html>

Here is how it looks in the jMessageConsoleTextPane:

first

Then I try to use System.out which should print bellow text:

[23:19:32] Getting driver...
[23:19:32] Connecting to database...
[23:19:32] Executing query...
5.6.16

and this happens:

second

Basically everything is getting added on the last cell of the second table. Even if I do jMessageConsoleTextPane.setText("") there is a sole table cell remaining like this (top left):

third

So what is going on? Why is there a cell left and how do I fix it?

Was it helpful?

Solution

The MessageConsole class was not designed to be using with HTML. All the class does is add the text to the end of the Document, which in your case appears to be the last cell of the table.

You can try adding an empty HTML tag to the Document when you first create the HTML. Then maybe the text will be added to this tag instead of the table cell.

Maybe something like:

       </table>
   <p></p>
</body>

This all depends on how the Document parses the text that is inserted into the Document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top