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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:56:24+00:00 2026-05-28T05:56:24+00:00

I am making an application where the user is prompt at some point to

  • 0

I am making an application where the user is prompt at some point to create a new file (object not file in OS). That file represents one of several subclasses of an abstract class named FilePack.
The user will choose the type of the file from a list (in a JOptionPane) then that option is forwarded to a method which will create the appropriate subclass.

An example:
User clicks on “create new file” button. JOptionPane comes with a list of available FilePack subclasses. User selects “Type A”. Selection forwarded to method which creates the appropriate subclass.

Which would be the optimal way of handling this operation which will allow the code to work when introducing new FilePack subclasses without having to go everywhere and changing bits of code?

(at this point you have to change the list in JOptionPane and the method creating the subclasses, but its possible changes will need to happen on more places later in development)

Also, is it possible to get two inputs from a JOptionPane? One using textfield and one from list ?Im only able to do one at time.

  • 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-28T05:56:25+00:00Added an answer on May 28, 2026 at 5:56 am

    I’d propose to use enum for options:

    enum FilePackSubclassOption {
    
        TYPE_A() {
            @Override
            public FilePack createFilePack() {
                return new FilePackTypeA();
            }
    
            @Override
            public String getName() {
                return "Type A";
            }
        };
    
        public abstract FilePack createFilePack();
        public abstract String getName();
    
        @Override
        public String toString() {
            return getName();
        }
    }
    

    To get all options you can use FilePackClassOption.values() as options parameter in JOptionPane. And when you add new subclass of FilePack your only need to add new value to enum and implement 2 methods.

    Example of use:

    public void chooseFilePack() {
        Object selection = JOptionPange.showInputDialog(null, 
                               "Select file pack type", "File pack", 0, null, 
                               FilePackSubclassOption.values(), null);
        if (selection != null) {
            return ((FilePackSubclassOption)selection).createFilePack();
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a php web application which stores user specific information that is not
I am making a application that the user will have to interact with one
I am making an application where at some point i need the user to
I am making an application that persists several different user settings. The way I
I'm making a small tool application that user can minimize in the taskbar .
I am making an application that does some custom image processing. The program will
I'm making an application that updates a user's location and path in real time
I am making an application using jQuery. In my app, the user enters some
I am making an application in which I am taking some information from user
I am making an application that creates and allows the user to edit an

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.