UIManager.put("EditorPane.foreground",Color.RED);
UIManager.put("EditorPane.background", Color.BLUE);
UIManager.put("EditorPane.selectionBackground", Color.GREEN);
UIManager.put("EditorPane.selectionForeground", Color.WHITE);
It sounds as though you really want to use a JTextPane instead of a JEditorPane. JEditorPane is great for displaying already formatted data, but less that fantastic if you want to manipulate things yourself.
If you do use JTextPane, the feature involves using the underlying StyledDocument model that underlies the JTextPane;
JTextPane myTextPane = new JTextPane();
StyledDocument myStyledDocument = myTextPane.getStyledDocument();
Now that you have the underlying StyledDocument model, you're going to want to use that to insert styled text using the
myStyledDocument.insertString(0,"myText",myAttributeSet);
method. It takes the offset (the location in the document to insert...(in this case we use 0 for the beginning), then "myText" as the string, and the "attribute set" which determines how the string should appear...its orientation, its style, its font, its foreground color, background color, etc.
To handle the attribute set you create one first, we'll use a SimpleAttributeSet
SimpleAttributeSet myAttributeSet = new SimpleAttributeSet();
to manipulate the attribute set use the StyleConstants class (a set of useful static methods for changing the attribute set because there isn't good documentation in the API for changing the attribute set yourself...StyleConstants pretty much has everything you'll need though...)
So let's change the foreground color to Red so that you have red text
you would do
StyleConstants.setForeground(myAttributeSet,Color.red);
which tells the attribute set to have a foreground color of red. There's a million other functions in StyleConstants to browse through (like I said including setting the alignment, background, highlighting etc).
now you have an attribute set to insert red colored text into the StyledDocument which will be displayed by JTextPane
so just run the myStyledDocument.insertString(0,"myText",myAttributeSet);
method and it will insert it and report a change event to the TextPane to let it know to draw the new text...
I hope this helps! I'm fairly new to all this too, so I hope I didn't steer you in the wrong direction.
Message was edited by: mr.v.
Note: to set the selection colors then you use the methods in JTextPanesetSelectedTextColor(Color) and setSelectionColor(color)
I am making skins. The textpanes are HTML texts having the default foreground font color. So it has to be implemented without html texts being regenerated! I tried this;
MutableAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, text_foreground);
StyledDocument doc = xxxx.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength(), keyWord, true);
But not working.
Well I still have no real idea what you are attempting to do or how you are going about it, so I guess I won't be of much help anymore.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Let's say, I have a text pane create as follows containing the html text;
JTextPane p = new JTextPane();
p.setEditable(false);
p.setContentType("text/html");
p.setText("<HTML><H2>my title</H2> blah blah blah
.....
Later on, a user may choose a different color UI skin menu, then codes similar to the following may be executed;
p.setForeground(Color.red);
p.setBackground(Color.blue);
UIManager.setLookAndFeel(skinname);
or
UIManager.put("EditorPane.foreground",Color.RED);
UIManager.put("EditorPane.background", Color.YELLOW);
UIManager.put("TextPane.foreground",Color.RED);
UIManager.setLookAndFeel(skinname);
This should be changing text pane background as "blue" with "red" text fonts. This actually works for background. But not for foreground!
Thanks. From the reference, I was able to figure out a solution as follows;
MutableAttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.red);
StyledDocument doc = textPane.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength(), keyWord, true);