JohnnyTheJet
JohnnyTheJet

Reputation: 129

Pass array into ActionListener, another array triggers events

Below is the code I've got. I'm trying to create an array of 6 random numbers and 30 check boxes. In the action listener, I want to validate that the six boxes the user clicked happen to be my six random numbers. However, I can't pull down my random number array to the actionlistener. Can someone give me some guidance please? I'm a total newb at this. The int[] rand array is what is giving me grief.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class JLottery2 extends JFrame implements ActionListener
{
   int actionCounter = 0;
   int matchTally = 0;

   final int MAXBOXES = 6; 
   final int WIDTH = 500; 
   final int HEIGHT = 200;
   JCheckBox[] boxes = new JCheckBox [30];

   public JLottery2()
   {
      super ("~*~*~ JLOTTERY 2 ~*~*~");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout (new FlowLayout());
      setSize(WIDTH, HEIGHT);

      System.out.println("Constructor");

      //great the check boxes and assign them a value      
      for (int count=0 ; count < 30; count++)
      {
         boxes[count] = new JCheckBox (Integer.toString(count));
         add(boxes[count]);
         boxes[count].addActionListener(this);
      }

      int[] rand = new int[MAXBOXES]; 
      for (int i = 0; i < MAXBOXES; i++)
      {
         rand[i] = (int)(Math.random() * 30);
         //incase it tries to generate the same random number 
         for (int j = 0; j < i; j++)
         {
            if(rand[i] == rand[j])
            {
              i--; 
            }
         }
      }


      setVisible(true);
   }

   public void actionPerformed(ActionEvent e, )
   {      
      System.out.println(rand[5]);

      if(actionCounter < MAXBOXES)
      {
         System.out.println("Action Triggered");
         Object source = e.getActionCommand();
         System.out.println(source);         
         actionCounter++;
         System.out.println(actionCounter);   
      }
      else 
      {
         System.out.println("You reached the max at " + actionCounter);
      }

   } 

   public static void main(String[] args)
   {
      JLottery2 prog = new JLottery2();
   }
}

Upvotes: 1

Views: 243

Answers (2)

Alexander Guyer
Alexander Guyer

Reputation: 2193

The actionPerformed method cannot access rand as rand is declared in the local scope of the JLottery2 constructor. If rand is declared in a global scope, as in, outside of all methods (just as MAXBOXES, WIDTH, HEIGHT, etc. are), then it would be accessible. Note that you can still initialize it within the constructor:

public class JLottery2 extends JFrame implements ActionListener
{
    int actionCounter = 0;
    int matchTally = 0;
    int[] rand;
    ......
    public JLottery2()
    {
        ....
        rand = new int[MADBOXES];

EDIT: Here's a good link for you to check out. It explains scope with basic Java code.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347204

Make rand an class instance field like boxes

public class JLottery2 extends JFrame implements ActionListener
{
     //...
     JCheckBox[] boxes = new JCheckBox [30];
     int[] rand;

     public JLottery2()
     {
         //...
         rand = new int[MAXBOXES]; 

This now gives you a class level context to the field, which allows you to access from anywhere within the class

Upvotes: 1

Related Questions