简体   繁体   中英

Creating a Java GUI in Swing for form input

Well, I've looked all over the internet and just haven't been able to find an answer to this question, so maybe someone can provide some insight.

I'm working on developing a relatively simple Java app that will replace a Word doc currently used for system access requests. It's designed to allow form entry of new employee hire information - name, access needed, and so forth.

So here's my problem. Trying to make a GUI with all of the text fields and everything is surprisingly painful. Because each widget is a bit different, getting the input once the form is filled out seems to require a separate reference for each widget so I can call them individually. This means each panel for a section has 6-10 different private fields. (I attempted adding all the similar widgets to a list and calling them in a loop but that didn't seem to work.)

It seems that a web form would be a better fit for this in some ways but I don't have the infrastructure available to do that. Has anyone out there found a better solution than this for something similar? It just seems like a ton of code. Please see below for an idea (I put in some comments rather than actual code because it is so long). Thanks for looking!

    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;
    }
}

Vanilla Swing is verbose - no argument there. Basically for this kind of thing I end up relying on a bunch of home-grown util/factory methods like createTextBox() that is parameterized for common usage and will execute the 3-8 statements necessary.

I will sometimes even parameterize the layout. I tend to reduce all layout to GridBagLayout (others do BorderLayout, etc; it's just a matter of personal preference) and then create methods that'll put a control at a particular point in the grid.

If you're not going Web-based, I would just stick with Swing. It might be verbose but it's not really all that difficult for a simple form.

Call me crazy but I dislike gui builders. I prefer the extra control that hand-coding brings. Also, as you wrote the code yourself it makes it far easier to debug. What is important is to layout your code in a logical order and to use methodical variable names.

You mentioned trying to use a list. However, I don't see how this is going to save you much time at all (or as I hinted eariler, what time you'll save on implementation, you'll lose during the debugging process). I'm guessing that the headaches are starting when you start sanity-checking the input of your JTextFields. I'd suggest subclassing JTextField into MoneyTextField, PhoneNumberTextField, etc. which can be used to setup the ActionListener to stop people doing stupid things.

您可以查看我尝试解决此问题的方法: http//code.google.com/p/swing-formbuilder/

Netbeans has a gui builder that is reasonably good by most accounts. This screen cast shows off the data binding feature that may be helpful for you.

Another option may be to roll your own presentation framework which, depending on how complex you want it, is not that hard and sorta fun. Just use reflection to pull fields out of an object and translate those fields into widgets and back again. Make sure to use a layout manager to do most of the heavy lifting for you.

Yes, on the whole, gui stuff is not java's strong suit, but you should find it's good enough.

if you are adventurous, you can try doing it in groovy, using griffon. Griffon is a GUI building framework that makes gui building easier by automating the repetitive parts. See http://griffon.codehaus.org/ for more details.

If you know how to use grails, griffon will feel similar, as it is built using similar concepts and metaphors.

Take a look at BetterBeansBinding . It's a tool which makes it easy to bind various JavaBean like properties between objects. You would set up an object which holds a request, and bind the properties of that object to the various gui components. When you're done, just return the bound object and it should have all the fields filled out.

Note that I haven't actually used this before, but I've seen the concept and have done similar things before it was released.

I would create a this form from a 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 then generate the java code for a Pojo and for the 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...)
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM