//MY Problem is that i'm trying to make a lottery program, the issue i have is when i click a button, i want it to be so that after i click 6 buttons, lets say ,1,2,3,4,5,6, the program will move onto the next stage( i have yet to build that)
Howver it allows me click as many buttons as i want and never ends...
Can i put a loop on action listener somehow? I did that but all it did was loop 6 times through the one button, so essentially print out 1 six times and so on..

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.util.Random;
import javax.swing.JOptionPane;

public class DisplayButtonMessage
{


    public DisplayButtonMessage()//Main constructor
    {
        //method for the everything that happens


        JFrame frame = new JFrame("Lottery App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(11, 4));//create gridlayput to hold buttons


        ActionListener al = new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent event) 
            {
                //display action command of jbutton


                String num = ((JButton) event.getSource()).getActionCommand();

                System.out.println(num);





                } 

        };

        for (int i = 0; i < 44; i++) 
        {
            JButton b = new JButton("No: " + String.valueOf((i + 1)));
            //Creates a button each iteration 
            //It labels each number the value of counter+1



            b.setActionCommand(String.valueOf((i + 1)));
            //This gets the value of the button, counter + 1 also

             b.addActionListener(al);//Adds action listener for above action Performed  



            panel.add(b);//Adds "b" to panel

        }

















        frame.add(panel);//Adds the panel to frame

        frame.pack();
        frame.setVisible(true);//Sets the frame visible




    }}        

//This is my main class.

import javax.swing.SwingUtilities;
public class apples
{
     public static void main(String[] args)
    {
        //set L&F and create UI on EDT
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {


                new DisplayButtonMessage();//Runs the main constructor

            }
        });
    }



}

All you need is a counter in your DisplayButtonMessage class that you can increment inside the actionPerformed until it reaches 6. You need to declare it in the class so its value will persist between the sucessive calls to actionPerformed.

Hi, i think i know what you mean but could you give me an example if possible? As in i need a while loop in my main class? Which i can say while less than 6, then in my action performed add ++ each time to it?

You don't need any kind of loop for this. Just declare the counter in the class and initialise it to zero. In your actionPerformed add 1 to it, then test to see if it's reached 6 yet.

Ok i'm following you, i have a variable called loop in my class now, initialized to 0, in my action performed i have loop++ so anytime you click a button, it essentially adds one to loop.
Then i have an "if" statement to say,
if(loop==6)

WHAT GOES HERE TO MOVE ONTO THE NEXT PART?

But how would i tell the program that i need it to close the grid and move onto my next part which i'll create?

This is what i have so far ...

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.util.Random;
import javax.swing.JOptionPane;

public class DisplayButtonMessage
{
        //display action command of jbutton
         int matches=0;
           int loop=0; 
    public DisplayButtonMessage()//Main constructor
    {
        //method for the everything that happens


        JFrame frame = new JFrame("Lottery App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new GridLayout(11, 4));//create gridlayput to hold buttons



        ActionListener al = new ActionListener() 
        {

            public void actionPerformed(ActionEvent event) 
            {


                loop++;


 ////////////////////////////////////////////////////////////////////

                String num = ((JButton) event.getSource()).getActionCommand();

                System.out.println(num);

                  int newNum= Integer.parseInt(num);//Number is no an integer


                  for(int counter=0; counter<5; counter++)
                  {
                  Random dice=new Random();
                  int number =dice.nextInt(45); 
                //Creates 6 random numbers for me



                  if(newNum==number)
                  {

                      matches++;
                     System.out.printf("You have %d Matches\n",matches);
                  }
                  //If my original numbers equal any of the random ones
                  //It adds one to a matches counter

                  }
                  if(loop==6)
                  {

                  }
            }








        };

 ///////////////////////////////////////////////////////////////////////////////









        for (int i = 0; i < 44; i++) 
        {
            JButton b = new JButton("No: " + String.valueOf((i + 1)));
            //Creates a button each iteration 
            //It labels each number the value of counter+1



            b.setActionCommand(String.valueOf((i + 1)));
            //This gets the value of the button, counter + 1 also

             b.addActionListener(al);//Adds action listener for above action Performed  



            panel.add(b);//Adds "b" to panel


        }















        frame.add(panel);//Adds the panel to frame

        frame.pack();
        frame.setVisible(true);//Sets the frame visible




    }



 }

`

OK i think i figured it. When it hits 6, another box pops up telling you how many matches you had to what you picked...best i can do!

Thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.