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 :

[Swing][xml]transformer xml en composant swing


Sujet :

AWT/Swing Java

  1. #1
    Nouveau membre du Club
    Inscrit en
    Février 2006
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 52
    Points : 34
    Points
    34
    Par défaut [Swing][xml]transformer xml en composant swing
    voilà je recois un flux xml, je le parse grace à l'api SAX et j'aimerais créer des composant le problème se situe au composants "groupe" ds le flux (panel ds swing) il faut ajouter les bon composants aux bon groupes...

    voilà le flux...

    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
     <?xml version="1.0" encoding="iso-8859-1"  ?> 
       <screen function="HR/SEH">
        <authorizations add="y" dup="y" del="y" save="y" print="y" /> 
       <parameters>
        <authors create="y" update="y"  lastused="n" /> 
       </parameters>
       <statdesc>
        <object id="GRP_1"  type="group" line="2" col="1" lineright="2" colright="1" border="n">
         <object id="HORSECT"  type="cbx"  line="1"  col="1"  length="40" visiblelength="35" format="A" oblig="y" key="y" load="HRPSECH_IND1"  /> 
        <object id="BTN_TV"  type="button" line="1" col="2" length="2" fctbtn="DISPTV" modal="HR/TVW"  key="y"  /> 
        <object id="LIBCOURT"  type="edit" line="1" col="3" length="20" format="a" oblig="y" /> 
       </object>
       <object id="GRP_2"  type="group" line="3" col="1" lineright="3" colright="1" border="y">
         <object id="DATTRAI"  type="date" line="1" col="1" /> 
        <object id="HORCODE"  type="cbx"  line="1"  col="2"  length="6"  format="A"  oblig="y"  zoom="HR/HOR" load="HRPHORC_IND1" lnk="LIBHORA" />  
        <object id="LIBHORA"  type="edit" line="1" col="3" length="40" format="a" protect="y" />  
        <object id="RANG"  type="edit" line="1" col="4" length="2" format="N" oblig="y" spin="y" max="99" min="1" /> 
        <object id="PROFIL"  type="edit" line="2" col="2" length="6" format="A" protect="y" /> 
        <object id="LIBPROF"  type="edit" line="2" col="3" length="40" format="a" protect="y" />  
        <object id="ATTRIB"  type="edit" line="3" col="2" length="6" format="A" protect="y" /> 
        <object id="LIBATTR"  type="edit" line="3" col="3" length="40" format="a" protect="y" />  
        <object id="LIBELLE"  type="edit" line="4" col="3" length="40" format="a" /> 
       </object>
        <object id="GRP_3"  type="pagectrl" line="4" col="1" lineright="4" colright="1" border="y" />  
       </statdesc>
        <dyndesc />  
        <authdesc />  
       </screen>
    voilà le code pr le parser....

    classe AnalyseSax...

    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
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
     //Parse un document XML en Java avec l'API SAX  
    //on importe les APIs necessaires  
    //pour l'analyse du XML  
    import org.xml.sax.*; 
    import org.xml.sax.helpers.DefaultHandler; 
     
    import javax.swing.*;
    import java.awt.*; 
    import java.util.Vector;
     
    public class AnalyseSAX extends DefaultHandler{ 
     
        static JFrame fenetreTest = new JFrame("Test");
        JPanel jpGroupe1;
        JPanel jpGroupe2;
        JPanel jpGroupe3;
        JPanel tabPanel[] = new JPanel[3];
        JComboBox cbx;
        JTextField tf;
        Vector vectComponent = new Vector();
        Vector vectGroupe = new Vector();
        int numero=1;
        int num=0;
        int id=1;
        int comp=1;
        JPanel groupe;
     
     
     
        public static JFrame constructionFenetre(){
            fenetreTest.setSize(800,600);
     
            /*JPanel b = new JPanel();
             b.setBackground(Color.red);
             b.setPreferredSize(new Dimension(700,50));
             JComboBox t1 = new JComboBox();
             JTextField t2 = new JTextField();
             JTextField t3 = new JTextField();
             JButton b1 = new JButton();
             JSpinner s1 = new JSpinner();
             
             b1.setPreferredSize(new Dimension(20,22));
             s1.setPreferredSize(new Dimension(40,22));
             t1.setPreferredSize(new Dimension(80,22));
             t2.setPreferredSize(new Dimension(340,22));
             t3.setPreferredSize(new Dimension(51,22));
             b.add(t1);
             b.add(t2);
             b.add(b1);
             b.add(t3);
             b.add(s1);*/
     
     
     
            return fenetreTest;
        }
     
     
     
     
     
     
        // Les méthodes qui suivent sont appelées   
        // automatiquement par l'analyseur   
        // lorsqu'un événement est détecté   
        //dans le fichier XML. 
        public void startDocument() throws SAXException {
     
            System.out.println( "debut du document");  
     
        }  
     
     
     
     
     
     
        public void endDocument() throws SAXException {
            fenetreTest.add(groupe);
            System.out.println( "fin du document");  
        }  
     
     
     
     
     
     
     
        /**@param String namespaceURI 
         * @param String sName  simple name 
         * @param String  qName qualified name 
         * @param Attributes  attrs
         */
     
        public void startElement(String namespaceURI, String sName, String  qName, Attributes  attrs) throws SAXException {  
            String eName = sName; // element name  
     
            if ("".equals(eName)) {  
                eName  = qName; // not namespaceAware 
     
            }
     
     
            num++;
            System.out.println( "balise ouvrante: " + eName + num);  
     
     
     
     
     
            if ("object".equals(eName)){
     
     
     
                if (attrs != null) {  
                    //  Listage des attributs  
                    if (attrs.getLength() != 0) {  
                        System.out.println( "listage des parametres pour la balise " + eName + ":");  
                    }   
     
                    for (int i = 0; i < attrs.getLength(); i++) {  
                        String  aName = attrs.getLocalName(i);  
                        //   Récupération du nom de l'attribut  
                        if ("".equals(aName)) {  
                            aName  = attrs.getQName(i);  
                        }  
     
                        System.out.println( "  " + aName + "=\"" + attrs.getValue(i) + "\""); 
     
                        if("type".equals(aName)){
                            if("group".equals(attrs.getValue("type"))||"pagectrl".equals(attrs.getValue("type"))){
                                System.out.println("création du groupe numéro"+id);
                                groupe = new JPanel();
                                groupe.setBackground(Color.red);
     
                            }
     
                            if("cbx".equals(attrs.getValue("type"))){
                                cbx = new JComboBox();
                                groupe.add(cbx);
                                System.out.println("création composant"+comp);
                                comp++;
                            }
     
                            if("edit".equals(attrs.getValue("type"))||"date".equals(attrs.getValue("type"))){
                                tf = new JTextField();
                                groupe.add(tf);
                                System.out.println("création composant"+comp);
                                comp++;
     
                            }
     
                            if("button".equals(attrs.getValue("type"))){
                                JButton bu = new JButton();
                                groupe.add(bu);
                                System.out.println("création composant"+comp);
                                comp++;
     
                            }
     
     
                        }
     
     
                    }
     
     
     
     
     
     
                }   
            }
        }
     
     
     
     
     
     
        public void endElement(  String  namespaceURI, String  simpleName, String  qualifiedName)  throws SAXException {  
     
            String nomElement = simpleName;  
            if (nomElement.equals("")) {  
                nomElement  = qualifiedName; 
                String b;
     
     
                JPanel b1 = new JPanel();
            }   
     
            if (num==6){
     
                System.out.println("ajout composant au groupe"+id);
                id++;
            }
     
            if ("object".equals(nomElement)){
                System.out.println( "endElement : " + nomElement + num);
     
                num--;
            }  
        }
     
     
    } // Fin de la classe   
     
     
    classe Test...
    import java.io.File;
    import java.io.IOException;
     
    import javax.swing.JFrame;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
     
    import org.xml.sax.helpers.DefaultHandler;
     
    public class Test {
     
          // Méthode principale 
          public static void main(String args[]) throws IOException {  
            // Si l'utilisateur a oublié de passer   
            // le nom du fichier XML en paramètre => erreur   
           if (args.length != 1) {  
           System.err.println( "Erreur, il manque un argument");  
           System.exit(1);  
          }   
            // on lance l'analyseur avec le fichier XML en parametre   
          DefaultHandler  handler = new AnalyseSAX();  
          SAXParserFactory  factory;  
          factory  = SAXParserFactory.newInstance();  
           try {  
           SAXParser  saxParser = factory.newSAXParser();  
           saxParser.parse( new File(args[0]), handler);  
          }  catch (Throwable t) {  
              // Si on a une erreur pendant l'analyse   
           t.printStackTrace();  
           System.exit(1);  
          }   
     
         JFrame fenetreTest = AnalyseSAX.constructionFenetre();
         fenetreTest.show();
     
          //System.exit(0);  
         }  // fin du main  
     
    }
    en fait comme il y a plusieurs groupe j'ai essayé d'attribuer des id aux groupes mais j'arrive pas a crée un panel différent pr chaque groupe je voudrais par exemple faire...

    JPanel nomPanel+idPanel = new JPanel();

    pr info edit = JTextField / group = JPanel / cbx = JComboBox

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Tu ne peux pas générer dynamiquement de nom de variable en java, par contre tu pourrais parfaitement utiliser une des diffrentes instantiation de Map (surement une HashMap) qui associera un couple nom/panel

    ex :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    Map<String,JPanel> m = new HashMap<String,JPanel>()
    JPanel p = new JPanel();
    String title = "nomDuPanel"+"idDuPanel";
    m.put(title,p);
     
    //puis pour récupérer le panel voulu:
     
    JPanel p1 = p.get(title);
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  3. #3
    Nouveau membre du Club
    Inscrit en
    Février 2006
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 52
    Points : 34
    Points
    34
    Par défaut
    ca marche pas ton truc j'ai essayé avec ton code et j'ai ajouté ca à une JFrame mais ca me lève une exception : java.lang.ClassCastException

    voila mon code....
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     Map<String,JPanel> m = new HashMap<String,JPanel>();
            JPanel p = new JPanel();
            String title = "nomDuPanel"+"idDuPanel";
            m.put(title,p);
    // puis pour récupérer le panel voulu:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
             JPanel p1 = ((Map<String,JPanel>) p).get(title);
            p1.setBackground(Color.red);
            fenetreTest.add(p1);

  4. #4
    Membre expérimenté Avatar de yann2
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2004
    Messages
    897
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2004
    Messages : 897
    Points : 1 635
    Points
    1 635
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     Map<String,JPanel> m = new HashMap<String,JPanel>();
             JPanel p = new JPanel();
             String title = "nomDuPanel"+"idDuPanel";
             m.put(title,p);
     
     //        puis pour récupérer le panel voulu:
     
             JPanel p1 = ((Map<String,JPanel>) p).get(title);
             p1.setBackground(Color.red);
             fenetreTest.add(p1);
    Euh p ce n'est pas une Map. C'est m la map.

    Il ya un gros pb...

    EDIT : Ah je viens de trouver. C'est sinok qui a dû faire une erreur d'inatention :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     Map<String,JPanel> m = new HashMap<String,JPanel>()
    JPanel p = new JPanel();
    String title = "nomDuPanel"+"idDuPanel";
    m.put(title,p);
    
    //puis pour récupérer le panel voulu:
    
    JPanel p1 = m.get(title);
    Bon courage

  5. #5
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    exact je viens de m'en rendre compte
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  6. #6
    Nouveau membre du Club
    Inscrit en
    Février 2006
    Messages
    52
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 52
    Points : 34
    Points
    34
    Par défaut
    ok c'est bon la ca marche maintenant je vais utiliser ca pr essayer de résoudre mon problème..

Discussions similaires

  1. transformation xml --> xsl --> xml
    Par Invité dans le forum Eclipse PHP
    Réponses: 0
    Dernier message: 03/02/2009, 16h15
  2. [XSLT] processeur pour une transformation XML vers XML
    Par fanette dans le forum XSL/XSLT/XPATH
    Réponses: 1
    Dernier message: 15/06/2006, 12h46
  3. [XSLT] transformation xml vers xml
    Par nemya dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 22/02/2006, 11h02
  4. Transformation XML vers XML
    Par runabout dans le forum XML/XSL et SOAP
    Réponses: 1
    Dernier message: 08/02/2006, 16h48

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