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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:47:32+00:00 2026-06-08T00:47:32+00:00

I need to create an application where I need to get a user input

  • 0

I need to create an application where I need to get a user input from a radio button and then use the selected filename in a different class.I’m not sure how to implement this, beacuse everytime I try to place a getString() method in the MyAction class it gives me a null value. thanks!!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;

public class SelectRadioButton{

    public SelectRadioButton(){

      // Directory path here
      String path = "W:\\materials"; 

      JFrame frame = new JFrame("Material Selection");
      JPanel panel = new JPanel(new GridLayout(0, 4));
      ButtonGroup bg = new ButtonGroup();

      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
      JRadioButton  first;

      for (int i = 0; i < listOfFiles.length; i++) 
      {

       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
           if (files.endsWith(".mtl") || files.endsWith(".MTL"))
           {

              first = new JRadioButton(files);
              panel.add(first,BorderLayout.CENTER);
              panel.revalidate(); 

              bg.add(first);
              first.addActionListener(new MyAction);
            }
         }
      }

       frame.add(panel, BorderLayout.NORTH);
       frame.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
       frame.setSize(1000, 400);
       frame.setVisible(true);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

  public class MyAction implements ActionListener{
        //String m;
      public void actionPerformed(ActionEvent e){
      String m =e.getActionCommand();
      String[] split = m.split("\\.");
      m=split[0];
      JOptionPane.showMessageDialog(null,"Your Selection is"+m+" radio button.");
   }
        /*
        public String getString(){
               return m;
        }
        */
   }
 }
  • 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-08T00:47:34+00:00Added an answer on June 8, 2026 at 12:47 am

    Obviously, the m variable only will be set when the specific radio button receive a click event.
    If you don’t want to change your code so much, do something like this:

    public class MyAction implements ActionListener{
        String m;
    
        public MyAction(String radioButtonLabel){
            m = radioButtonLabel;
        }
        public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null,
                    "Your Selection is"+m+" radio button.");
        }
        public String getString(){
            return m;
        }
    }
    

    And replace the:

    first.addActionListener(new MyAction());
    

    by:

    first.addActionListener(new MyAction(files));
    

    And improve the names of your variables… it is a little bit confusing!
    Hope it helps.

    UPDATE

    To get the selected radio button:

    public static JRadioButton getSelection(ButtonGroup group) {
        for (Enumeration e = group.getElements(); e.hasMoreElements();) {
            JRadioButton b = (JRadioButton) e.nextElement();
            if (b.getModel() == group.getSelection()) {
                return b;
            }
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a application which is similar to those we will get
I have to create a test application on JMeter where i need to get
I want to create gallery application which would get thumbnails and metadata from web
I need to create application logs to capture users signing in/out and their requests,
I need to create an application capable to modify and manage files on IOS.
So, just like the title says, I need to create an application that gets
I need to create a simple application that would generate reports based on database
I need to create a voip application ( like msn ). What do I
I need to create a desktop application that will run third party code, and
I need to create a web application using php, I had many users having

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.