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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:11:58+00:00 2026-05-28T14:11:58+00:00

that’s the problem.. This is a simplified version of what i would like do:

  • 0

that’s the problem..
This is a simplified version of what i would like do:

public class Class{
    public void main(){
       Vector<Boolean> boo=new Vector<Boolean>;
       System.out.println("Hi all");
       ArrayList<String> a=new ArrayList<String>()
       a.add("hi");
       a.add("all");
       JRadioButtonExample b=new JRadioButtonExample(2,a);
       boo=b.getCheck();
       for(Boolean b:boo){
         System.out.println(b);
       }
    }
}

I must call an external class for the GUI..
The problem is that i can’t manage to syncronize the system.out.println in the main with the actionperformed in the JRadioButtonExample.

The JRadioButtonExample class is as follows:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;


public class JRadiobuttonExample extends JFrame {

static JCheckBox b[]; 
static Vector<Boolean> check=new Vector<Boolean>();
JButton altervista=new JButton("RUN");
JButton selectall=new JButton("select all");
JButton deselectall=new JButton("deselect all");
static int num;
int i=0;

public static JCheckBox[] getB() {
    return b;
}
public void setB(JCheckBox[] b2) {
    b = b2;
}
public Vector<Boolean> getCheck() {
    return check;
}
public void setCheck(Vector<Boolean> check2) {
    check = check2;
}
public JRadiobuttonExample(int num, ArrayList<String> lbl) {

    super("JRadiobuttonExample");

    getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));

    b= new JCheckBox[num];

    for(i=0; i<num; i++) {
        //creo i bottoni
        b[i] = new JCheckBox(lbl.get(i));
        getContentPane().add(b[i]);
    }

    //adding buttons
    getContentPane().add(selectall);
    getContentPane().add(deselectall);
    getContentPane().add(altervista);

    //adding listeners
    AscoltatoreSel asc1=new AscoltatoreSel();
    selectall.addActionListener(asc1);
    setVisible(true);

    AscoltatoreDesel asc2=new AscoltatoreDesel();
    deselectall.addActionListener(asc2);
    setVisible(true);

    Ascoltatore asc=new Ascoltatore();
    altervista.addActionListener(asc);
    setVisible(true);

    this.pack();
}

class Ascoltatore extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==altervista){
            setVisible(false);
            boh(b);
        }
    }
}

class AscoltatoreSel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==selectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(true);
                setVisible(true);
            }
        }
    }
}

class AscoltatoreDesel extends WindowAdapter implements ActionListener{
    public void windowClosing(WindowEvent e){
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==deselectall){
            for(i=0; i<num; i++) {
                b[i].setSelected(false);
            }
        }
    }
}


public static void boh(JCheckBox[] b){
    JCheckBox[] buttons=getB();

    for (JCheckBox c:buttons){
        check.add(c.isSelected());
    }

}

}

Thanks in advance!

p.s. if all checkboxes are selected i need to get boo=[true;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-28T14:11:59+00:00Added an answer on May 28, 2026 at 2:11 pm

    JRadioButtonExample is Observable, your Class is an Observer

    In the JRadioButtonExample you should keep a list of the Observer’s which you want to notify when this object’s state changes. You implement a method like below, notifyObservers() to notify all the registered observers.

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.beans.PropertyChangeListener;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Observer;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    
    public class JRadioButtonExample extends JFrame   {
    
        static JCheckBox b[];
        static Vector<Boolean> check = new Vector<Boolean>();
        JButton altervista = new JButton("RUN");
        JButton selectall = new JButton("select all");
        JButton deselectall = new JButton("deselect all");
        static int num;
    
        private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
    
        public static JCheckBox[] getB() {
            return b;
        }
    
        public void setB(JCheckBox[] b2) {
            b = b2;
        }
    
        public Vector<Boolean> getCheck() {
            return check;
        }
    
        public void setCheck(Vector<Boolean> check2) {
            check = check2;
        }
    
        public JRadioButtonExample(int num, ArrayList<String> lbl) {
    
            super("JRadioButtonExample");
    
            getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
    
            b = new JCheckBox[num];
    
            for (int i = 0; i < num; i++) {
                // creo i bottoni
                b[i] = new JCheckBox(lbl.get(i));
                getContentPane().add(b[i]);
            }
    
            // adding buttons
            getContentPane().add(selectall);
            getContentPane().add(deselectall);
            getContentPane().add(altervista);
    
            // adding listeners
            AscoltatoreSel asc1 = new AscoltatoreSel();
            selectall.addActionListener(asc1);
            setVisible(true);
    
            AscoltatoreDesel asc2 = new AscoltatoreDesel();
            deselectall.addActionListener(asc2);
            setVisible(true);
    
            Ascoltatore asc = new Ascoltatore();
            altervista.addActionListener(asc);
            setVisible(true);
    
            this.pack();
        }
        public void addPropertyChangeListener(PropertyChangeListener listener){
            this.listeners.add(listener);
        }
        public void notifyObservers(){
            for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
                PropertyChangeListener name = (PropertyChangeListener) iterator
                        .next();
                name.propertyChange(null);
    
            }
        }
    
        class Ascoltatore extends WindowAdapter implements ActionListener {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
    
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == altervista) {
                    setVisible(false);
                    boh(b);
                }
                notifyObservers();
            }
        }
    
        class AscoltatoreSel extends WindowAdapter implements ActionListener {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
    
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == selectall) {
                    for (int i = 0; i < num; i++) {
                        b[i].setSelected(true);
                        setVisible(true);
                    }
                }
                notifyObservers();
            }
        }
    
        class AscoltatoreDesel extends WindowAdapter implements ActionListener {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
    
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == deselectall) {
                    for (int i = 0; i < num; i++) {
                        b[i].setSelected(false);
                    }
                }
                notifyObservers();
            }
        }
    
        public static void boh(JCheckBox[] b) {
            JCheckBox[] buttons = getB();
    
            for (JCheckBox c : buttons) {
                check.add(c.isSelected());
            }
    
        }
    }
    

    Your Class should implement PropertyChangeListener, and and should register itself as al listener to the JRadioButtonExample.
    And implement propertyChange(..) method, that’s where you want to print().

    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.ArrayList;
    
    import java.util.Vector;
    
    public class Class implements PropertyChangeListener{
        private JRadioButtonExample b;
    
        public Class(JRadioButtonExample b){
            this.b = b;
            b.addPropertyChangeListener(this);
        }
    
        public static void main(String[] args){
            ArrayList<String> a = new ArrayList<String>();
            a.add("hi");
            a.add("all");
            JRadioButtonExample myButton = new JRadioButtonExample(2,a);
            Class myClass = new Class(myButton);         
        }
    
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Vector<Boolean> boo = b.getCheck();
             for(Boolean bool : boo){
                   System.out.println(bool);
             }      
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That show up on menu bar It's like this question: How to create a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That is, I'd like to have a tuple of values. The use case on
That's it. If you want to document a function or a class, you put
That was helpful kgiannakakis. I'm facing a problem as below: a = ['zbc','2.3'] for
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
That should be simple web site but with an editable tree. This tree will

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.