The problem with the key release event in the KeyListener interface is that it is invoked only when the user let go of the key. In your code, if the user held onto a key to repeat it, only the last character will be modified according to the set style. Also, during typing, if the user press the second key before releasing the first one, again only the last character is updated.
One alternate approach is to invoke your code within the keyTyped method in KeyListener interface.
Also simplified the code a bit.
private void textoKeyReleased(javax.swing.event.CaretEvent evt) {
int end = textPane.getSelectionEnd();
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.getStyle("negra");
StyleConstants.setBold(style, car == 1);
doc.setCharacterAttributes(end - 1, 1, style, false);
textPane.requestFocus();
}
@Override
public void keyTyped(KeyEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
textoKeyReleased(null);
}
});
}