Link to home
Start Free TrialLog in
Avatar of gla
gla

asked on

change color on a selected text in jEditorPanel

I have a jEditorPanel with setContentType("text/html") and some text and I want to set color or change font (html) on selected text or set html code in cursor position.
any idea?
Avatar of sciuriware
sciuriware

I did:

      pane.setSelectedTextColor(Color.BLACK);
      pane.setSelectionColor(Color.YELLOW);

;JOOP!
Avatar of gla

ASKER

ok this is true if added the html code..

but i want to insert text (html) like "<strong>" in a cursor position for example:

the text in jEditorPanel is:

language JAVA

the cursor position is between language and java like:

language | java

and when I click on a button the text will change to:

language <strong> java..
The simplest way is to collect the contents             String x = pane.getText();
know the caret position              pane.getCaretPosition();
to insert or replace the text you want
and then put it back                 pane.setText(x);

;JOOP!
pane.getDocument().insertString(pane.getCaretPosition(), your text here);
Avatar of gla

ASKER

hoomanv

the insertString() has another on option the Attribute
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gla

ASKER

the comments that help me is the following(I accept wrong comment by mistake):

hoomanv:
http://java.sun.com/docs/books/tutorial/uiswing/components/example-1dot4/index.html

see the TextComponentDemo
http://java.sun.com/docs/books/tutorialJWS/uiswing/components/example-1dot4/TextComponentDemo.jnlp

hoomanv:
pane.getDocument().insertString(pane.getCaretPosition(), "your text here", null);