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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:00:25+00:00 2026-06-18T06:00:25+00:00

I am trying to create a JPanel with two simple buttons. The first button

  • 0

I am trying to create a JPanel with two simple buttons. The first button doubles the number of RoachComponent components in the panel. The second button will clear all of the roaches off.

Now, I have the majority of the program working, but there is a rather serious problem. Only one roach per click is added, even when it’s supposed to be adding a couple thousand roaches. I have tested everything that I can think of, but it’s still eluding me. Even when I manually add two roaches, only one is displayed. Here is the code in my main function. I’m 99% sure that everything is correct in every other part of the code, although I can post it if needed.

final JFrame frame = new JFrame();
    final JPanel panel = new JPanel();
    // Button to create a new roach
    JButton button = new JButton("New Roach");

    final RoachPopulation roachPopulation = new RoachPopulation(2);

    // The label for displaying the results
    final JLabel label = new JLabel("Population: " + roachPopulation.getPopulation());

    // ActionListener class
    class doubleListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            System.out.println("------------------");
            for (int i = 0; i < roachPopulation.getPopulation(); i++) {
                RoachComponent newRoach = new RoachComponent();
                panel.add(newRoach);
                newRoach.getCoords();
            }
            panel.repaint();
            frame.repaint();
            roachPopulation.doublePopulation();
            label.setText("Population: " + roachPopulation.getPopulation());
            // Showing that there *is* the correct number of roaches in the panel.
            System.out.println(panel.getComponentCount());
        }
    }
    RoachComponent r1 = new RoachComponent();
    RoachComponent r2 = new RoachComponent();
    panel.setLayout(new BorderLayout());
    panel.add(button, BorderLayout.SOUTH);
    panel.add(label, BorderLayout.NORTH);
    panel.add(r1);
    panel.add(r2);
    //panel.add(component, BorderLayout.CENTER);
    frame.add(panel);

    frame.repaint();

    ActionListener doubleListener = new doubleListener();
    button.addActionListener(doubleListener);

    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
  • 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-18T06:00:26+00:00Added an answer on June 18, 2026 at 6:00 am
    • remove all code for frame.repaint(); and to add panel.revalidate() before jpanel.repaint();

    • (final look) everything depends of used LayoutManager for JPanel

    • for better help sooner post and SSCCE

    • this post will help you

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

Sidebar

Related Questions

I am trying to create a JPanel which has 3 components arranged in a
I'm trying to create simple Pong game. The first thing I'm working on is
I am trying to create this simple GUI where the number of clicks is
I trying to create a JPanel that draws rectangles. The Panel needs to draw
I am trying to create a JScrollPane that contains a JPanel that will be
I've trying to create a JToolTip over a JPanel . The idea is, that
I am trying to create a layout that will looking like: +---+--------+---+ | |
im trying to create a custom JPanel which i add into a card layout
I'm trying to create a application where the taskbar and the JPanel(for example) has
I am trying to create a custom panel for a very basic statistical calculator

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.