Creating a Java GUI in Swing for form input

Well, I looked all over the Internet and just couldn't find the answer to this question, so maybe someone can give some insight.

I am working on a relatively simple Java application that will replace the Word document currently used for system access requests. It is designed to enter a form of new information about hiring an employee - name, necessary access, etc.

So here is my problem. Trying to make a GUI with all text fields and everything is surprisingly painful. Since each widget is slightly different, getting input after filling out the form seems to require a separate link for each widget, so I can name them individually. This means that each panel for a section has 6-10 different private fields. (I tried adding all the similar widgets to the list and calling them in a loop, but this does not seem to work.)

It looks like the web form is somewhat better suited for this, but I don't have the infrastructure available for this. Has anyone out there found a better solution than this for something like that? This is just a ton of code. Please see the idea below (I added some comments, not the actual code, because it took so long). Thanks for watching!

    private JComboBox my_dates;
    private JTextField my_date1;
    private JTextField my_date2;
    private JTextField my_request_date;
    private JTextField my_new_legal_name;
    private JTextField my_new_pref_name;
    private JTextField my_new_username;
    private JTextField my_prev_legal_name;
    private JTextField my_prev_pref_name;
    private JTextField my_prev_username;
    private JTextField my_emp_id;
    private JTextField my_manager;
    private JTextField my_auth_requestor;
    private JTextField my_auth_phone;

    public NameChangePanel(FormSection the_section)
    {
        super();
        initialize();
        buildPanel(the_section.getFields());
    }

    private void initialize()
    {
        // Create all the widgets individuall
    }

    private void buildPanel(List the_fields)
    {
        // add a field label
        // add a component
        // repeat for all values
    }

    public List getFormValues()
    {
        // Call all of the private fields individually
        return values;
    }
}
+3
7

- - . util/ factory, createTextBox(), 3-8 .

. GridBagLayout ( BorderLayout .., ), , .

, Swing. , , .

+5

, gui. , . , , . , .

. , , (, , , ). , , , JTextFields. JTextField MoneyTextField, PhoneNumberTextField .., ActionListener, .

+3

Netbeans gui, . , .

, , , , . , . , .

, , gui- java, .

+1

, groovy, . Griffon - GUI, gui . . http://griffon.codehaus.org/.

, grails, , .

+1

BetterBeansBinding. , JavaBean . , , gui. , , .

, , , .

0
source

I would create this form from an XML file, something like

<form name="MyForm" package="com.nowhere.swing">
 <field component="javax.swing.JTextField" label="Enter your Name" pattern="[a-z]+" name="name"/>
 <field component="javax.swing.JTextArea" label="Enter your Comment" pattern="[a-z]+" name="comment"/>
</form>

and then using XSLT , I would generate Java code for Pojo and for Swing interface using this definition.

class MyFormPojo
 {
 private String name;
 private String comment;
 (...)
 } 

class MyFormPane extends JPanel
 {
 JTextField field4name;
  JTextArea field4comment;
 (...)
 MyFormPane()
   {
   (...)
   field4name= new JTextField();
   (.. create labels, etc...)
   }
 }
0
source

Source: https://habr.com/ru/post/1707667/


All Articles