附加到JTextPane的多个问题

时间:2015-01-06 03:33:45

标签: java swing

第一期:追加特定行

我正在尝试使用getStyledDocument方法追加到JTextPane中的特定行。例如:

     try {
         displayResults.getStyledDocument().insertString(5,"Hello",null);
     } catch (BadLocationException ex) {
         Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
     }

似乎偏移5只能水平偏移5个空格。除了insertString之外,是否有其他方法可以实现我想要做的事情?当我尝试从向下方向输出excel电子表格中的行时,这也成为一个问题。我一直想在我希望输出的特定字符串之前添加"\n"

     try {
         displayResults.getStyledDocument().insertString(5,"\n + Hello",null);
     } catch (BadLocationException ex) {
         Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
     }

第二期:使用HTML代码附加下标

输入excel文件中的一些变量有下标,所以我试图使用html语法在JTextPane中输出它们。我尝试过使用.setContentType("text/html")方法两次。第一个是在获得styledDocument之前。如果没有上面显示的try-catch语句,setContentType("text/html")可以正常工作,但是一旦我尝试实现上面的try-catch语句,内容类型就会恢复为默认值。

我发现stackoverflow上的以下问题很有帮助:

JTextPane append HTML string

我尝试以这种方式使用上述链接中的解决方案:

HTMLDocument doc=(HTMLDocument) displayResults.getStyledDocument();
   try {
       doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),s1);
   } catch (BadLocationException ex) {
       Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IOException ex) {
       Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
   }
    }

然而,我一直收到错误,告诉我:

Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: javax.swing.text.DefaultStyledDocument cannot be cast to javax.swing.text.html.HTMLDocument

我的完整代码

private void fullShapeTypesValueChanged(javax.swing.event.ListSelectionEvent evt) {                                            

StoreData d = new StoreData(); // Class of data
SubstringGenerator gen = new SubstringGenerator();

    labels = d.getLabels();    // Get the headers of each excel column   

    for (int i = 76; i >= 0 ; i--){


    int character = 0; // variable to help determine the number of characters of the label

    String s1 = "";
    String s2 = "";
    String s3 = "";

    character = labels[i].length();

    /* Any characters after the first one
     * will be converted to subscript using HTML.
     * second variable in gen.subscriptGen(a, b) --> "b" will be converted to
     * <html><sub>b</sub></html>
     */

    switch(character){
            case 1:
                s2 = labels[i];
                s1 = s2;
                break;
            case 2:
            case 3:
            case 4:
            case 5:
                s3 = gen.subscriptGen(s2, labels[i].substring(1));
                s1 = s3;
                break;        
        }

  HTMLDocument doc=(HTMLDocument) displayResults.getStyledDocument();
   try {
       doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()),s1);
   } catch (BadLocationException ex) {
       Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
   } catch (IOException ex) {
       Logger.getLogger(ShapeData.class.getName()).log(Level.SEVERE, null, ex);
   }
    }
displayResults.setContentType("text/html");
}              

1 个答案:

答案 0 :(得分:2)

对于第一个问题,请使用具有方法

的javax.swing.Utilities类
/**
 * Determines the starting row model position of the row that contains
 * the specified model position.  The component given must have a
 * size to compute the result.  If the component doesn't have a size
 * a value of -1 will be returned.
 *
 * @param c the editor
 * @param offs the offset in the document >= 0
 * @return the position >= 0 if the request can be computed, otherwise
 *  a value of -1 will be returned.
 * @exception BadLocationException if the offset is out of range
 */
public static final int getRowStart(JTextComponent c, int offs)

查找指定行号的所需偏移量。然后在insertString()

中使用找到的行start

对于第二个问题显然在某处您重置编辑器套件并且它不再是HTMLEditorKit。如果发生这种情况,我不能没有你的代码。尝试添加更多调试检查getDocument()的类,以找到重置EditorKit的位置和原因。