afficher les texte renvoyé par un parseur dans une jframe

cs_betoile Messages postés 28 Date d'inscription vendredi 11 juin 2010 Statut Membre Dernière intervention 22 mai 2013 - 25 juin 2011 à 13:28
 Utilisateur anonyme - 28 juin 2011 à 00:35
bonjour
package text;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.AttributeSet;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class bi extends DefaultHandler{

private static JTextPane textPane = new JTextPane();
//public String dossier = Fichier.dossier;
public String dossier="7.xml";
private boolean inName, ne_pas, intitle , existe = false, inCollectionLink;


static SimpleAttributeSet TITRE = new SimpleAttributeSet();

static SimpleAttributeSet SOUS_TITRE = new SimpleAttributeSet();

static SimpleAttributeSet PARAGRAPHE = new SimpleAttributeSet();

static SimpleAttributeSet LIEN = new SimpleAttributeSet();


static{
StyleConstants.setForeground(TITRE, Color.black);
StyleConstants.setFontFamily(TITRE, "Calibri");
StyleConstants.setFontSize(TITRE, 25);
StyleConstants.setBold(TITRE, true);
StyleConstants.setAlignment(TITRE, StyleConstants.ALIGN_CENTER);

StyleConstants.setForeground(SOUS_TITRE, Color.red);
StyleConstants.setFontFamily(SOUS_TITRE, "Times New Roman");
StyleConstants.setFontSize(SOUS_TITRE, 15);
StyleConstants.setBold(SOUS_TITRE, true);

StyleConstants.setForeground(PARAGRAPHE, Color.darkGray);
StyleConstants.setFontFamily(PARAGRAPHE, "Times New Roman");
StyleConstants.setFontSize(PARAGRAPHE, 16);
StyleConstants.setBold(PARAGRAPHE, true);
StyleConstants.setAlignment(PARAGRAPHE, StyleConstants.ALIGN_JUSTIFIED);

StyleConstants.setForeground(LIEN, Color.blue);
StyleConstants.setFontFamily(LIEN, "Times New Roman");
StyleConstants.setFontSize(LIEN, 16);
StyleConstants.setBold(LIEN, true);
StyleConstants.setAlignment(LIEN, StyleConstants.ALIGN_JUSTIFIED);
}
protected void insertText(String text, AttributeSet set) {
try {
textPane.getDocument().insertString(
textPane.getDocument().getLength(), text, set);
} catch (BadLocationException e) {
e.printStackTrace();
}
}

// Needed for inserting icons in the right places
protected void setEndSelection() {
textPane.setSelectionStart(textPane.getDocument().getLength());
textPane.setSelectionEnd(textPane.getDocument().getLength());
}

@Override
public void startElement(String uri,
String localName,
String qName,
Attributes attributes)throws SAXException{
setEndSelection();
if (qName.equals("name"))
inName = true;
else if(qName.equals("p"))
{
insertText("\n ", TITRE);
}
else if(qName.equals("title"))
{
intitle = true;
insertText("\n\n", SOUS_TITRE);
}
else if((qName.equals("languagelink"))||(qName.equals("image")))
ne_pas = true;
else if (qName.equals("collectionlink"))
inCollectionLink = true;
}

//detection fin de balise
@Override
public void endElement(String uri,
String localName,
String qName)
throws SAXException{
setEndSelection();
if (qName.equals("name"))
inName = false;
else if(qName.equals("title"))
{
intitle = false;
//y.set(1, y.get(1).concat("\n "));

}
else if(qName.equals("p"))
insertText("\n ", PARAGRAPHE);
else if ((qName.equals("languagelink"))||(qName.equals("image")))
ne_pas = false;
else if(inCollectionLink)
inCollectionLink = false;
}
//detection de caracteres


public class Bouton extends JPanel{
JLabel label = new JLabel();
private Color X,Y,Z;
public Bouton(String nom, Color x, Color y, Color z)
{
super();
label.setText(nom);
X = x;
Y = y;
Z = z;
}


@Override
public void paintComponent(Graphics g)
{

Graphics2D g2d = (Graphics2D) g;
GradientPaint k = new GradientPaint(0,this.getHeight()/4,
X, 0,this.getHeight(), Y);
g2d.setPaint(k);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight()/2);
k = new GradientPaint(0,this.getHeight()/4,
Y, 0,this.getHeight(), Z);
g2d.setPaint(k);
g2d.fillRect(0, this.getHeight()/2, this.getWidth(), this.getHeight());
label.setFont(new Font("Calibri", Font.BOLD, 20));
label.setForeground(Color.black);
this.add(label);
label.setVisible(true);
}
}
@Override
public void characters(char[] ch,
int start,
int length)
throws SAXException{
setEndSelection();
String lecture = new String(ch,start,length).replaceAll("\\s", " ");

String[] h = lecture.split(" ");
if (!lecture.equals(" ") && !ne_pas)
{


if (inName)
{
//insertText("\t\t\t",TITRE);
//setEndSelection();
textPane.setEditable(false);
Bouton titre1 = new Bouton(lecture.toUpperCase(), Color.white, Color.blue, Color.white);
titre1.setPreferredSize(new Dimension(textPane.getWidth(), 100));
textPane.insertComponent(titre1);
insertText("\n\n\t", TITRE);
//insertText(lecture.toUpperCase()+"\n\n", TITRE);

}
else if (intitle)
{
//insertText(lecture.toUpperCase()+"\n", SOUS_TITRE);
Bouton titre1 = new Bouton(lecture.toUpperCase(),Color.white, Color.red, Color.white);
titre1.setPreferredSize(new Dimension(textPane.getWidth(), 50));
textPane.insertComponent(titre1);
setEndSelection();
insertText("\n", SOUS_TITRE);
}
else if (inCollectionLink)
{
insertText(lecture+" ", LIEN);
}
else
insertText(lecture+" ", PARAGRAPHE);
}
}


//debut du parsing
@Override
public void startDocument() throws SAXException {

textPane = new JTextPane();
}
//fin du parsing
@Override
public void endDocument() throws SAXException {
}
public bi(){}

public void parser(File x) throws SAXException, IOException
{
XMLReader parseur = XMLReaderFactory.createXMLReader();
DefaultHandler gestionnaire = new bi();

// lecture d'un fichier XML avec un DefaultHandler
parseur.setContentHandler(gestionnaire);
parseur.parse(dossier+x.getName());

}
public JTextPane getElem()
{
return(textPane);
}



}
je ne c pas comment lui faire appel pour l'afficher

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
27 juin 2011 à 08:43
Je ne comprends rien à ta question. Faire appel à qui ? Pour afficher quoi ?
0
Utilisateur anonyme
28 juin 2011 à 00:35
Ça, ça sent le code récupéré sur Internet et mal bricolé...En plus, ce n'est absolument pas clair et c'est complétement illisible. Et les normes Java dont celle du camelCase ne sont pas respectées.

Refais un post clair en utilisant les balises pour afficher ton code proprement.


--
Pylouq
0
Rejoignez-nous