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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:14:11+00:00 2026-06-05T12:14:11+00:00

I’m building a JPanel that contains multiple fields, but according a product, the fields

  • 0

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.

  • 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-05T12:14:13+00:00Added an answer on June 5, 2026 at 12:14 pm

    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:

    package stackoverflow.test;
    
    public interface CommonFields {
    
        public void setName(String name);
    
        public void setLastName(String lastName);
    
        public void setAge(int age);
    
        public String getName();
    
        public String getLastName();
    
        public int getAge();
    }
    

    –

    package stackoverflow.test;
    
    public interface SchoolFields {
    
        public void setSchoolName(String schoolName);
    
        public void setGrade(int grade);
    
        public void setHonorsProgram(boolean isHonors);
    
        public String getSchoolName();
    
        public int getGrade();
    
        public boolean hasHonorsProgram();
    }
    

    –

    package stackoverflow.test;
    
    public interface CompanyFields {
    
        public void setCompanyName(String companyName);
    
        public void setJobTitle(String jobTitle);
    
        public void setAddress(String address);
    
        public String getCompanyName();
    
        public String getJobTitle();
    
        public String getAddress();
    }
    

    –

    package stackoverflow.test;
    
    import java.awt.GridLayout;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class AbstractJPanel extends JPanel implements CommonFields {
    
        private static final long serialVersionUID = 150030761086805569L;
    
        private JTextField nameField = null;
        private JTextField lastNameField = null;
        private JTextField ageField = null;
        private JLabel nameLabel = null;
        private JLabel lastNameLabel = null;
        private JLabel ageLabel = null;
    
    
        public AbstractJPanel() {
            super(new GridLayout(0, 2));
    
            nameField = new JTextField();
            lastNameField = new JTextField();
            ageField = new JTextField();
    
            nameLabel = new JLabel("Name: ");
            lastNameLabel =new JLabel("Last Name: ");
            ageLabel = new JLabel("Age: ");
    
            add(nameLabel);
            add(nameField);
            add(lastNameLabel);
            add(lastNameField);
            add(ageLabel);
            add(ageField);
        }
    
        public void setName(String name) {
            nameField.setText(name);
        }
    
        public void setLastName(String lastName) {
            lastNameField.setText(lastName);
        }
    
        public void setAge(int age) {
            ageField.setText(""+age);
        }
    
        public String getName() {
            return nameField.getText();
        }
    
        public String getLastName() {
            return lastNameField.getText();
        }
    
        public int getAge() {
            return Integer.parseInt(ageField.getText());
        }
    }
    

    –

    package stackoverflow.test;
    
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class SchoolPanel extends AbstractJPanel implements SchoolFields {
    
        private static final long serialVersionUID = -6201476099194804075L;
    
        private JTextField schoolNameField = null;
        private JTextField gradeField = null;
        private JCheckBox honorsCheckBox = null;
        private JLabel schoolNameLabel = null;
        private JLabel gradeLabel = null;
        private JLabel honorsLabel = null;
    
        public SchoolPanel() {
            super();
    
    
            schoolNameLabel = new JLabel("School Name: ");
            gradeLabel = new JLabel("Grade: ");
            honorsLabel = new JLabel("Is Honors: ");
    
            schoolNameField = new JTextField();
            gradeField = new JTextField();
            honorsCheckBox = new JCheckBox();
    
            add(schoolNameLabel);
            add(schoolNameField);
            add(gradeLabel);
            add(gradeField);
            add(honorsLabel);
            add(honorsCheckBox);
        }
    
        @Override
        public void setSchoolName(String schoolName) {
            schoolNameField.setText(schoolName);
        }
    
        @Override
        public void setGrade(int grade) {
            gradeField.setText(""+grade);
        }
    
        @Override
        public void setHonorsProgram(boolean isHonors) {
            honorsCheckBox.setSelected(isHonors);
        }
    
        @Override
        public String getSchoolName() {
            return schoolNameField.getText();
        }
    
        @Override
        public int getGrade() {
            return Integer.parseInt(gradeField.getText());
        }
    
        @Override
        public boolean hasHonorsProgram() {
            return honorsCheckBox.isSelected();
        }
    }
    

    –

    package stackoverflow.test;
    
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class CompanyPanel extends AbstractJPanel implements CompanyFields {
    
        private static final long serialVersionUID = 7834845724312492112L;
    
        private JTextField companyNameField = null;
        private JTextField jobTitleField = null;
        private JTextField addressField = null;
        private JLabel companyNameLabel = null;
        private JLabel jobTitleLabel = null;
        private JLabel addressLabel = null;
    
        public CompanyPanel() {
            super();
    
            companyNameLabel = new JLabel("Company Name: ");
            jobTitleLabel = new JLabel("Job Title: ");
            addressLabel = new JLabel("Address: ");
    
            companyNameField = new JTextField();
            jobTitleField = new JTextField();
            addressField = new JTextField();
    
            super.add(companyNameLabel);
            super.add(companyNameField);
            super.add(jobTitleLabel);
            super.add(jobTitleField);
            super.add(addressLabel);
            super.add(addressField);
        }
    
        @Override
        public void setCompanyName(String companyName) {
            companyNameField.setText(companyName);
        }
    
        @Override
        public void setJobTitle(String jobTitle) {
            jobTitleField.setText(jobTitle);
        }
    
        @Override
        public void setAddress(String address) {
            addressField.setText(address);
        }
    
        @Override
        public String getCompanyName() {
            return companyNameField.getText();
        }
    
        @Override
        public String getJobTitle() {
            return jobTitleField.getText();
        }
    
        @Override
        public String getAddress() {
            return addressField.getText();
        }
    
    }
    

    –

    package stackoverflow.test;
    
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    
    public class MainClass {
    
        public MainClass() {
            JFrame frame1 = new JFrame();
            frame1.setLayout(new BorderLayout());
            frame1.add(new CompanyPanel());
            frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame1.setSize(500, 500);
            frame1.setVisible(true);
    
    
            JFrame frame2 = new JFrame();
            frame2.setLayout(new BorderLayout());
            frame2.add(new SchoolPanel());
            frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame2.setSize(500, 500);
            frame2.setVisible(true);
        }
    
    
        public static final void main(String ... args) {
            new MainClass();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.