Bonjour,
Je souhaite créer mon propre composant : un JLabel + un JComboBox (ou JTextField).
Pour cela, je déclare une classe Composant dérivant de JComponent. Voici mon code :
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
 
public class Composant extends JComponent {
 
    // Un label qui affiche l'information à entrer
    private JLabel label = null;
 
    // Un composant qui permet la saisie de l'information
    private JComponent composant = null;
 
    // Un panel qui gère le label et le composant
    private JPanel panelPrincipal = null;
 
     public Composant(String texte, String typeComposant) {
         // Si le texte et le composant ne sont pas nulls
         if ((texte != null) && (composant != null)) {
 
             // On instancie le label avec le texte
             label = new JLabel(texte + " : ");
 
             // Si on demande une liste déroulante
             if (typeComposant.equals("JComboBox")) {
                 // On instancie uns liste déroulante
                 composant = new JComboBox();
 
             // Si on demande un JTextField
             } else if (typeComposant.equals("JTextField")) {
                 // On instancie un champ texte
                 composant = new JTextField();
             }
 
             // On instancie le panelPrincipal
             panelPrincipal = new JPanel(new FlowLayout());
 
             // On ajoute le label et le composant au panelPrincipal
             panelPrincipal.add(label);
             panelPrincipal.add(composant);
 
             // On ajoute le panelPrincipal au composant
             this.add(panelPrincipal);
         }
     }
}
Ensuite, j'ai créer une fenêtre avec un panel géré par un BoxLayout, voici un bout de mon autre classe :
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
 
// On instancie les listes déroulantes
private Composant serveur = new Composant("Serveur", "JComboBox");
private Composant client = new Composant("Type de client", "JComboBox");
private Composant typeDocument = new Composant("Type de document", "JComboBox");
 
// On instancie le champ texte
private Composant id = new Composant("ID du client", "JTextField");
 
// On instancie le panel des listes déroulantes et du champ texte
panelInformations = new JPanel();
 
// On définit le layout pour le panel
panelInformations.setLayout(new BoxLayout(panelInformations, BoxLayout.Y_AXIS));
 
// On ajoute les listes déroulantes et le champ texte au panelInformations
serveur.setAlignmentX(Component.LEFT_ALIGNMENT);
panelInformations.add(serveur);
typeDocument.setAlignmentX(Component.LEFT_ALIGNMENT);
panelInformations.add(typeDocument);
client.setAlignmentX(Component.LEFT_ALIGNMENT);
panelInformations.add(client);
id.setAlignmentX(Component.LEFT_ALIGNMENT);
panelInformations.add(id);
 
...
 
// On instancie le panel principal
panelPrincipal = new JPanel(new BorderLayout());
 
// On ajoute le panelInformations au panelPrincipal
panelPrincipal.add("Center", panelInformations);
 
// On affiche les boutons dans la fenêtre (this)
this.getContentPane().add(panelPrincipal);
Mon problème est qu'aucun "Composant" ne s'affiche dans la fenêtre et je ne vois vraiment pas où est mon erreur. Alors si vous avez des idées, merci !