I have a text that needs to be formatted, and the first word of the text needs to be bold with large font and centered.
To do this formatting i am using the solution from TextSamplerDemo.java in the oracle tutorial of the JTextComponents, the solution works rather well but the centering doesn't work!
Now i know that there are answers already in Stack Overflow about aligning text in a JTextPane and on other forums but they are all solutions about aligning all the text, and there are non about aligning one word or a part of the text.
Again, the font, the size and the "boldness" (don't know the correct term but you understand what i mean ;-p ) they all work, but the centering doesn't.
Here is the code that i am using to set up the JTextPane:
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JInternalFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import domain.Fiche_Employe;
import persistance.Lecture_Fiche;
public class Attestation extends JInternalFrame {
/**
* Launch the application.
*/
/**
*
*/
private static final long serialVersionUID = 1L;
JTextPane attest;
/**
* Create the frame.
*/
public Attestation(String mat) {
//This a DataBase connection and data fetching
Fiche_Employe fiche=new Fiche_Employe();
Lecture_Fiche f=new Lecture_Fiche(mat, fiche);
f.lire_fiche();
//Frame Creation
setBounds(100, 100, 450, 300);
setVisible(true);
//Creation of the JtextPane
attest=createTextPane(fiche.getSociete(), fiche.getSexe(), fiche.getnom(), String.valueOf(fiche.getcnss()), new SimpleDateFormat("dd MMMMMMMMM yyyy", Locale.FRANCE).format(fiche.getDateEntree()), fiche.getQualification(),fiche.getCategorie(), fiche.getEchelon(), fiche.getSituationProf());
getContentPane().add(attest, BorderLayout.CENTER);
}
private JTextPane createTextPane(String code, String sexe, String nomPrenom, String cnss, String dateEntree,
String Qualif, String Categ, String ech, String SituatProf) {
String civilite = null;
String Societe;
if (sexe.replaceAll("\\s+$", "").toLowerCase().equals("m"))
civilite="Monsieur ";
else if (sexe.replaceAll("\\s+$", "").toLowerCase().equals("f"))
civilite="Madame ";
if (code.replaceAll("\\s+$", "")=="200")
Societe="text";
else
Societe="text";
String newline = "\n";
String[] initString =
{ "ATTESTATION",
newline+newline+newline+"Nous soussignés, "+Societe+" attestons que Monsieur ",
nomPrenom.replaceAll("\\s+$", ""),
", immatriculé à la caisse Nationale de Sécurité Sociale sous le numéro ",
cnss.replaceAll("\\s+$", ""),
", travaille dans notre société depuis le ",
dateEntree+ "." + newline+newline,
civilite, //regular
nomPrenom.replaceAll("\\s+$", ""),
" est employé actuellement en qualité de " +
Qualif.replaceAll("\\s+$", "") + " "+
SituatProf.replaceAll("\\s+$", ""),
" catégorie "+Categ.replaceAll("\\s+$", ""),
" échelon "+ech.replaceAll("\\s+$", ""),
", conformément à la Convention Collective Nationale de l’Industrie Laitière et Dérivés.",
newline +newline,
"Cette attestation est délivrée à l’intéressé, sur sa demande, pour servir et valoir ce que de droit."
};
String[] initStyles =
{ "centeredBold", "regular", "regular", "regular", "regular",
"regular", "regular", "regular", "regular",
"regular", "regular", "regular", "regular", "regular", "regular"
};
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText("<html><center><b> </b></center></html>");
StyledDocument doc = textPane.getStyledDocument();
addStylesToDocument(doc);
try {
for (int i=0; i < initString.length; i++) {
doc.insertString(doc.getLength(), initString[i],
doc.getStyle(initStyles[i]));
}
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text into text pane.");
}
return textPane;
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "Calibri");
StyleConstants.setFontSize(regular, 16);
Style s = doc.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = doc.addStyle("bold", regular);
StyleConstants.setBold(s, true);
doc.addStyle("centeredBold", regular);
SimpleAttributeSet center=new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
StyleConstants.setBold(center, true);
StyleConstants.setFontSize(center, 26);
StyleConstants.setFontFamily(center, "Cambria");
doc.getStyle("centeredBold").addAttributes(center);
s = doc.addStyle("small", regular);
StyleConstants.setFontSize(s, 10);
s = doc.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);
s = doc.addStyle("icon", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
s = doc.addStyle("button", regular);
StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
}
}
Any help would be appreciated and thank you in advance.