Search code examples
javaswingactionlistenergrid-layoutmouselistener

Reading and storing the random values of multiple JButtons on a gridLayout | Java SWING Finding a triplet


I'm trying to implement Finding a triplet exercise as SWING GUI, where there's a grid of random numbers and the user clicks three numbers. If the first two sums up to the third it increments a counter of correct tries. Trying to understand how can I store each clicked number in an object, so I could check for correctness.

I'm trying to understand where should I implement the actionListener, at the Square class or the Board class, and how to get and store the value of each clicked button

This is as far as I got in the matter of getting the first value, but have no idea how to read it outside the actionListener, or how to get the other values.

private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==finish)
                JOptionPane.showMessageDialog(null, "You made " + wrongs + "correct tries.");
            else{
                Square sqr = (Square) e.getSource();
                int num = sqr.getNum();
            }
        }
    }

I want to understand the principle of how to tackle such a problem. I have a feeling there's a whole different approach needs to be taken.


Solution

  • The solution not is a unique, but the resposte is depends.

    If you want give the sequence of the click with the numbar I think is better using an List.

    If you want give only result of the sum is better using unique veriable.

    An example solution, if Suppose we use a component with global visibility (although object-oriented programming does not recommend these components with global visibility)

    public class SingletonGame{
    
        private static final SingletonGame SINGLETON = new SingletonGame();
    
        public static SingletonGame getInstance(){
            return SINGLETON;
        }
    
        private int sum;
        //or
        private List<Integer> operations = new ArrayList<>();
    
        private SingletonGame(){}
    
        public int getSum(){
            return sum;
        }
    
        //or
        public List<Integer> getOperations(){
            return operations;
        }
    
        public void addSum(int num){
            this.sum += num;
        }
    
        //or
        public void addOp(int num){
            this.operations.add(num);
        }
    
        publi void getResult(){
            //inside this method you have your logic
            //for calculate result
        }
    
    }
    
    //Inside your action
    
    private class ButtonListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==finish)
                    JOptionPane.showMessageDialog(null, "You made " + wrongs + "correct tries.");
                else{
                    Square sqr = (Square) e.getSource();
                    int num = sqr.getNum();
                    // you must do control when is case for call SingletonGame.getInstance().getResult();
                    SingletonGame.getInstance().sum(num);
    
                }
            }
        }