Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8941529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:19:26+00:00 2026-06-15T11:19:26+00:00

I am having a problem with check boxes in GUI. I have a 5

  • 0

I am having a problem with check boxes in GUI. I have a 5 check boxes and a button called “generate report”. What I want to do is when I press “generate report” I want to check which check boxes are selected so that I can “generate report” with the information selected in the check boxes. I know how to check which were selected but if a check box is selected and afterwards deselected before clicking “generate report” I am afraid that the program won’t know if it was deselected after it was selected.

Here is my code:

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

public class MonthReportGUI implements ItemListener
{
 static Calendar calendar = Calendar.getInstance();

 JFrame frame = new JFrame("Month Report");
 JPanel mainPanel = new JPanel();
 JPanel comboPanel = new JPanel();
 JLabel info = new JLabel("Use the check boxes to select the information to include in the month report");
 JCheckBox checkBoxOne = new JCheckBox("Number accomplished");
 JCheckBox checkBoxTwo = new JCheckBox("Number not accomplished");
 JCheckBox checkBoxThree = new JCheckBox("Total Number of Jobs");
 JCheckBox checkBoxFour = new JCheckBox("Month Salary");
 JCheckBox checkBoxFive = new JCheckBox("Average wage per job");

 Boolean boxOneSelected = false;

 static String [] monthList = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"};

 static String [] createYearList()
 {
   String [] yearList = new String[89];
   String year = (calendar.get(Calendar.YEAR)) + "";
   for(int i = 0; i < 88; i++)
      yearList[i] = (Integer.parseInt(year) + i) + "";
   return(yearList);
 }
 JLabel monthL = new JLabel("Month:");
 JLabel yearL = new JLabel("Year:");
 static JComboBox monthCB = new JComboBox(monthList);
 static JComboBox yearCB = new JComboBox(createYearList());
 JButton generate = new JButton("Generate Report"); // ACTION HAS TO BE ADDED

 Boolean oneSelected = false;
 Boolean twoSelected = false;
 Boolean threeSelected = false;
 Boolean fourSelected = false;
 Boolean fiveSelected = false;

 MonthReportGUI()
 {
   mainPanel.setLayout(new GridLayout(8,1));
   mainPanel.add(info, BorderLayout.CENTER);
   mainPanel.add(checkBoxOne, BorderLayout.CENTER);
   mainPanel.add(checkBoxTwo, BorderLayout.CENTER);
   mainPanel.add(checkBoxThree, BorderLayout.CENTER);
   mainPanel.add(checkBoxFour, BorderLayout.CENTER);
   mainPanel.add(checkBoxFive, BorderLayout.CENTER);
   mainPanel.add(comboPanel);
   mainPanel.add(generate, BorderLayout.CENTER);

   comboPanel.add(monthL);
   comboPanel.add(monthCB);
   comboPanel.add(yearL);
   comboPanel.add(yearCB);

   checkBoxOne.addItemListener(this);
   checkBoxTwo.addItemListener(this);
   checkBoxThree.addItemListener(this);
   checkBoxFour.addItemListener(this);
   checkBoxFive.addItemListener(this);

   frame.add(mainPanel);
   frame.pack();
   frame.setVisible(true); //set false
  }

  public void itemStateChanged(ItemEvent e) //perform action to know which are selected to use for writting report
  {
    Object source = e.getItemSelectable();
    if (source == checkBoxOne) 
    {
      System.out.println(boxOneSelected);
      boxOneSelected = true;
      System.out.println(boxOneSelected);
    }
  }

  public static void main (String agrs[])
  {
    MonthReportGUI monthReport = new MonthReportGUI();
  }
}

Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:19:29+00:00Added an answer on June 15, 2026 at 11:19 am

    There is no point in storing the selection state of a checkbox in a separate boolean field. Instead, when the button is clicked, ask the checkbox if it’s selected directly:

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            generateReport(checkbox1.isSelected(),
                           checkbox2.isSelected(),
                           checkbox3.isSelected(),
                           checkbox4.isSelected(),
                           checkbox5.isSelected());
        }
    });
    

    Doing it like you’re doing could lead to correct results, but you’re duplicating functionality, introducing a new path for bugs, and making it more complex than it should be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So the problem I am currently having is that I have A drop down
I having problem figuring howto preserve the present state ...Let say I have selected
Im having trouble with a jquery function where i have 5 option boxes (which
I'm new to programming and having problem with using getCheckedItemPositions() for check boxes and
I have 2 check boxes that shows and displays some divs. The first check
I am having problems here if I want to check if eerste points to
I'm having a problem trying to create a Javascript function that checks all the
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I'm having problem with datagrid view. I have attached an image with the code
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.