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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:56:18+00:00 2026-06-16T00:56:18+00:00

Possible Duplicate: java – How would I dynamically add swing component to gui on

  • 0

Possible Duplicate:
java – How would I dynamically add swing component to gui on click?

I want to add array of buttons dynamically.
I tried like this:

this.pack();
    Panel panel = new Panel();
    panel.setLayout(new FlowLayout());
    this.add(panel);
    panel.setVisible(true);
    for (int i = 0; i < Setting.length; i++) {
        for (int j = 0; j < Setting.width; j++) {
            JButton b = new JButton(i+"");
            b.setSize(30, 30);
            b.setLocation(i * 30, j * 30);
            panel.add(b);
            b.setVisible(true);
        }
    }

but didn’t get anything , what mistake did I make?

Edit

I have jFrame class “choices” on it i have a button , when I press the button, this is supposed to happen:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       structure.Setting s = new Setting(8, 8, 3, 1, 1);
       Game g = new Game();
       g.setSetting(s);
       this.dispose();
       g.show();
    }

then i go to the Game class (also jFrame class) to the function setSetting and it is like this:

void setSetting(Setting s) {
        this.setting = s;
        structure.Game game = new structure.Game(setting);
        JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4));
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                JButton b = new JButton(String.valueOf(i));
                panel.add(b);
            }
        }
        add(panel);
        pack();
        setVisible(true);
    }
    structure.Setting setting;
}
  • 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-16T00:56:20+00:00Added an answer on June 16, 2026 at 12:56 am

    You may use GridLayout to add equal height/width buttons:

    enter image description here

    public class Game extends JFrame {
        private JPanel panel;
        public Game(int rows,int cols,int hgap,int vgap){
            panel=new JPanel(new GridLayout(rows, cols, hgap, vgap));
            for(int i=1;i<=rows;i++)
            {
                for(int j=1;j<=cols;j++)
                {
                    JButton btn=new JButton(String.valueOf(i));
                    panel.add(btn);
                }
            }
            add(panel);
            pack();
            setVisible(true);
        }
    }
    

    and code in button’s handler should be:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       Game g = new Game(5,5,3,3);
    }
    

    Note that you can also pass Setting object reference via Game constructor (when you may add widgets dynamically) instead of calling setSetting method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: Reading integers from a file into an array I want to
Possible Duplicate: Java Full Screen Program (Swing) -Tab/ALT F4 I've got a full screen
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have
Possible Duplicate: Java hangman game with gui, problems with incrementing/decrementing numbers So the problem
Possible Duplicate: Java Serial Communication on Windows Friends, I want to connect and transfer
Possible Duplicate: Java String.equals versus == I thought this would be a neat way
Possible Duplicate: Java - easily convert array to set Can someone help me with
Possible Duplicate: Java: Array of primitive data types does not autobox I've been doing
Possible Duplicate: Java swing Table transparency It is not so easy to make a
Possible Duplicate: Java how to: Generic Array creation import java.util.EmptyStackException; import java.util.Vector; public class

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.