0

I want to add 52 buttons in a JPanel. All with ActionListeners. When I reach a certain amount (13) and run the program, not all of the buttons show up. For example I've added 15 buttons and only 9 or 12 of them show up. Sometimes all of them, but not every time.

Here's the code for two of the JButtons:

    JButton button_one=new JButton();
    button_one.setPreferredSize(new Dimension(100,150));
    mainpanel.add(button_one);
    button_one.addActionListener(new ActionListener(){
        
        @Override
        public void actionPerformed(ActionEvent button_1_picked){
            amount_of_cards_picked=amount_of_cards_picked+1;
            cardamountcheck();
            if(twocardspicked==true){
                userpick2=0;
                System.out.println(setoutcards[userpick2]);
                pairdetermination();
            }
            else if(twocardspicked==false){
                userpick1=0;
                System.out.println(setoutcards[userpick1]);
                
            }
        }
    });
    
    JButton button_two = new JButton();
    button_two.setPreferredSize(new Dimension(100, 150));
    mainpanel.add(button_two);
    button_two.addActionListener(new ActionListener(){
        
        @Override
        public void actionPerformed(ActionEvent button_2_picked){
            amount_of_cards_picked=amount_of_cards_picked+1;
            cardamountcheck();
            if(twocardspicked==true){
                userpick2=1;
                System.out.println(setoutcards[userpick2]);
                pairdetermination();
            }
            else if(twocardspicked==false){
                userpick1=1;
                System.out.println(setoutcards[userpick1]);
                
            }
        }
    });

Basically when one of these buttons are clicked, variables in my code get changed. These buttons run fine and work exactly how I want them to, but they don't all appear when there is more than 13 of them, and I need 52.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What is the layout manager used? – NomadMaker Jan 06 '21 at 12:52
  • You haven't posted the code where you attempt to add the 52 buttons – ControlAltDel Jan 06 '21 at 13:14
  • @ControlAltDel It's literally the same button copied and pasted 52 times, I only posted two as an example. – Michal Gis Jan 06 '21 at 13:16
  • 1
    Also I strongly advise you to follow MVC design. Build your model (the matching game) first, without the GUI. Test it, and then when everything is working attach a GUI to it – ControlAltDel Jan 06 '21 at 13:16
  • @NomadMaker JFrame and JPanel in java – Michal Gis Jan 06 '21 at 13:16
  • @ControlAltDel I've done that. The game works just how I want it to without the GUI. – Michal Gis Jan 06 '21 at 13:18
  • 1
    *It's literally the same button copied and pasted 52 times* - well then rewrite the code to use a loop. See: https://stackoverflow.com/a/33739732/131872 for a working example. Then post a proper [mre] (as demonstrated in the above link) if you still have problems. – camickr Jan 06 '21 at 15:16
  • 1
    A [mre] should have a ``public static void main(String[] args)`` method and the import statements. It should be possible for us to copy and paste it into our ide and run it. Otherwise you are handicapping the people who want to help you. – NomadMaker Jan 06 '21 at 16:01

1 Answers1

2

I want to add 52 buttons in a JPanel. All with ActionListeners.

Done. I created a GUI that displays 52 JButtons. I made them half the size of the OP's, so the GUI would fit better in the answer.

52 JButton GUI

It's fairly straightforward using a GridLayout on a JPanel.

Here's the complete runnable code. A minimal reproducible example.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FiftyTwoJButtonGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FiftyTwoJButtonGUI());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("52 JButton GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 13, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        CardListener listener = new CardListener();
        
        for (int i = 0; i < 52; i++) {
            JButton button = new JButton("" + (i + 1));
            button.addActionListener(listener);
            button.setPreferredSize(new Dimension(50, 75));
            panel.add(button);
        }
        
        return panel;
    }
    
    public class CardListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            int cardNumber = Integer.valueOf(button.getText());
            System.out.println(cardNumber);
        }
        
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111