Bonjour,

J'éprouve des difficultés avec un PropertyChangeListener.

Celui-ci ne semble pas écouter comme il le faudrait la classe à laquelle je l'ai ajouté.

Dans mon cas, je gère une application qui gèrent plusieurs boites de dialogues dont plusieurs peuvent être ouvertes simultanément.

Concrètement, j'ai une classe qui étends JDialog, qui s'ouvre lorsque l'utilisateur désire ajouter un nouvel objet.
Dans cette JDialog, j'ai plusieurs comboBox d'objets correspondantes chacune à une caractéristique de mon objet principal.
L'utilisateur peut alors ajouter s'il le souhaite une nouvelle entrée à l'une de ces caractéristiques.
C'est à ce moment là qu'une autre JDialog s'ouvre permettant la création de cette nouvelle entrée.

Mon but étant de mettre à jour la comboBox correspondante lorsqu'une nouvelle entrée a été ajouté à ma liste d'objets.

Pour cela j'opère ainsi :


L'une des caractéristiques de mon objet :


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
public class BrandDialog extends JDialog implements MouseListener, ActionListener{
 
    /** serial uid */
    private static final long serialVersionUID = 8240735682218996098L;
 
    /** log */
    private static final Logger log = Logger.getLogger(BrandDialog.class);
 
    /** container */
    private final Container container;
 
    /** buttons */
    private JButton validButton, cancelButton;
 
    /** textfield */
    private JTextField brandNameTF;
 
    /** label */
    private JLabel labelErrorMessage;
 
    /** timer */
    private Timer timerBlink;
 
    /** brands list */
    private List<Brand> listBrands;
 
    /** property change support */
    private final PropertyChangeSupport changeSupport;
 
    /**
     * Constructor
     * 
     * @param parentFrame 
     * @param listBrands 
     */
    public BrandDialog(JFrame parentFrame, List<Brand> listBrands){
 
        // call super
        super(parentFrame, "Nouvelle marque", true);
 
        this.listBrands = listBrands;
 
        // size
        this.setSize(278, 110);
 
        // not resizable
        this.setResizable(false);
 
        // location
        this.setLocationRelativeTo(this.getParent());
 
        // container
        container = this.getContentPane();
 
        // property change support
        changeSupport = new PropertyChangeSupport(this);
 
        buildGUI();
    }
 
     /**
     * add brand to list
     * 
     * @param brand
     */
    public void addBrand(Brand brand){
 
        listBrands.add(brand);
 
        Collections.sort(listBrands, new Comparator<Brand>() {
            public int compare(Brand b1, Brand b2) {
                return ((b1.getName()).compareTo(b2.getName()));
            }
        });
 
        changeSupport.firePropertyChange(DataEvent.ADDBRAND.toString(), null, brand);
    }

Mon objet principal :

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
public class ProductDialog extends JDialog implements ActionListener,
        ItemListener, MouseListener, PropertyChangeListener{
 
    /** serial uid */
    private static final long serialVersionUID = -4530501436916190299L;
 
    /** log */
    private static final Logger log = Logger.getLogger(ProductDialog.class);
 
    /** container */
    private final Container container;
 
    /** buttons */
    private JButton validButton, cancelButton, addBrandButton, addProductCategoryButton, addProductSubCategoryButton, addShopButton;
 
    /** textfields */
    private JTextField productNameTF, productPriceTF;
 
    /** combobox */
    private JComboBox productBrandCB, productCategoryCB, productSubCategoryCB, productShopCB;
 
    /** product categories list */
    private List<ProductCategory> listCategories;
 
    /** products list */
    private List<Product> listProducts;
 
    /** brands list */
    private List<Brand> listBrands;
 
    /** shops list */
    private List<Shop> listShops;
 
    /** brand dialog */
    private final BrandDialog brandDialog;
 
    /**
     * Constructor
     * 
     * @param parentFrame parent frame
     * @param listProducts products list
     * @param listBrands brands list
     * @param listShops shops list
     * @param listCategories categories list
     */
    public ProductDialog(JFrame parentFrame, List<Product> listProducts,
            List<Brand> listBrands, List<Shop> listShops,
            List<ProductCategory> listCategories){
 
        // call super
        super(parentFrame, "Nouveau produit", true);
 
        this.listProducts = listProducts; 
        this.listBrands = listBrands;
        this.listShops = listShops;
        this.listCategories = listCategories;
 
        // size
        this.setSize(278, 250);
 
        // not resizable
        this.setResizable(false);
 
        //location
        this.setLocationRelativeTo(this.getParent());
 
        // container
        container = this.getContentPane();
 
        // brand dialog 
        brandDialog = new BrandDialog((JFrame) this.getParent(), listBrands);
 
        // add listener
        brandDialog.addPropertyChangeListener(this);
 
        buildGUI();
    }
 
    /* (non-Javadoc)
     * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
     */
    /**
     * 
     */
    @Override
    public void propertyChange(PropertyChangeEvent arg0) {
 
        String eventName = arg0.getPropertyName();
 
        log.debug(eventName);
 
        try {
            DataEvent event = DataEvent.valueOf(eventName);
 
            log.debug(event);
        }
        catch (IllegalArgumentException e){
 
            log.debug(e.getMessage());
        } 
    }
Avec ceci je récupère bien quelque chose lorsque la boite de dialogue s'ouvre mais c'est tout.
Lorsque j'appelle firePropertyChange() lors de l'ajout d'un objet dans ma liste, je ne récupère rien dans mon listener.


J'ai aussi tenté d'ajouter directement un propertyChangeSupport à ma liste mais cela ne marche pas non plus :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
PropertyChangeSupport changeSupport = new PropertyChangeSupport(listBrands);
 
changeSupport.addPropertyChangeListener(this);