0

Ok, I am writing a text editor using a JTextPane. I am trying to figure out how to change the font size for specific text that the user types in. I have a JButton that the user can click to bring up a font-size and font-type selection menu. When the user selects the proper font size and type and presses ok, I then proceed to get the font size and type inside a Font. All I need to do now is to set the Font of the selected text in the JTextPane without overriding the current style(s) of the text. I haven't found anything that enables me to do this. Maybe I just missed something somewhere...

Thanks in advance.

Note: I am a newbie on StackOverFlow so please edit my question if I did something wrong.

  • 1
    try to use the function setFont. more information in http://stackoverflow.com/questions/10585956/jtextpane-how-to-set-the-font-size – Konstantin Purtov Aug 09 '16 at 19:53
  • http://stackoverflow.com/questions/1905461/java-jtextpane-change-font-of-selected-text there's a similar question. – twodee Aug 09 '16 at 19:59
  • `textpane.setFont(font);` where textpane = jtextpane; and font = java.awt.Font[family=Serif,name=Serif,style=plain,size=20] doesn't work for me... – onelittleprogrammer Aug 09 '16 at 20:04
  • @CoderDudeTwodee in that answer you get the styled document and then add a style that contains the selected text to where the selected text is, I figured out how to do that, but it still erases all previous styles... – onelittleprogrammer Aug 09 '16 at 20:21

2 Answers2

0

Did you tried with this.

replaceSelection()

http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#replaceSelection(java.lang.String)

GlacialMan
  • 579
  • 2
  • 5
  • 20
  • I have tried this, the problem with this is you have to getSelectedText and save it as a String. when I do that, I lose all previous styles of text. Is there a way to getSelectedText and save it with all current styles and then "overwrite" any styles I need to? – onelittleprogrammer Aug 09 '16 at 20:13
0

Use a styled editor kit and fire the actions inside the actionperformed method.