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
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.regex.*;
public class Test1 extends JFrame implements ItemListener
{
JEditorPane jep = new JEditorPane();
JCheckBox cb1;
public Test1() {
super();
cb1 = new JCheckBox("Surligner TOTO");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200,200);
this.getContentPane().add(jep,BorderLayout.CENTER);
this.getContentPane().add(cb1,BorderLayout.SOUTH);
cb1.addItemListener(this);
jep.setContentType( "text/html" );
jep.setText("toto");
System.out.println(jep.getText());
this.setVisible(true);
}
public static void main(String[] args) {
Frame f = new Test1();
}
public void itemStateChanged(ItemEvent e) {
String contenu = jep.getText();
if(e.getStateChange()==ItemEvent.SELECTED) {
if (cb1== e.getItem()){
Pattern regex = Pattern.compile("toto");
Matcher match = regex.matcher(contenu);
boolean b = match.find();
if (b == true) {
String matchFound = match.group();
String matchColored ="<font size=\"4\" style bgcolor=\"blue\" color=\"white\">"+matchFound+"</font>";
contenu = contenu.replaceAll(matchFound, matchColored);
}
}
jep.setText(contenu);
}
if (e.getStateChange() == ItemEvent.DESELECTED){
if (cb1 == e.getItem()){
Pattern regex = Pattern.compile("<font(.*)>(.*)</font>");
Matcher match = regex.matcher(contenu);
boolean b = match.find();
if (b == true) {
//on ne rentre jamais dans ce if
String matchFound = match.group();
contenu = contenu.replaceAll(matchFound, match.group(2));
}
}
jep.setText(contenu);
}
}
} |