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 8789831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:30:49+00:00 2026-06-13T22:30:49+00:00

JPanel p = new JPanel(); p.setLayout(new GridLayout(4, 4, 5, 5)); String[] buttons = {

  • 0
JPanel p = new JPanel();
    p.setLayout(new GridLayout(4, 4, 5, 5));
    String[] buttons = {
        "1", "2", "3", "/", "4",
        "5", "6", "*", "7", "8", "9", "-", "0", ".", "=", "+"
    };
    for (int i = 0; i < buttons.length; i++) {
            p.add(new JButton(buttons[i]));
   add(p);

This code produces a nice calculator layout is there a way to addActionListener to each button while at the same time keeping this layout
what i mean is not doing it for each and every button like so.

JButton button1 = new JButton("1");
     button1.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent event) {
// interesting code for button1 goes here
      }
   });
  • 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-13T22:30:50+00:00Added an answer on June 13, 2026 at 10:30 pm

    i did it using this code…

       String[] buttons = {
            "1", "2", "3", "/", "4",
            "5", "6", "*", "7", "8", "9", "-", "0", ".", "=", "+"
        };
    
        Action[] allActions = new Action[buttons.length];
    
        for ( i = 0; i < buttons.length; i++) {
    
            allActions[i] = new ButtonAction(buttons[i],i);
    
            JButton button = new JButton(allActions[i]);
    
               panel.add(button);
    
    
            }
    

    with the class ButtonAction as follows

      public class ButtonAction extends AbstractAction 
      {
    int i;
       public ButtonAction(String text, int i) {
        super(text);
        this.i = i;
        }
       public void actionPerformed(ActionEvent e) {
        /* you can put any action here. either make the action depend on array value or 
          the string text */
       System.out.println(i);
    
       }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Text extends JPanel { private String text; public Text() { this.setPreferredSize(new Dimension(20,20));
Recently I asked here how to add a new JPanel to JFrame . The
How do we show the gridline in GridLayout? in Java? JPanel panel = new
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons.
So i have this code that creates a Calculator in Java. But it is
I’m using a JPanel to add JComponents on and I tried to set the
im trying to create a custom JPanel which i add into a card layout
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
I am having a lot of trouble with this code. The code compiled and
Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which

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.