Bonjour, je reviens vers vous pour un problème, mais aussi surtout une incompréhension alors que je suis le cours sur le Java.

Pour mon projet, je dois créer une interface graphique avec plusieurs checkbox, textfield, et autres. Cette interface sera utile pour paramétrer une machine. Le technicien qui utilisera cette interface rentrera les détails souhaités, puis ces informations rentreront dans un fichier XML. Ce fichier XML ira dans une machine et la machine sera donc configuré.

J'ai réussi à créer mon document XML (éléments parent, élément enfant, attributs, nom de document). Les attributs de ce document changeront en fonction des détails choisis dans l'interface.

De plus, j'ai déjà réussi à faire que lorsque j'écris dans la console, ça me remplace un attribut (au bon endroit, le bon attribut, etc) donc ceci est OK. J'ai donc eu pour but de créer une interface, cependant les problèmes arrivent. Tout d'abord, je suis aller sur l'IDE NetBeans car il est plus simple visuellement et plus léger.

Ensuite, je créer tout d'abord des TextField (pour que le technicien rentre son information) (ces TextField seront remplacés par des checkbox).

Cependant, c'est là que le problème arrive. Je n'arrive pas à récupérer la valeur de JTextField, pour ensuite faire que mon attribut ai la valeur saisie/récupéré.

Voici mon programme qui permet de générer mon document XML avec tous ces éléments et attributs :

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
package tutoriel;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
import java.util.Random;
import java.util.Scanner;
 
public class main_window extends javax.swing.JFrame {
 
        static int nombreAleatoire = 900000 + (int)(Math.random() * ((999999 - 900000) + 1));
    static String a ="";
 
    public main_window() {
        initComponents();
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {
 
        jLabel1 = new javax.swing.JLabel();
        txt1 = new javax.swing.JTextField();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Configurateur");
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
 
        jLabel1.setText("jLabel1");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(552, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(424, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                       
 
    /**
     * @param args the command line arguments
     * @throws javax.xml.transform.TransformerException
     * @throws javax.xml.parsers.ParserConfigurationException
     */
    public static void main(String[] args) throws TransformerException, ParserConfigurationException {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 
         //Creation d'un numero de reference aleatoire
         Random rand = new Random();
         int nb = rand.nextInt(999999 - 900000 + 1) + 900000;
         a = String.valueOf(nb);
 
         DocumentBuilder builder = factory.newDocumentBuilder();
 
 
 
//Demande d'ecriture des donnees dans la console      
         Scanner brand = new Scanner(System.in);
         System.out.println("Nom:");
         String nom= brand.nextLine();
 
         Scanner fcts= new Scanner(System.in);
         System.out.println("Fonction:");
         String fonction= fcts.nextLine();
 
         Scanner fcts1 = new Scanner(System.in);
         System.out.println("Fonction 1:");
         String fonction1 = fcts1.nextLine();
 
         Document xml = builder.newDocument(); //Création du fichier
         Element root = xml.createElement("conf"); //Création de notre élément racine   
 
         //Création noeud XML
         Element machine = xml.createElement("machine");
         Element truc1= xml.createElement("truc1");
         Element truc2= xml.createElement("truc2");
         Element truc3= xml.createElement("truc3");
 
 
         //Commentaires
         Comment comment = xml.createComment("Configuration machine");
         root.appendChild(comment);
         Comment comment2 = xml.createComment("Définition de la machine ");
 
 
      //Création et valeurs des attributs
      machine.setAttribute("nom",nom);
      machine.setAttribute("fonction",fonction);
      machine.setAttribute("fonction 1",fonction1);
 
      truc1.setAttribute("aide","3");
      truc1.setAttribute("coefA", "3");
 
      truc2.setAttribute("coefB","3");
 
      truc3.setAttribute("count", "3"); 
 
 
 
         //Indication quel noeud parent avec quel noeud(s) enfant(s)
         machine.appendChild(truc1);
         machine.appendChild(truc2);
         machine.appendChild(truc3);
         root.appendChild(machine);
 
         Transformer t = TransformerFactory.newInstance().newTransformer();
         t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "madTdbidon.dtd");
         t.setOutputProperty(OutputKeys.INDENT, "yes");
         t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
         t.setOutputProperty(OutputKeys.VERSION, "1.0");
         // appliquer la transformation
         String resultFile = a+".conf";
         StreamResult XML = new StreamResult(resultFile);
 
         t.transform(new DOMSource(root), XML); 
 
        }
            public void run() {
                new main_window().setVisible(true);
            }
 
    // Variables declaration - do not modify                    
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txt1;
    // End of variables declaration                  
}
Merci beaucoup à ceux qui m'aideront et comprendrons le problème