youngknight
youngknight

Reputation: 33

Assign JButton for-loop

 String[] nomMois =
{"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Aout",
"Septembre","Octobre","Novembre","Decembre"};

JPanel tabJPanelMois[] = new JPanel[nomMois.length];

for(int indice=0; indice<tabJPanelMois.length; indice++){
        tabJPanelMois[indice]= new JPanel();
        tabJPanelMois[indice].setLayout(new GridLayout(0,7,8,18));

        for(int j=1; j<=Date.dernierJourDuMois(indice+1,2017);j++){

            tabJPanelMois[indice].add(new JButton(Integer.toString(j)));
        }

I want to put an ActionListener on each Button but with that code i don't assign a name for each JButton so I can't,how must I do?

Upvotes: 0

Views: 36

Answers (1)

Define a temporal Button, and use it in the loop, creating a new object will allow you to recycle the variable x as much as you need:

JButton x;
for(int j=1; j<=Date.dernierJourDuMois(indice+1,2017);j++){
        x = new JButton(Integer.toString(j));
        x.setMyNewListener(abcListner);
        tabJPanelMois[indice].add(x);
    }

Upvotes: 1

Related Questions