Search code examples
javauser-interfacejbutton

How to count how many JButtons have been clicked from a 10 x 10 java JButton grid


I want to count the number of JButtons that has been clicked on from a 10 x 10 JButton grid.

This is what I am talking about enter image description here

Anyways, I don't know how to count how many JButtons have been clicked. I thought about making 100 JButtons but that seems silly.

Also how do I prevent more than 14 buttons from being clicked?

ActionListener al = new ActionListener()
{
        public void actionPerformed(ActionEvent e)
        {
             JButton button = (JButton)e.getSource();
             button.setEnabled( false );

        }
};

    for(int row = 0; row < 10; row++)
    {
       for(int col = 0; col < 10; col++)
       {
               button = new JButton();
               button.addActionListener( al );
               panel_1.add(button);
       }
    }

This is my forLoops for making 100 buttons and giving each of them an actionListener so when a JButton is clicked, it becomes unclickable.

    ActionListener al = new ActionListener()
    {
        int clicked = 0;

        public void actionPerformed(ActionEvent e)
        {
            button = (JButton)e.getSource();

             if(clicked != 14)
             {
                 clicked++;
             }
             else

                button = (JButton)e.getSource();
                button.setEnabled( false );

        }
    };

    for(int row = 0; row < 10; row++)
    {
        for(int col = 0; col < 10; col++)
        {
                button = new JButton();
                button.addActionListener( al );
                panel_1.add(button);

        }
    }

I have tried putting in a counter but it is obviously not correct. Can I even compared e.getSource() to an int or something?


Solution

  • If you want to count the number of clicks you will need to create an int (or long) variable to store them in, and simply add a ++ statement in the actionPerformed method:

    private int buttonClicks = 0; // Or public
    public void actionPerformed(ActionEvent e)
            {
                 if(buttonClicks == 14){
                   System.exit(0); // Or a different script
                 }else{
                 JButton button = (JButton)e.getSource();
                 button.setEnabled( false );
                 buttonClicks++; // Record click
                 }
            }
    

    and if you want to count the number of clicks on a specific button, you will need to create a string array containing all the button names which are found in the constructors:

       JButton jbtn = new JButton("Button") // Button is the name
    

    and a separate int array which will store the clicks. Then you can use a for loop to figure out which button was pressed, and increase it's click in the int array.

    Consider the following example:

    aClass(){
    
        JFrame jfrm = new JFrame("Example");
        jfrm.setSize(200, 200);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.setLayout(new FlowLayout());
        jfrm.setLocationRelativeTo(null);
    
        JButton jbtn1 = new JButton("Push");
        JButton jbtn2 = new JButton("Click");
        JButton jbtn3 = new JButton("Press");
    
        jbtn1.addActionListener(this);
        jbtn2.addActionListener(this);
        jbtn3.addActionListener(this);
    
        jfrm.add(jbtn1);
        jfrm.add(jbtn2);
        jfrm.add(jbtn3);
    }
    
    public static String[] buttonNames = {"Push", "Click", "Press"}; // Put button names in an array
    public static int[] buttonClicks = {0, 0, 0}; // Set the clicks to default
    
        public void actionPerformed(ActionEvent ae)
                {
                     for(int i = 0; i < buttons.length; i++){
                        if(ae.getActionCommand().equals(buttonNames[i])){
                        buttonClicks[i] =  buttonClicks[i] + 1; // Record the clicks. I think you can use buttonClicks[i]++, but I'm not sure
                        }
                     }
                }
    

    and anytime you need to access the number of clicks for a specific button, you could use something like the following:

    public static int getClicks(String buttonName){
           for(int i = 0; i < aClass.buttonNames.length; i++){
               if(buttonName.equals(aClass.buttonNames[i])){
               return aClass.buttonClicks[i];
               }
           }
    }
    

    and when you call that method, all you have to do is pass the button name to it as a string. getClicks("Push");