Here is my assignment: In Chapter 5, you created a lottery game application. Create a similar game using check boxes. For this game, generate six random numbers, each between 0 and 30 inclusive. Allow the user to choose six check boxes to play the game. (Do not allow the user to choose more than six boxes.) After the player has chosen six numbers, display the randomly selected numbers, the player ’ s numbers, and the amount of money the user has won, as follows: Matching Numbers Three matches Four matches Five matches Six matches Zero, one, or two matches Award ($) 100 10,000 50,000 1,000,000 0

Additional requirements are as follows:
• Add a menu bar to the program with a File menu.
• In the File menu, add a submenu (JMenuItem) called About.
• When the user clicks on the About menu item, display a
JOptionPane message dialog that contains your name, your
course, the section number, and MEID.


The primary issue I am having is with the limiting of checkboxes, I cannot seem to find examples of this in the text or anywhere online (except in javascript and html, which obviously do not help me here.) I have posted my code below. Please note that I am only looking for answers to the limit on check boxes at the moment the rest of the code I can complete myself.

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

public class JLottery2 extends JFrame implements ActionListener
{
    //instantiate constants
    final int HIGHEST_VAL = 30;
    final int LOWEST_VAL = 1;
    final int BOXES_CHECKED = 6;
    final int WIDE = 400;
    final int TALL = 400;
    
    //set random number generator
    int numR = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR2 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR3 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR4 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR5 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR6 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    
   

    @SuppressWarnings("LeakingThisInConstructor")
    public JLottery2() 
    {
        //set title and size of the frame
        setTitle("Final Project");
        setSize(750, 750);
        
        // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
        
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        
        // Create and add simple menu item to one of the drop down menu
        JMenuItem aboutAction = new JMenuItem("About");  
        
        //add about to the file menu
        fileMenu.add(aboutAction); 
        
        //set the layout for the checkboxes
        JPanel checkBoxPane = new JPanel();
        checkBoxPane.setLayout(new GridLayout(6, 5));

        //instantiate new checkboxes
        JCheckBox box1 = new JCheckBox("1");
        JCheckBox box2 = new JCheckBox("2");
        JCheckBox box3 = new JCheckBox("3");
        JCheckBox box4 = new JCheckBox("4");
        JCheckBox box5 = new JCheckBox("5");
        JCheckBox box6 = new JCheckBox("6");
        JCheckBox box7 = new JCheckBox("7");
        JCheckBox box8 = new JCheckBox("8");
        JCheckBox box9 = new JCheckBox("9");
        JCheckBox box10 = new JCheckBox("10");
        JCheckBox box11 = new JCheckBox("11");
        JCheckBox box12 = new JCheckBox("12");
        JCheckBox box13 = new JCheckBox("13");
        JCheckBox box14 = new JCheckBox("14");
        JCheckBox box15 = new JCheckBox("15");
        JCheckBox box16 = new JCheckBox("16");
        JCheckBox box17 = new JCheckBox("17");
        JCheckBox box18 = new JCheckBox("18");
        JCheckBox box19 = new JCheckBox("19");
        JCheckBox box20 = new JCheckBox("20");
        JCheckBox box21 = new JCheckBox("21");
        JCheckBox box22 = new JCheckBox("22");
        JCheckBox box23 = new JCheckBox("23");
        JCheckBox box24 = new JCheckBox("24");
        JCheckBox box25 = new JCheckBox("25");
        JCheckBox box26 = new JCheckBox("26");
        JCheckBox box27 = new JCheckBox("27");
        JCheckBox box28 = new JCheckBox("28");
        JCheckBox box29 = new JCheckBox("29");
        JCheckBox box30 = new JCheckBox("30");

        //add checkbox panel to the frame
        add(checkBoxPane);
        
        //add check boxes to the frame
        checkBoxPane.add(box1);
        checkBoxPane.add(box2);
        checkBoxPane.add(box3);
        checkBoxPane.add(box4);
        checkBoxPane.add(box5);
        checkBoxPane.add(box6);
        checkBoxPane.add(box7);
        checkBoxPane.add(box8);
        checkBoxPane.add(box9);
        checkBoxPane.add(box10);
        checkBoxPane.add(box11);
        checkBoxPane.add(box12);
        checkBoxPane.add(box13);
        checkBoxPane.add(box14);
        checkBoxPane.add(box15);
        checkBoxPane.add(box16);
        checkBoxPane.add(box17);
        checkBoxPane.add(box18);
        checkBoxPane.add(box19);
        checkBoxPane.add(box20);
        checkBoxPane.add(box21);
        checkBoxPane.add(box22);
        checkBoxPane.add(box23);
        checkBoxPane.add(box24);
        checkBoxPane.add(box25);
        checkBoxPane.add(box26);
        checkBoxPane.add(box27);
        checkBoxPane.add(box28);
        checkBoxPane.add(box29);
        checkBoxPane.add(box30);
        
        //event handling
        aboutAction.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent a) 
    {
        //message to be displayed when About selection is made
        JOptionPane.showMessageDialog(null, "Student Information Here");
    }

    //main method to execute
    public static void main(String args[]) 
    {
        //instantiate object for execution
        JLottery2 displayIt = new JLottery2();
        displayIt.setVisible(true);
    }
}

Recommended Answers

All 10 Replies

setCount to zero
while setCount < 6
{
add lotteryNumber
add 1 to setCount
}

setCount to zero
while setCount < 6
{
add lotteryNumber
add 1 to setCount
}

When you say add lotteryNumber are you mapping this to anything else? I can see how this would create a loop no greater than 6 but I am not seeing how it limits the users choice of check box. Please elaborate. Thanks!

it was just a little pseudocode to help you with one part.
what do you mean by limits the users choice of check box?

This in particular is what I am trying to do."Allow the user to choose six check boxes to play the game. (Do not allow the user to choose more than six boxes.)" I am not certain how to implement this rule.


I understand using a loop to limit the number of random numbers, I had just created several number generators so i will be adjusting it to use the loop thank you.

set a counter to zero
every time he chooses one,
add 1
when he selects one, first check whether the value for the counter is less then six, if so, select it and add 1 to the counter, if not, don't select

Okay this is what I am doing now.

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

public class JLottery2 extends JFrame implements ItemListener
{
    //instantiate constants
    final int HIGHEST_VAL = 30;
    final int LOWEST_VAL = 1;
    final int BOXES_CHECKED = 6;
    final int WIDE = 400;
    final int TALL = 400;
    
    //set random number generator
    int numR = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR2 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR3 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR4 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR5 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    int numR6 = ((int) (Math.random() * 100) % HIGHEST_VAL + LOWEST_VAL);
    
    //instantiate new checkboxes
        JCheckBox box1 = new JCheckBox("1");
        JCheckBox box2 = new JCheckBox("2");
        JCheckBox box3 = new JCheckBox("3");
        JCheckBox box4 = new JCheckBox("4");
        JCheckBox box5 = new JCheckBox("5");
        JCheckBox box6 = new JCheckBox("6");
        JCheckBox box7 = new JCheckBox("7");
        JCheckBox box8 = new JCheckBox("8");
        JCheckBox box9 = new JCheckBox("9");
        JCheckBox box10 = new JCheckBox("10");
        JCheckBox box11 = new JCheckBox("11");
        JCheckBox box12 = new JCheckBox("12");
        JCheckBox box13 = new JCheckBox("13");
        JCheckBox box14 = new JCheckBox("14");
        JCheckBox box15 = new JCheckBox("15");
        JCheckBox box16 = new JCheckBox("16");
        JCheckBox box17 = new JCheckBox("17");
        JCheckBox box18 = new JCheckBox("18");
        JCheckBox box19 = new JCheckBox("19");
        JCheckBox box20 = new JCheckBox("20");
        JCheckBox box21 = new JCheckBox("21");
        JCheckBox box22 = new JCheckBox("22");
        JCheckBox box23 = new JCheckBox("23");
        JCheckBox box24 = new JCheckBox("24");
        JCheckBox box25 = new JCheckBox("25");
        JCheckBox box26 = new JCheckBox("26");
        JCheckBox box27 = new JCheckBox("27");
        JCheckBox box28 = new JCheckBox("28");
        JCheckBox box29 = new JCheckBox("29");
        JCheckBox box30 = new JCheckBox("30");
    
    @SuppressWarnings("LeakingThisInConstructor")
    public JLottery2()
    {
        //set title and size of the frame
        setTitle("Final Project");
        setSize(750, 750);
        
        //set the layout for the checkboxes
        JPanel checkBoxPane = new JPanel();
        checkBoxPane.setLayout(new GridLayout(6, 5));

        //add checkbox panel to the frame
        add(checkBoxPane);
        
        //add check boxes to the frame
        checkBoxPane.add(box1);
        checkBoxPane.add(box2);
        checkBoxPane.add(box3);
        checkBoxPane.add(box4);
        checkBoxPane.add(box5);
        checkBoxPane.add(box6);
        checkBoxPane.add(box7);
        checkBoxPane.add(box8);
        checkBoxPane.add(box9);
        checkBoxPane.add(box10);
        checkBoxPane.add(box11);
        checkBoxPane.add(box12);
        checkBoxPane.add(box13);
        checkBoxPane.add(box14);
        checkBoxPane.add(box15);
        checkBoxPane.add(box16);
        checkBoxPane.add(box17);
        checkBoxPane.add(box18);
        checkBoxPane.add(box19);
        checkBoxPane.add(box20);
        checkBoxPane.add(box21);
        checkBoxPane.add(box22);
        checkBoxPane.add(box23);
        checkBoxPane.add(box24);
        checkBoxPane.add(box25);
        checkBoxPane.add(box26);
        checkBoxPane.add(box27);
        checkBoxPane.add(box28);
        checkBoxPane.add(box29);
        checkBoxPane.add(box30);
        
        //event handling
        box1.addItemListener(this);
        box2.addItemListener(this);
        box3.addItemListener(this);
        box4.addItemListener(this);
        box5.addItemListener(this);
        box6.addItemListener(this);
        box7.addItemListener(this);
        box8.addItemListener(this);
        box9.addItemListener(this);
        box10.addItemListener(this);
        box11.addItemListener(this);
        box12.addItemListener(this);
        box13.addItemListener(this);
        box14.addItemListener(this);
        box15.addItemListener(this);
        box16.addItemListener(this);
        box17.addItemListener(this);
        box18.addItemListener(this);
        box19.addItemListener(this);
        box20.addItemListener(this);
        box21.addItemListener(this);
        box22.addItemListener(this);
        box23.addItemListener(this);
        box24.addItemListener(this);
        box25.addItemListener(this);
        box26.addItemListener(this);
        box27.addItemListener(this);
        box28.addItemListener(this);
        box29.addItemListener(this);
        box30.addItemListener(this);
    }
    
    private class JLottery extends JFrame implements ActionListener
    {
        @SuppressWarnings("LeakingThisInConstructor")
        public JLottery()
        {
            
            // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
        
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        
        // Create and add simple menu item to one of the drop down menu
        JMenuItem aboutAction = new JMenuItem("About");  
        
        //add about to the file menu
        fileMenu.add(aboutAction); 
        
        aboutAction.addActionListener(this);
        }
        
        @Override
        public void actionPerformed(ActionEvent a) 
        {
            //message to be displayed when About selection is made
            JOptionPane.showMessageDialog(null, "Dave Morand\ndavem60641\nCIS263AA - 33073\nJava Programming: Level II");
        }
    }
    
    @Override
    public void itemStateChanged(ItemEvent check)
    {
         Object source = check.getItem();
         if(source == box1)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box2)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box3)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box4)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box5)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box6)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box7)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box8)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box9)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box10)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box11)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box12)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box13)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box14)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box15)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box16)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box17)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box18)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box19)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box20)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box21)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box22)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box23)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box24)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box25)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box26)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box27)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box28)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box29)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         else
         if(source == box30)
         {
             int select = check.getStateChange();
             if(select == ItemEvent.SELECTED)
             {
                 for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)
                     {
                         JOptionPane.showMessageDialog(null, "Please try again. You can only select 6 boxes");
                     }
                 }
             }
         }
         
    }
    

    //main method to execute
    public static void main(String args[]) 
    {
        //instantiate object for execution
        JLottery2 displayIt = new JLottery2();
        JFrame holdIt = new JFrame();
        holdIt.add(displayIt);
    }
}

and this is the error I am receiving.

run:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
	at java.awt.Container.checkNotAWindow(Container.java:419)
	at java.awt.Container.addImpl(Container.java:1008)
	at java.awt.Container.add(Container.java:928)
	at javax.swing.JFrame.addImpl(JFrame.java:545)
	at java.awt.Container.add(Container.java:348)
	at JLottery2.main(JLottery2.java:632)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Please, use the following for your labels:

JLabel [] labels = new JLabel[20];
		MyClass() {
			setLayout(new FlowLayout());
			
			for (int i=0; i<labels.length; i++) {
				labels[i] = new JLabel (String.valueOf (i+1));
				add(labels[i]);
			}
			
		}

what hidde said, definitly, also your random number generation probly works but it would be more "appropriate" to use the "logical" way to set their value :

number = (int)(Math.random()*30+1)

what i mean by logical is if you were to translate both ways to pseudo code, the one i'm giving you would resemble more how you would explain the generation steps to someone in english.

also make those 6 diferent variables an array of 6 variables and but the above line in a loop , just like the creation/adition of the labels.

Id also suggest making the assignation of random numbers in a method so that its reusable when you want to generate more than 1 set of winning numbers per execution of your program.

Now on to your issue :

for(int i = 0; i < 6; ++i)
                 {
                     if(i > 6)...

please tell me you can see above that your if statements with error messages are never going to execute. The for loops from 0 to 5 , the if within that very loop checks if the value is over 6. impossible to reach.

Solution , drop all the loops and ifs in the "public void itemStateChanged(ItemEvent check)" ...

instead set up a global variable "iNbChecked" and initialize it to zero , everytime user clicks a checkbox, if hes selecting it , increment iNbChecked, as long as its under 6, if its value is 6 , then manualy set the checkbox to unselected and fire your error message. If user is un-selecting a selected checkbox, then iNbChecked--; and thats it.

Im not going to get into how but there is surely a way to do your "public void itemStateChanged(ItemEvent check)" without 30 ifs for 30 checkboxes, all their names are standarized, use String functions to isolate number and then do the code once with that number and your checkbox array.

Good luck :)

(look at your code, and imagine i ask you to do the same concept with 25000 checkboxes in a 5000 by 5000 grid, THAT is why you are going the wrong way about it and learning how to do this now will make you a better programmer in the long run! )

Great! Thanks all. This is very helpful. And yes I see the issue with doing so many if statements much easier to use a checkbox array. Thanks for the help.

a switch statement would also have been easier.

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.