简体   繁体   中英

Is there a better way to create an actionListener to JButtons in a for-loop?

I want to create a number of JButton s which symbolize a specific time-intervall. All JButton s share basically the same actionListener : They mark their specific intervall as "free" ("Frei") or "not free" ("Belegt"). To do that, they not only just have to change text, but they have to call a method of another class, which uses their intervall as @parameter . I create the JButton s with naming JLabel s like this:

for (int i = 1; i <= numberOfIntervalls; i++) {
            String toolTextString;
            if(controller.getCalendar(calendarName).isFree(i, controller.getDayString(year, month, day))) {
                buttonText = "Frei";
                toolTextString = "Belegt";
            }else {
                buttonText = "Belegt";
                toolTextString = "Frei";
            }
            
            if(i%5 == 0) {
                gridx = 1;
                buttonGridy += 2;
                textGridy += 2;
            }else if(i < 5){
                gridx = (i%5);
            }else {
                gridx = (i%5) + 1;
            }
            intervallButtons[i-1] = new JButton(buttonText);
            intervallButtons[i-1].addActionListener((ActionListener) this);
            intervallLabels[i-1] = new JLabel(intervallLabelBuilder(i));
            intervallButtons[i-1].setToolTipText(String.format("als %s markieren", toolTextString));
            
            GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
            gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
            gbc_btnNewButton.gridx = gridx;
            gbc_btnNewButton.gridy = buttonGridy;       
            contentPane.add(intervallButtons[i-1], gbc_btnNewButton);
            
            GridBagConstraints gbc_labels = new GridBagConstraints();
            //gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
            gbc_labels.gridx = gridx;
            gbc_labels.gridy = textGridy;
            contentPane.add(intervallLabels[i-1], gbc_labels); 
            
        }
    }

my actionListener -method is this because I know that numberOfIntervalls = 12 , for quik ajustments, the numberOfIntervalls should be a variable, but it s static and final`:

public void actionPerformed (ActionEvent e){
         System.out.println("-----------");
            if(e.getSource() == intervallButtons[0]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                        controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(1, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(1, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[0].setToolTipText("Als Frei markieren");
                    
                }else {
                    controller.getCalendar(calendarName).setFree(1, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[0].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[0].setText(buttonText);
                intervallButtons[0].setEnabled(false);
                intervallButtons[0].setEnabled(true);

            }else if(e.getSource() == intervallButtons[1]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                        controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                
                if(controller.getCalendar(calendarName).isFree(2, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(2, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[1].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(2, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[1].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[1].setText(buttonText);
                intervallButtons[1].setEnabled(false);
                intervallButtons[1].setEnabled(true);
            }
            else if (e.getSource() == intervallButtons[2]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(3, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(3, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[2].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(3, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[2].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[2].setText(buttonText);
                intervallButtons[2].setEnabled(false);
                intervallButtons[2].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[3]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(4, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(4, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[3].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(4, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[3].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[3].setText(buttonText);
                intervallButtons[3].setEnabled(false);
                intervallButtons[3].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[4]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(5, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(5, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[4].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(5, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[4].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[4].setText(buttonText);
                intervallButtons[4].setEnabled(false);
                intervallButtons[4].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[5]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(6, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(6, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[5].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(6, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[5].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[5].setText(buttonText);
                intervallButtons[5].setEnabled(false);
                intervallButtons[5].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[6]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(7, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(7, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[6].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(7, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[6].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[6].setText(buttonText);
                intervallButtons[5].setEnabled(false);
                intervallButtons[5].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[7]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(8, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(8, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[7].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(8, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[7].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[7].setText(buttonText);
                intervallButtons[7].setEnabled(false);
                intervallButtons[7].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[8]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(9, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(9, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[8].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(9, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[8].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[8].setText(buttonText);
                intervallButtons[8].setEnabled(false);
                intervallButtons[8].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[9]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                        controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(10, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(10, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[9].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(10, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[9].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[9].setText(buttonText);
                intervallButtons[9].setEnabled(false);
                intervallButtons[9].setEnabled(true);
                
            }else  if (e.getSource() == intervallButtons[10]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(11, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(11, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[10].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(11, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[10].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[10].setText(buttonText);
                intervallButtons[10].setEnabled(false);
                intervallButtons[10].setEnabled(true);
                
            }else if (e.getSource() == intervallButtons[11]){
                if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                    controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(12, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(12, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[11].setToolTipText("Als Frei markieren");
                }else {
                    controller.getCalendar(calendarName).setFree(12, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[11].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[11].setText(buttonText);
                intervallButtons[11].setEnabled(false);
                intervallButtons[11].setEnabled(true);
                
            }
        }

it's incredibly long and barely ajustable with just one variable, so all I want is following code standing in the creating for -loop:

 if(!controller.getCalendar(calendarName).isLoaded(controller.getDayString(year, month, day)))
                        controller.getCalendar(calendarName).summonDay(controller.getDayString(year, month, day));
                if(controller.getCalendar(calendarName).isFree(1, controller.getDayString(year, month, day))) {
                    controller.getCalendar(calendarName).setFree(i, false, controller.getDayString(year, month, day));
                    buttonText = "Belegt";
                    intervallButtons[i-1].setToolTipText("Als Frei markieren");
                    
                }else {
                    controller.getCalendar(calendarName).setFree(i, true, controller.getDayString(year, month, day));
                    buttonText = "Frei";
                    intervallButtons[i-1].setToolTipText("Als Belegt markieren");
                }
                intervallButtons[i-1].setText(buttonText);
                intervallButtons[i-1].setEnabled(false);
                intervallButtons[i-1].setEnabled(true);

but it doesn't work, because the actionListener can only handle non-local Variables... Do you have any ideas?

and int i is different for every JButton, because it needs to be the int i from that for-loop.

One way is to set the "action command" for the button.

When you create the button you use:

JButton button = new JButton(...);
button.setActionCommand("" + i);

Then in the ActionListener you use:

int index = Integer.parseInt( event.getActionCommand() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM