First Issue: Appending to Specific Row
I'm trying to append to a specific row in a JTextPane using the getStyledDocument
method. 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:
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");
}