I solved my problem with this method:
public void styleFont(boolean bold, boolean under, boolean italic,String fontFamaly,Color color, int size,int tipo){
String textSelected = null;
int count = 0;
int start = 0;
int end = 0;
try{
end = seuJTextPane.getSelectionEnd();
start = seuJTextPane.getSelectionStart();
count = seuJTextPane.getSelectedText().length();
textSelected= seuJTextPane.getSelectedText();
}catch(NullPointerException e){
}
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setBold(attributes, bold);
StyleConstants.setUnderline(attributes, under);
StyleConstants.setItalic(attributes, italic);
StyleConstants.setFirstLineIndent(attributes, 400);
if(tipo == 1){
StyleConstants.setFontFamily(attributes, fontFamaly);
StyleConstants.setForeground(attributes,color);
StyleConstants.setFontSize(attributes, size);
}
if (textSelected != null) {
try {
seuJTextPane.getStyledDocument().remove(start,count);
} catch (BadLocationException ex) {
Logger.getLogger(Create.class.getName()).log(Level.SEVERE, null, ex);
}
try {
seuJTextPane.getStyledDocument().insertString(start,textSelected , attributes);
seuJTextPane.select(start, end);
seuJTextPane.setSelectedTextColor(color);
} catch (BadLocationException ex) {
Logger.getLogger(Create.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
try {
seuJTextPane.getStyledDocument().insertString(tp.getStyledDocument().getLength()," " , attributes);
} catch (BadLocationException ex) {
Logger.getLogger(Create.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
managed using the Simpleattributeset class
– Fabiano Araujo