1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class MonTexte extends JFrame {
private static final long serialVersionUID = 1L;
String original = "Un agneau se désaltérait dans le courant d'une onde pure mais un méchant loup attendait sa proie qui ne se doutait pas tout de suite qu'il était surveillé. La suite de cette histoire est bien connue...";
public MonTexte() {
initialize();
}
private void initialize() {
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
JEditorPane Memo = new JEditorPane();
Memo.setContentType("text/html");
JScrollPane scrollPane = new JScrollPane(Memo);
scrollPane.setBounds(41, 11, 351, 193);
getContentPane().add(scrollPane);
Memo.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), HTMLEditorKit.defaultKeyTypedAction);
Memo.getActionMap().put(HTMLEditorKit.defaultKeyTypedAction,
new TextAction(HTMLEditorKit.defaultKeyTypedAction) {
Action defaultAction = new HTMLEditorKit.DefaultKeyTypedAction();
@Override
public void actionPerformed(ActionEvent e) {
if ( e.getActionCommand().equals("\n") ) {
JTextComponent target = getTextComponent(e);
if ((target != null) && (e != null)) {
if ((!target.isEditable()) || (!target.isEnabled())) {
return;
}
target.replaceSelection("");
HTMLDocument htmldoc = ((HTMLDocument) target.getDocument());
Element elem = htmldoc.getCharacterElement(target.getCaretPosition());
if ( target.getCaretPosition()==elem.getEndOffset() ) {
try {
htmldoc.insertAfterEnd(elem, "<br>");
} catch (BadLocationException | IOException e1) {
e1.printStackTrace();
}
}
else if ( target.getCaretPosition()==elem.getStartOffset() ) {
try {
htmldoc.insertBeforeStart(elem, "<br>");
} catch (BadLocationException | IOException e1) {
e1.printStackTrace();
}
}
else {
HTMLEditorKit kit = (HTMLEditorKit)((JEditorPane)target).getEditorKit();
try {
kit.insertHTML( htmldoc,
target.getCaretPosition(),
"<br>",
0,
0,
HTML.Tag.BR);
} catch (BadLocationException | IOException e1) {
e1.printStackTrace();
}
}
Element newelem = htmldoc.getCharacterElement(target.getCaretPosition());
target.setCaretPosition(newelem.getStartOffset());
}
}
else {
defaultAction.actionPerformed(e);
}
}
}
);
Memo.setText(original);
JButton Test = new JButton("Tester");
Test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String wk = Memo.getText();
System.out.println(wk);
Memo.setText(wk);
}
});
Test.setBounds(286, 215, 89, 23);
getContentPane().add(Test);
;
this.setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new MonTexte();
}
} |
Partager