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 599491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:30:16+00:00 2026-05-13T16:30:16+00:00

I am new to swing, I would like to ask how can I make

  • 0

I am new to swing, I would like to ask how can I make this code , so when the submit button is pressed to get the selected checkboxes and display their names.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class ACheckBox {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "CheckBox Sample" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Pizza Toppings");
    panel.setBorder(border);
    JCheckBox check = new JCheckBox("Anchovies");
    panel.add(check);
    check = new JCheckBox("Garlic");
    panel.add(check);
    check = new JCheckBox("Onions");
    panel.add(check);
    check = new JCheckBox("Pepperoni");
    panel.add(check);
    check = new JCheckBox("Spinach");
    panel.add(check);
    JButton button = new JButton("Submit");
    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}
  • 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-05-13T16:30:16+00:00Added an answer on May 13, 2026 at 4:30 pm

    You need to define a bespoke ActionListener implementation whose job is to analyse each JCheckBox‘s selected state when an action is fired; i.e. when the “Submit” button is clicked.

    // ActionListener implementation that maintains a reference to each JCheckBox.
    // We will register this listener with the Submit button.
    public class MyActionListener implements ActionListener {
      private final List<JCheckBox> checkBoxes = new LinkedList<JCheckBox>();
    
      /**
       * Adds the specified JCheckBox to the list of JCheckBoxes.
       */
      public void addCheckBox(JCheckBox checkBox) {
        this.checkBoxes.add(checkBox);
      }
    
      /**
       * Called when the Submit button is pressed.
       */
      public void actionPerformed(ActionEvent evt) {
        StringBuilder sb = new StringBuilder();
        sb.append("Selected Check Boxes: ");
    
        // Iterate over each JCheckBox and build message ready for display.
        // Could do something more sophisticated here if required.
        for (JCheckBox checkBox : checkBoxes) {
          if (checkBox.isSelected()) {
            sb.append(checkBox.getText()).append(' ');
          }
        }
    
        JOptionPane.showMessageDialog(null, sb);
      }
    }
    
    // 1. Create ActionListener implementation.
    MyActionListener al = new MyActionListener();
    
    // 2. Register JCheckBoxes with ActionListener.
    al.addCheckBox(checkBox);
    // etc.
    
    // 3. Finally register ActionListener with Submit button.
    submitButton.addActionListener(al);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java, I would like to know how can I get my
I would like to open another swing gui after clicking a button and I
i would like to ask for further information and suggestion about this problem. I
I would like to create a new event-dispatch thread in Swing, and I'm having
I just want to ask, I am new to Java and I would like
I'm new to Java Swing applications and I'd like to port an old WindowsForm
In a swing application, I would like to re-utilize a spawned thread instead of
I would like to customize my titlebar, minimize-, maximize- and the close-button. So I
I have a GUI problem that I would like to get sorted out, but
I would like to use a Swing Timer , in a Java class that

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.