I’m building a JPanel that contains multiple fields, but according a product, the fields may vary.
There some common fields and some particular fields for each product.
Using some Interfaces, it’s possible to group the fields to build the referred JPanel with all needed fields, like this:
public class VPNProduct extends JPanel implements VPNFields{
//use and positioning of the fields
}
interface VPNFields extends CommonFields{
//particular VPN fields
}
interface CommonFields{
//fields common to all products
}
My question is, there’s some best practice or technique that make it easier or more organized, including the position of the fields in the panel?
Thanks in advance.
There are some elegant ways of doing this. First off an interface will have your get/set methods that will access your fields. So you will have an interface for your common fields and other ones for other fields. The common fields interface (since it will be shared) should be implemented to an abstract class. This abstract class will extend JPanel and construct your JLabel, JComboBoxes …etc and lay them out in a layout manager.
Then for any other class that you would need to add more fields, you would create a Class and have it extend your abstract class (so now it is a JPanel with all common fields already created and laid out) and implement an interface with your extra fields. These extra fields you will have to create their Swing components and add them to the layout manager that currently has the common fields.
If you provide some field examples or give us a hint at the structure I could draft some code sample so you can see.
A code example showing common fields that can be use for Company and School and how we create 2 JFrames with forms all originating from 1 JPanel abstract class:
–
–
–
–
–
–