IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Comment faire une coloration syntaxique ?


Sujet :

AWT/Swing Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2007
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2007
    Messages : 10
    Points : 5
    Points
    5
    Par défaut Comment faire une coloration syntaxique ?
    Bonjours,

    je suis entrain de crée une application java où on peut taper du code.
    Dans un souci de clarté, je voudrais que les mots clés soient colorées. Mais je ne sais pas comment colorer plusieurs mots dans un texte.
    Quelle classe me conseillez-vous ? Et quelle fonction peut colorer un partie du texte uniquement?

    Merci de votre aide.

  2. #2
    Membre expert
    Avatar de Janitrix
    Inscrit en
    Octobre 2005
    Messages
    3 391
    Détails du profil
    Informations forums :
    Inscription : Octobre 2005
    Messages : 3 391
    Points : 3 401
    Points
    3 401
    Par défaut
    Je te conseil d'utiliser JTextPane qui permet de donner un style différent à plusieurs parties du texte. C'est un composant très permettant de multiples opérations. Cependant, le composant en lui même est assez déroutant. J'avais trouvé une implémentation facilitant son utilisation, je ne sais plus où, la voilà :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    package org.janitrix.ui;
     
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
     
    public class AdvancedTextPane extends JTextPane {
     
    	//CLASS VAR.
    	static final long serialVersionUID = 0;
    	//PRIVATE VAR.
    	private StyledDocument styledDoc = getStyledDocument();
    	//PROTECTED VAR.
    	protected float COMPONENT_ALIGNEMENT = 0.82f;
     
    	/**
             * Invoked for generate a style name.
             * @param bold
             * @param italic
             * @param underline
             * @param size
             * @param color
             * @return
             */
    	private static String getNameStyle(boolean bold, boolean italic, boolean underline, int size, Color color) {
    		StringBuffer sb = new StringBuffer();
    		if(bold) {
    			sb.append("1");
    		}else {
    			sb.append("0");
    		}
    		if(italic) {
    			sb.append("1");
    		}else {
    			sb.append("0");
    		}
    		if(underline) {
    			sb.append("1");
    		}else {
    			sb.append("0");
    		}
     
    		sb.append(size);
    		sb.append(color.getRGB());
     
    		return sb.toString();
    	}
     
    	/**
             * Invoked for create and bet back the style define by the parameters.
             * @param bold
             * @param italic
             * @param underline
             * @param size
             * @param color
             * @return
             */
    	private Style getStyle(boolean bold, boolean italic, boolean underline, int size, Color color) {
     
    		String styleName = AdvancedTextPane.getNameStyle(bold, italic, underline, size, color);
     
    		Style style = styledDoc.getStyle(styleName);
     
    		if(style != null) {
    			return style;
    		}else {
    			style = styledDoc.addStyle(styleName, styledDoc.getStyle(StyleContext.DEFAULT_STYLE));
     
    			StyleConstants.setBold(style, bold);
    			StyleConstants.setItalic(style, italic);
    			StyleConstants.setUnderline(style, underline);
    			StyleConstants.setFontSize(style, size);
    			StyleConstants.setForeground(style, color);
     
    			return style;
    		}
    	}
     
     
     
    	/**
             * Invoked for add a text to the end of the text pane.
             * @param texte
             * @param bold
             * @param italic
             * @param underline
             * @param size
             * @param color
             */
    	public synchronized void insertTextEnd(String texte, boolean bold,
    			boolean italic, boolean underline, int size, Color color) {
    			try {
    				styledDoc.insertString(styledDoc.getLength(),
    						texte, getStyle(bold, italic, underline, size, color));
    				setCaretPosition(styledDoc.getLength());
    			}catch(Exception e) {}
    	}
     
     
    	/**
             * Invoked for insert a icon at the selection space.
             * @param icon
             */
    	public synchronized void insertIconSelect(Icon icon) {
    		insertIcon(icon);
    	}
     
     
    	/**
             * Invoked for add a icon to the end of the text pane.
             * @param icon
             */
    	public synchronized void insertIconEnd(Icon icon) {
    		select(styledDoc.getLength(), styledDoc.getLength());
    		insertIconSelect(icon);
    	}
     
     
    	/**
             * Invoked for insert a component at the selection place.
             * @param c
             */
    	public synchronized void insertComponentSelect(JComponent c) {
    		c.setAlignmentY(COMPONENT_ALIGNEMENT);
    		insertComponent(c);
    	}
     
    	/**
             * Invoked for add a component to the end of the text pane.
             * @param c
             */
    	public synchronized void insertComponentEnd(JComponent c) {
    		select(styledDoc.getLength(), styledDoc.getLength());
    		insertComponentSelect(c);
    	}	
     
    }
    Les commentaires sont assez explicites. Bonne chance.

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2007
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2007
    Messages : 10
    Points : 5
    Points
    5
    Par défaut
    Merci.

    Je ne me suis jamais servis de JTextPane au par avant. Et je me suis rendu compte que awt.event.TextListener ne fonctionne pas avec les JTextPane.
    Que dois je utiliser pour intercepter les evenements ? Auriez vous un tutoriel qui traite des JTextPane ?

  4. #4
    Membre régulier
    Inscrit en
    Octobre 2006
    Messages
    108
    Détails du profil
    Informations forums :
    Inscription : Octobre 2006
    Messages : 108
    Points : 118
    Points
    118
    Par défaut
    J'ai posté le même genre de question la semaine dernière, et en fait j'ai récupéré du code trouvé sur le net que j'adapte. Je me suis basé sur jalopy (sourceforge). Sinon, tu peux regarder jedit et jext qui doivent se baser sur la même chose que jalopy.

Discussions similaires

  1. Comment faire une interface de ce type....
    Par SpiderAlpha dans le forum C++Builder
    Réponses: 6
    Dernier message: 30/04/2007, 13h50
  2. Réponses: 18
    Dernier message: 20/08/2006, 14h35
  3. Réponses: 2
    Dernier message: 03/05/2004, 12h13
  4. [VB6] Comment faire une fonction qui renvoie 2 résultats
    Par tazarine dans le forum VB 6 et antérieur
    Réponses: 10
    Dernier message: 15/01/2004, 00h13
  5. Réponses: 10
    Dernier message: 10/10/2003, 14h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo