简体   繁体   English

将HTML内容设置为JTextPane会混淆System.out重定向

[英]Setting HTML content to JTextPane messes up System.out redirection

I am using the MessageConsole class to redirect System.out and System.err to a JTextPane named jMessageConsoleTextPane . 我使用MessageConsole类将System.outSystem.err重定向到名为jMessageConsoleTextPaneJTextPane

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 . 并且在NetBeans中我将它设置为contentType text/html

It prints everything fine with System.out and System.err but here comes my problem. 它使用System.outSystem.err打印一切都很好,但这是我的问题。 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: 我尝试将jMessageConsoleTextPane的文本设置为我基于一些变量创建的html表(其中包含两个其他html表), 所以我这样做:

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

Here is how the htmlTable String can look like (taken straight from the debugger): 以下是htmlTable String的外观(直接从调试器中获取):

<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 : 以下是它在jMessageConsoleTextPane中的jMessageConsoleTextPane

第一

Then I try to use System.out which should print bellow text: 然后我尝试使用System.out ,它应该打印下面的文字:

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

and this happens: 这发生了:

第二

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): 即使我执行jMessageConsoleTextPane.setText("") ,也会有一个像这样的唯一表格单元格(左上角):

第三

So what is going on? 那么发生了什么? Why is there a cell left and how do I fix it? 为什么还有一个单元格,如何解决?

The MessageConsole class was not designed to be using with HTML. MessageConsole类未设计为与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. 所有类都将文本添加到Document的末尾,在您的情况下,它似乎是表的最后一个单元格。

You can try adding an empty HTML tag to the Document when you first create the HTML. 首次创建HTML时,可以尝试向Document添加空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. 这完全取决于Document如何解析插入到Document中的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM