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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:57:26+00:00 2026-05-31T15:57:26+00:00

I am having problem to return a simple int value out of radiobutton i

  • 0

I am having problem to return a simple int value out of radiobutton i have created.

        static int f5(String question) 
        {
            int intValue = 0;
            answered = false;
            JFrame f = new JFrame(question);
            f.setSize(300, 750);

            f.addWindowListener(new WindowAdapter() 
            {
              public void windowClosing(WindowEvent we) 
              { 
                  System.exit(0); 
              }
            });

            JPanel p = new JPanel(new GridLayout(0,1,3,3));
            final ButtonGroup bg = new ButtonGroup();
            JRadioButton radioButton;

            p.add(radioButton = new JRadioButton("q1"));
            radioButton.setActionCommand("1");
            bg.add(radioButton);
            p.add(radioButton = new JRadioButton("q2"));
            radioButton.setActionCommand("2");
            bg.add(radioButton);
            p.add(radioButton = new JRadioButton("q3"));
            radioButton.setActionCommand("3");
            bg.add(radioButton);
            p.add(radioButton = new JRadioButton("q4"));
            radioButton.setActionCommand("4");
            bg.add(radioButton);

            JButton orderButton = new JButton("Submit");
            p.add(orderButton);

            f.getContentPane().setLayout(new FlowLayout());
            f.getContentPane().add(p);
            f.pack();


            orderButton.addActionListener(new ActionListener() 
            {
              public void actionPerformed(ActionEvent ae) 
              {

                String ans = bg.getSelection().getActionCommand();
                boolean sel;
                f5Answer = Integer.parseInt(ans);
                answered = true;
                System.out.println("in void: " + f5Answer); //how to return the f5Answer?

              }

            });
            f.setVisible(true);


            f5Answer = intValue;
            System.out.println("out of void: " + f5Answer);//this only returns 0 right away



            return intValue;
        }

I would like to return the value and end function after I find value when i press submit. Right now, the value is returned to 0 right after I use the function in main. How can I make the function to only return the value after submit is pressed??

Thanks in advance!

  • 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-31T15:57:27+00:00Added an answer on May 31, 2026 at 3:57 pm

    I think I sort of understand your problem. What you should do is make your ButtonGroup object a private class field, and then give your class a public method, say something like …

    public int getSelection() {
       ButtonModel btnModel = bg.getSelection();
       if (bg == null) {
         // TODO: throw an exception -- no radio button has been selected
       } else {
          return Integer.parseInt(btnModel.getActionCommand());
       }
    }
    

    Then in your submit button’s actionlistener, notify any and all parties that the selection has been made, and that they should call the getSelection() method on this object to find out what the selection is.

    Edit
    Ah, now I see what you’re trying to do — you’re trying to create a question dialog. My suggestion is that you in fact do just this, create a dialog not a JFrame, and in fact make it a modal dialog so that your method will wait for the user to respond before moving on, and so that when the method ends, the ButtonGroup will in fact hold the correct information. The easiest way to do this is to use a JOptionPane which are very flexible critters, and use one that holds a JPanel that holds a grid of your JRadioButtons. Then find out what the user selected after the JOptionPane returns. Here, a static method could work fine since you’re not changing the state of an object. For example:

       public static String myQuestion(String question, String[] answers) {
          JPanel panel = new JPanel(new GridLayout(0, 1));
          final ButtonGroup btnGroup = new ButtonGroup();
    
          for (String answer : answers) {
             JRadioButton radioBtn = new JRadioButton(answer);
             radioBtn.setActionCommand(answer);
             btnGroup.add(radioBtn);
             panel.add(radioBtn);
          }
    
          String[] options = { "Submit", "Cancel" };
          String initialValue = "Submit";
    
          int optionResult = JOptionPane.showOptionDialog(null, panel, question,
                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
                options, initialValue);
    
          // *** code will pause here and wait for the user to handle the modal dialog
    
          if (optionResult != 0) {
             return ""; // did not select "Submit"
          } else if (optionResult == 0) {
             ButtonModel btnModel = btnGroup.getSelection();
             if (btnModel == null) {
                return ""; // error! nothing selected
             } else {
                return btnModel.getActionCommand();
             }
          }
          return "";
       }
    
       public static void main(String[] args) {
          String question = "Where is Ulysses S. Grant buried?";
          String[] answers = { "In my back yard", "At the white house",
                "red my lord! No, blue!", "In Grant's tomb", "Who the hell is Ulysses S. Grant?" };
          String selectedString = myQuestion(question, answers);
          System.out.println("Answer selected: " + selectedString);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a simple guessing game. I'm having a weird problem: -(int)setRandom
I'm having a weird problem with the following function, which returns a string with
Im having a very strange problem, i have a complicated view that returns incorrect
I'm having problem when running my Windows Forms program. In the program, I have
I'm having a problem with one of my functions, I'm working on a simple
I am having a strange problem with MySQL Stored Procedure. I have written a
Basically, I have an int value, x, that is an instance variable of a
I have a pretty simple problem with a not-so-obvious solution. I have a relational
I have a very simple class: People: class People { private string LastName =
I'm having some trouble with a linq query. It's a simple problem, but I'm

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.