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
| // appeler le fichier Exemple102_HighlightCaretLine.java
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
// Nicolas_75
// dimanche 23 septembre 2007
// pour http://www.developpez.net/forums/showthread.php?t=414305
// voir aussi :
// http://java.sun.com/docs/books/tutorial/uiswing/components/textapi.html#carrots
// http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TextFieldDemoProject/src/components/TextFieldDemo.java
public class Exemple102_HighlightCaretLine extends JFrame {
private static final String INITIAL_TEXT = "La terre est bleue comme une orange...\n\nLa terre est bleue comme une orange\nJamais une erreur les mots ne mentent pas\nIls ne vous donnent plus à chanter\nAu tour des baisers de s'entendre\nLes fous et les amours\nElle sa bouche d'alliance\nTous les secrets tous les sourires\nEt quels vêtements d'indulgence\nÀ la croire toute nue.\n\nLes guêpes fleurissent vert\nL'aube se passe autour du cou\nUn collier de fenêtres\nDes ailes couvrent les feuilles\nTu as toutes les joies solaires\nTout le soleil sur la terre\nSur les chemins de ta beauté.\n\nPaul ELUARD, L'Amour la poésie (1929)";
private static final int TEXT_COMPONENT_WIDTH = 400;
private static final int TEXT_COMPONENT_HEIGHT = 250;
private static final Color HIGHLIGHT_COLOR = Color.YELLOW;
final private JTextArea textarea = new JTextArea();
final private Highlighter highlighter = new DefaultHighlighter();
final private Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(HIGHLIGHT_COLOR);
// constructeur
public Exemple102_HighlightCaretLine() {
// mise en place de la zone de texte :
textarea.append(INITIAL_TEXT);
textarea.setHighlighter(highlighter);
// ajout d'un listener réagissant aux mouvement du curseur
// il appelle la méthode de surlignement
textarea.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
highlightCurrentLine();
}
});
// mise en place du contenu de la frame :
textarea.setEditable(true);
JScrollPane scroll = new JScrollPane(textarea);
scroll.setPreferredSize(new Dimension(TEXT_COMPONENT_WIDTH ,TEXT_COMPONENT_HEIGHT));
this.getContentPane().add(scroll);
// préparation de la frame :
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null); // pour center
this.setVisible(true);
// surlignement initial :
highlightCurrentLine();
}
// méthode procédant au surlignement de la ligne en cours
// elle est appellée à chaque mouvement de curseur
private void highlightCurrentLine() {
highlighter.removeAllHighlights();
try {
int lineNumber = textarea.getLineOfOffset(textarea.getCaretPosition());
highlighter.addHighlight(
textarea.getLineStartOffset(lineNumber),
textarea.getLineEndOffset(lineNumber),
painter);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
// "main" de démonstration
public static void main(String[] args) {
new Exemple102_HighlightCaretLine();
}
} |
Partager