1

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);

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Linell
  • 750
  • 3
  • 9
  • 27
  • 3
    Regarding, `" I'm 99% sure that everything is correct in every other part of the code, although I can post it if needed."`: you don't know where the error is coming from, so you would be best to make no assumptions. I can't tell what is wrong based on this snippet, and no one has the time or desire to see all the code, and so I suggest that you boil everything down to an [sscce](http://sscce.org) and post that. – Hovercraft Full Of Eels Feb 01 '13 at 23:18

2 Answers2

6
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 for the Layout Manager suggestion. You are using a BorderLayout. The add(..) method will simply add the component to the center. Only one component can be displayed in the center. You need to use a LayoutManager which supports multiple components. – camickr Feb 02 '13 at 03:48
1

One issue is that you need to revalidate your JPanel after adding a RoachComponent:

panel.revalidate();
Reimeus
  • 158,255
  • 15
  • 216
  • 276