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.
I’d propose to use enum for options:
To get all options you can use
FilePackClassOption.values()asoptionsparameter inJOptionPane. And when you add new subclass ofFilePackyour only need to add new value to enum and implement 2 methods.Example of use: