Salut !!


J'ai un souci avec un CardLayout qui me pourrit la vie; je vous explique :

C'est pour le design d'une grille de sudoku et les listener qui vont avec.

Chaque case a le Layout d'un CardLayout. Cette case est dans mon cas une pile qui va contenir 2 composants (dans un cas, il s'agit d'un bouton classique -> choix final; dans l'autre, c'est un ensemble de 9 nouveaux petits boutons -> les possibilités).

Mon problème, c'est que j'aimerais déclarer la fameuse case dans ma classe plutot que dans mon constructeur (histoire de pouvoir l'utiliser dans une méthode plus loin). Or, si je la déclare à cet endroit, j'ai l'impression qu'il n'existe qu'une instance de cette case alors que j'en voudrais 81. A noter que si je la déclare dans le constructeur, y'a aucun souci ...


Voici le code (la case qui me cause problème est la variable jpcasefinale) :

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
 
public class PanelVue1 extends JPanel implements Observer{
 
    private Placement sudomodele;
 
    JPanel pane1;
    JPanel sousgrille;
    JPanel boutonfinal;    
    JPanel grilleposs;    
    JButton [][] tabjb = new JButton[9][9];
    JButton [][][] tabjbposs = new JButton[9][9][9];    
    JPanel jpcasefinale;
    CardLayout pilecarte = new CardLayout();;                                    
 
    public PanelVue1(Placement smodele){
 
        int i,j,k;
 
        this.sudomodele = smodele;        
        smodele.addObserver(this);
        this.setLayout(new BorderLayout());                
 
        pane1 = new JPanel();
        pane1.setLayout(new GridLayout(3,3,1,1));
        pane1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(0,Color.LIGHT_GRAY,Color.GRAY),"Grille"));        
        pane1.setPreferredSize(new Dimension(675,675));
        for (i=0;i<9;i++) {
            sousgrille = new JPanel();
            sousgrille.setLayout(new GridLayout(3, 3));
            sousgrille.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
            for (j=0;j<9;j++){                        
 
                jpcasefinale = new JPanel();                
                jpcasefinale.setLayout(pilecarte);    
 
                MouseListener buttonListener = new MouseAdapter(){
                    public void mouseClicked(MouseEvent ev) {                       
                        JButton obj = (JButton)ev.getSource();
                        String s = obj.getActionCommand();
                        int []coord=new int[2];
                        int int1,int2,int3;
                        if (obj.getActionCommand().startsWith("bfin")) {
                            int1 = Integer.parseInt(s.substring(4,5));
                            int2 = Integer.parseInt(s.substring(5,6));
                            convgrlcoord(int1,int2,coord);
                            if (SwingUtilities.isRightMouseButton(ev) || ev.isPopupTrigger()){                                                             
                                if (sudomodele.getChiffrechoisi() != 0 ){                                    
                                    sudomodele.clicp1fbd(coord);                                                       
                                }                                                                
                                if (sudomodele.getGrillep()[coord[0]][coord[1]].isEtatpile()==true) swap();//pilecarte.next(jpcasefinale);
                            }
                            else if (SwingUtilities.isLeftMouseButton(ev) || ev.isPopupTrigger()){                                                              
                                if (sudomodele.getChiffrechoisi() != 0 ){                                    
                                    sudomodele.clicp1fbg(coord);
                                }
                            }
                        }                        
                        else if (obj.getActionCommand().startsWith("bpos")){
                            int1 = Integer.parseInt(s.substring(4,5));
                            int2 = Integer.parseInt(s.substring(5,6));
                            int3 = Integer.parseInt(s.substring(6,7));       
                            convgrlcoord(int1,int2,coord);
                            if (SwingUtilities.isRightMouseButton(ev) || ev.isPopupTrigger()){                                                               
                                sudomodele.clicp1pbd(coord,int3);
                                if (sudomodele.getGrillep()[coord[0]][coord[1]].isEtatpile()==false) swap();//pilecarte.next(jpcasefinale);                            
                            }
                            else if (SwingUtilities.isLeftMouseButton(ev) || ev.isPopupTrigger()){                                                                    
                                sudomodele.clicp1pbg(coord,int3);
                                if (sudomodele.getGrillep()[coord[0]][coord[1]].isEtatpile()==false) swap();//pilecarte.next(jpcasefinale);                        
                            }
                        }
                    }
                };             
                boutonfinal = new JPanel();
                boutonfinal.setLayout(new BorderLayout());
                tabjb[i][j] = new JButton();                
                tabjb[i][j].setFont(new Font("TimesRoman",1,23));                
                tabjb[i][j].setActionCommand("bfin"+Integer.toString(i)+Integer.toString(j));
                tabjb[i][j].addMouseListener(buttonListener);                
                boutonfinal.add(tabjb[i][j],BorderLayout.CENTER);
                grilleposs = new JPanel();
                grilleposs.setLayout(new GridLayout(3,3));
                grilleposs.setBorder(BorderFactory.createLineBorder(Color.GRAY));
                for (k=0;k<9;k++){
                    tabjbposs[i][j][k] = new JButton();
                    tabjbposs[i][j][k].setBorder(null);
                    tabjbposs[i][j][k].setMargin(new Insets(0,0,0,0));
                    tabjbposs[i][j][k].setFont(new Font("TimesRoman",0,15));
                    tabjbposs[i][j][k].setActionCommand("bpos"+Integer.toString(i)+Integer.toString(j)+Integer.toString(k));
                    tabjbposs[i][j][k].addMouseListener(buttonListener);
                    grilleposs.add(tabjbposs[i][j][k]);
                }
                jpcasefinale.add(boutonfinal, "choixfinal");
                jpcasefinale.add(grilleposs, "grillepossibilites");
                sousgrille.add(jpcasefinale);
            }
            pane1.add(sousgrille);
        }
        this.add(pane1, BorderLayout.CENTER);
    }
Pour mieux comprendre, voilà ce que j'obtiens (quelle que soit la case sur laquelle je clique-droit, c'est toujours sur la dernière que s'opère l'action).




Un tout grand merci d'avance si vous pouvez m'éclairer ...