J'ai donc JTextPane et j'ai ajouté un keyListener, comme ça, je peux savoir si le bouton Enter a été pressé:Ajouter un caractère (ou une chaîne) lorsque vous appuyez sur Entrée (dans un JTextPane)
JTextPane textPane = new JTextPane();
textPane.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
// add there the code to add a character to the textPane!
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
Mais maintenant je suis bloked, comment ajouter un caractère '}' au panneau de texte?
(nulle part, juste après la position du curseur, à ce qui suit ...)
N'utilisez jamais un KeyListener avec un composant de texte tel que JTextPane. Si vous voulez écouter ENTER, utilisez Key Bindings - ou un DocumentListener ou peut-être ici un DocumentFilter. –
Cette question a la solution que vous cherchez, je crois https://stackoverflow.com/questions/4059198/jtextpane-appending-a-new-string –