A.Sharma
A.Sharma

Reputation: 2799

Multiple Issues Appending to JTextPane

First Issue: Appending to Specific Row

I'm trying to append to a specific row in a JTextPane using the getStyledDocumentmethod. For example:

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

It seems that the offset 5 only offsets it 5 spaces horizontally. Is there a different method other than insertString that can achieve what I'm trying to do? This is also becoming an issue when I try to output rows from an excel spreadsheet in a downward direction. I've been resorting to adding "\n" before the specific String I want outputted as so.

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

Second Issue: Appending with Subscripts using HTML Code

Some of my variables from the input excel file have subscripts so I'm trying to output them in the JTextPane using html syntax. I have tried using the .setContentType("text/html") method twice. The first was before getting the styledDocument. Without the try-catch statement shown above, the setContentType("text/html") works, but as soon as I try to implement the try-catch statement above, the content type reverts back to default.

I have found the following question on stackoverflow to be helpful:

JTextPane append HTML string

I tried to use the solution in the aforementioned link in this manner:

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);
   }
    }

However I keep getting an error for this that's telling me:

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

MY FULL CODE

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");
}              

Upvotes: 0

Views: 194

Answers (1)

StanislavL
StanislavL

Reputation: 57381

For the first issue use javax.swing.Utilities class which has the method

/**
 * 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)

To find desired offset for specified row number. Then use the found row start in the insertString()

For the second issue obviously somewhere you reset the editor kit and it's not HTMLEditorKit anymore. Where it happens I can't suppose without your code. Try to add more debug checking getDocument()'s class to find where and why your EditorKit is reset.

Upvotes: 2

Related Questions