Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:33:29+00:00 2026-06-10T07:33:29+00:00

I’ve got an array that creates buttons from A-Z, but I want to use

  • 0

I’ve got an array that creates buttons from A-Z, but I want to use it in a
Method where it returns the button pressed.

this is my original code for the buttons:

String  b[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for(i = 0; i < buttons.length; i++)
{
    buttons[i] = new JButton(b[i]);
    buttons[i].setSize(80, 80);
    buttons[i].setActionCommand(b[i]);
    buttons[i].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            String choice = e.getActionCommand();
            JOptionPane.showMessageDialog(null, "You have clicked: "+choice);
        }
    });
    panel.add(buttons[i]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:33:31+00:00Added an answer on June 10, 2026 at 7:33 am

    I wasn’t sure exactly what you question was, so I have a few answers:

    1. If you want to pull the button creation into a method – see the getButton method in the example

    2. If you want to access the actual button when it’s clicked, you can do that by using the ActionEvent.getSource() method (not shown) or by marking the button as final during declaration (shown in example). From there you can do anything you want with the button.

    3. If you question is “How can I create a method which takes in a array of letters and returns to me the last clicked button”, you should modify you question to explicitly say that. I didn’t answer that here because unless you have a very special situation, it’s probably not a good approach to the problem you’re working on. You could explain why you need to do that, and we can suggest a better alternative.

    Example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TempProject extends Box{
        /** Label to update with currently pressed keys */
        JLabel output = new JLabel();
    
        public TempProject(){
            super(BoxLayout.Y_AXIS);
            for(char i = 'A'; i <= 'Z'; i++){
                String buttonText = new Character(i).toString();
                JButton button = getButton(buttonText);
                add(button);
            }
        }
    
        public JButton getButton(final String text){
            final JButton button = new JButton(text);
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "You have clicked: "+text);
                    //If you want to do something with the button:
                    button.setText("Clicked"); // (can access button because it's marked as final)
                }
            });
            return button;
        }
    
        public static void main(String args[])
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.setContentPane(new TempProject());    
                    frame.pack();
                    frame.setVisible(true);
                    new TempProject();
                }
            });
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.