Java ActionListener for an unnamed JButton?

I was wondering if there is a way to implement an action listener for an unnamed Jbutton. For example, I have the following for loop that creates a button for each letter of the alphabet.

for (int i = 65; i < 91; i++){
     alphabetPanel.add(new JButton("<html><center>" + (char)i));
}

      

Is there a way I can add an action listener for each of these buttons without getting rid of the for loop and hardcoding each JButton and then creating an action listener for each?

Thanks in advance,

Tomek

0


source to share


4 answers


Your question is a little vague. it would be trivial to change the loop to add a listener inside the loop:

ActionListener listener = something;

for (int i = 65; i < 91; i++){
     JButton button = new JButton("<html><center>" + (char)i);
     button.addActionListener( listener );
     alphabetPanel.add(button);
}

      



If you cannot change the loop, you can iterate over all the children of the panel and add listeners to any of the children, which are jbuttons.

also why are you using html to center the text? isn't this excess? isn't the jbutton already the center of the text?
you can use setHorizontalAlignement (SwingConstants.CENTER) to do this too.

+4


source


What is the problem?

for (int i = 65; i < 91; i++){
     JButton button = new JButton("<html><center>" + (char)i));
     button.addActionListener( new ButtonListener());
     alphabetPanel.add(button);
}

      

...



class ButtonListener implements ActionListener {
     ButtonListener() {
     }
     public void actionPerformed(ActionEvent e) {
         //TODO:
     }
}

      

Also, if the button text does not identify the button, you can set the button name with an alphabet letter.

button.setName((char)i)); // or button.setName(i);

      

+2


source


By the name, you seem to mean storing the button instance in a local variable of your immediate method. Trying to avoid this will likely make your code more difficult to read. But to answer your question:

The most obvious way is to use the old but newly popular double anchor idiom.

alphabetPanel.add(new JButton("<html><center>" + (char)i) {{
    addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ...
        }
    });
}});

      

Please note that in this case, since I am not final, it will not be used from the anonymous inner class. Either assign it to another (final) variable, or reformulate the loop.

Another route is to go through Action. (I usually suggest avoiding actions as they are poor Hashtable people. ButtonModel is "nice" though.)

alphabetPanel.add(new JButton(new AbstractAction("<html><center>" + (char)i) {
    public void actionPerformed(ActionEvent event) {
        ...
    }
}));

      

Then of course there is an application-specific library way:

Form alphabetForm = new Form(alphabetPanel);
for (char c='A'; c <= 'Z'; ++c) {
    alphabetForm.button("<html><center>" + c, new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            ...
        }
    });
}

      

+1


source


You can add ActionListener to anonymous components as shown below:

new JButton().addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO your action             
            }           
        });

      

However, in your case, when you are trying to add an anonymous JButton to your panel, this approach will not work because the return type of the addActionListener method (which is invalid) will be used instead of the JButton constructor, as shown below:

for (int i = 65; i < 91; i++){
             alphabetPanel.add(new JButton("<html><center>" + (char)i).addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        // TODO your action             
                    }           
                }));
        }

      

The above code complains about an invalid argument to the panel.add () method.

So, in your case, you will need to create a named JButton instance.

Hope this clarifies.

Best wishes Suresh

0


source







All Articles