Formatage (paragraphes) dans un JTextPane

Formatage (paragraphes) dans un JTextPane - Java - Programmation

Marsh Posté le 12-05-2005 à 11:23:11    

bonjour,
j'utilise un JTextPane (que j'ai redefini) pour pouvoir afficher du texte selon ce format :


ceci est le titre sur une ligne
    ceci est le message qui peut etre
    sur plusieurs lignes!
 
 
ceci est un autre titre
    et ceci est un autre message
 
 
(...)


 
voici la methode qui permet de formater mon texte :

Code :
  1. public void setText(String title,String msg){
  2.  try{
  3.   doc.setParagraphAttributes(this.getDocument().getLength(),(title.length())+(msg.length()),doc.getStyle("para" ),false);
  4.   doc.insertString(this.getDocument().getLength(), title+"\r\n", doc.getStyle("title" ));
  5.   doc.insertString(this.getDocument().getLength(), msg, doc.getStyle("msg" ));
  6.   doc.insertString(this.getDocument().getLength(),"\r\n\r\n",null);
  7.  }
  8.  catch(BadLocationException e){ System.out.println(e.getMessage()); }
  9. }


 
et voici mes styles :

Code :
  1. Style titleStyle = doc.addStyle("title", null);
  2.  StyleConstants.setForeground(titleStyle,Color.GRAY);
  3.  StyleConstants.setItalic(titleStyle,true);
  4.  StyleConstants.setFontSize(titleStyle , 11);
  5.  Style msgStyle = doc.addStyle("msg",null);
  6.  StyleConstants.setFontSize(msgStyle,12);
  7.  Style parStyle = doc.addStyle("para",null);
  8.  StyleConstants.setFirstLineIndent(parStyle,0);
  9.  StyleConstants.setLeftIndent(parStyle,10);


 
mais visiblement ca marche pas!
j'obtiens toujours le meme resultat :


    ceci est le titre sur une ligne
    ceci est le message qui peut etre
    sur plusieurs lignes!
 
 
    ceci est un autre titre
    et ceci est un autre message
 
 
(...)


 
 
le titre et le message sont tout les deux decallé :(
 
 
je n'arrive pas a trouver d'aide sur le fonctionnement des paragraphes :(

Reply

Marsh Posté le 12-05-2005 à 11:23:11   

Reply

Marsh Posté le 12-05-2005 à 12:18:23    

reponse ici :

Code :
  1. public void setText(String title,String msg){
  2.  int l = this.getDocument().getLength();
  3.  try{
  4.   doc.insertString(this.getDocument().getLength(), title+"\r\n", doc.getStyle("title" ));
  5.   doc.insertString(this.getDocument().getLength(), msg, doc.getStyle("msg" ));
  6.   doc.setParagraphAttributes(l,(title.length()),doc.getStyle("para" ),false);
  7.   doc.insertString(this.getDocument().getLength(),"\r\n\r\n",null);
  8.  }
  9.  catch(BadLocationException e){ System.out.println(e.getMessage()); }
  10. }

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed