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

  • Home
  • SEARCH
  • 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 8505773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:24:55+00:00 2026-06-11T02:24:55+00:00

I have a Swing app with multiple JCheckBoxes. Each JCheckBox corresponds to another object

  • 0

I have a Swing app with multiple JCheckBoxes. Each JCheckBox corresponds to another object of type Foo have elsewhere in my program. For each object of type Foo, I’ll have one of these JCheckBoxes available for the user to click on (it tells the Foo object if they should display on the UI or not).

What I am currently doing is using the name attribute on JCheckBox as a reference back to the string “name” of the Foo object. I feel like a much cleaner solution would be to extend JCheckBox to add an extra field to it, so that the JCheckBox can keep track of which Foo object it’s attached to by simply storing a reference to it. Thoughts? Do people regularly extend Swing components so that they can link the parts in the controller/view back to the model? Is there some horrible pitfall I’m going to run into?

  • 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-11T02:24:56+00:00Added an answer on June 11, 2026 at 2:24 am

    It is hard to get the whole picture for this particular example without additional details. You may be able to accomplish this task using actions. See How to Use Actions for more details. Here is an example:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.*;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    public class CheckBoxDemo {
    
        static class Foo {
            private boolean visible;
            private String name;
    
            public Foo(String name) {
                this.name = name;
            }
    
            public boolean isVisible() {
                return visible;
            }
    
            public void setVisible(boolean visible) {
                this.visible = visible;
                System.out.println(getName() + ".setVisible:" + visible);
            }
    
            public String getName() {
                return name;
            }
        }
    
        static class FooAction extends AbstractAction {
            private Foo foo;
    
            public FooAction(Foo foo) {
                super(foo.getName());
                this.foo = foo;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox checkBox = (JCheckBox) e.getSource();
                foo.setVisible(checkBox.isSelected());
            }
        }
    
        private static void createAndShowGUI() {
            List<Foo> fooList = new ArrayList<Foo>();
            fooList.add(new Foo("Foo1"));
            fooList.add(new Foo("Foo2"));
            fooList.add(new Foo("Foo3"));
    
            JPanel content = new JPanel(new BorderLayout());
    
            JPanel fooPanel = new JPanel();
            for (Foo foo : fooList) {
                fooPanel.add(new JCheckBox(new FooAction(foo)));
            }
    
            content.add(fooPanel, BorderLayout.NORTH);
    
            final JTextArea textArea = new JTextArea();
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(200, 200));
            content.add(scrollPane, BorderLayout.CENTER);
    
            JFrame frame = new JFrame("CheckBoxDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(content);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
    
            // just for brevity of example
            System.setOut(new PrintStream(new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                    textArea.append(String.valueOf((char) b));
                }
            }));
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Swing app. I have a JFrame with few JPanels . One of
We have a one Java Swing app. Now there is one requirement that it
I have a simple Swing app compiled with netbeans 7.1.2 on a ubuntu machine
I have a Swing runnable app which updates messages, then I have a Java
I have various Java Swing applications that are used by multiple users. My deployment
i have a Swing app and i want to run it with Nimbus look'n'feel
I have a chat like desktop java swing app, where i keep getting String
I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table
I am creating a client side swing app that will have data provided by/from
I have a Swing app with a large panel which is wrapped in a

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.