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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:55:46+00:00 2026-06-13T02:55:46+00:00

Here is my program.This program simply creates a frame using swings in java and

  • 0

Here is my program.This program simply creates a frame using swings in java and create a grid first and adds button to that frame:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class one {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400,400);
        frame.getContentPane().setPreferredSize(new Dimension(500,500));
        frame.pack();
        frame.setVisible(true);
        Container pane=frame.getContentPane();
        pane.setLayout(new GridLayout(5,6));

        JButton[] buttons = new JButton[26];

        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(int 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);
                }
            });
            System.out.println("adding button\n");
            pane.add(buttons[i]);
        }

    }
    public static void main(final String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

The program compiles fine.But the button creation i can see only one button that is “A” and rest of the buttons i am not able to see in the pane.

  • 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-13T02:55:48+00:00Added an answer on June 13, 2026 at 2:55 am

    It turns out that this seems to be a race condition and/or system dependent type issue, since it works for others. In any case, the Javadoc for java.awt.Container.add() states:

    This method changes layout-related information, and therefore,
    invalidates the component hierarchy. If the container has already been
    displayed, the hierarchy must be validated thereafter in order to
    display the added component.

    You should therefore call pane.validate() after you have added all components (or, as MadProgrammer says, execute the setVisible() call after you have added all your components).

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

Sidebar

Related Questions

Here are the full codes that I am using to implement this program. Everything
NOTE: This is a followup to my question here. I have a program that
i'm trying to create a simple Opencv program in C that creates a file
I'm running a program that creates a table and then inserts some data. This
I have a simple java program that creates a series of temporary files stored
This is a really simple program I'm writing here but came up to the
there is this check-in-out program here at my workplace, it only takes the data
I am trying to send mail using SmtpClient() within my Winforms VB.Net program Here
I have a simple Java program that reads in a text file, splits it
I have a simple program which creates a thread, and waits when this thread

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.