This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane problem  RSS feed

 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to write a parser to parse out ANSI color escape sequences from a buffer and apply them to a screen(JTextPane).
I am at the point where the parser detect the colors and I am applying them back to the screen. My problem is that when I set my forground color to change the color of the text, it changes the forgrond color, but the background color also changes too. So instead of getting green text on a black background for example, I get a green block, then when it attempts to turn the text back to white on black I get white blocks, so my screen consists of white and green blobs.
I don't think this is a big deal, I'm guess I'm just missing something small, I hope somebody can point me in the right direction.
Robert
the code i'm using is like this:

private static StringBuffer hldBuffer = new StringBuffer();
private static SimpleAttributeSet a = new SimpleAttributeSet();
if (color.equals("0")) // normal display
{
StyleConstants.setBold(a, false);
StyleConstants.setUnderline(a, false);
StyleConstants.setItalic(a, false);
StyleConstants.setBackground(a, Color.black);
StyleConstants.setForeground(a, Color.white);
}
else if (color.equals("32")) // green foreground
StyleConstants.setForeground(a, Color.green);
super.getStyledDocument().insertString(
super.getStyledDocument().getLength(),
hldBuffer.toString(),
a);
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the following code, but getStyledDocument() method has to return a DefaultStyledDocument object. If the object is not the DefaultStyledDocument, I think you should use setDocument(new DefaultStyledDocument) for your JTextPane:
StyleContext m_sc = new StyleContext();
Style def = m_sc.getStyle(StyleContext.DEFAULT_STYLE);
Style style = m_sc.getStyle("mystyle");
if (style == null) {
style = m_sc.addStyle("mystyle", def);
}
if (color.equals("0")) // normal display
{
StyleConstants.setBold(style, false);
StyleConstants.setUnderline(style, false);
StyleConstants.setItalic(style, false);
StyleConstants.setBackground(style, Color.black);
StyleConstants.setForeground(style, Color.white);
} else {
StyleConstants.setForeground(style, Color.green);
}
getStyledDocument().insertString(getStyledDocument().getLength(), hldBuffer.toString(), m_sc.getStyle("mystyle"));
Regards.
 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the code and I get the same outcome.
For whatever reason when I set my foreground it changes the background to be the same.
With both sets of code I can select the text on screen and because the selected text has a different colored background I can actually read the text and see the colors applied. The applied colors are correct and all of the text is there, I just need to know how to stop the background and foreground from being the same.
Robert
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
It's a bug of Swing. JTextPane's background color doesn't work with Swing of JVM1.2.2 or early. This bug is fixed in Swing of JVM1.3.0 or later. So you should use JVM1.3.0.
Good luck.
 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it, I had the IBM JRE v1.2.2 running as well and it was first in my path. I switched it around to use the Sun JRE and it worked perfectly.
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!