tags:

views:

1827

answers:

8

Well, I've looked all over the internet (including good ol' StackOverflow) 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;
    }
}
+1  A: 

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.

cletus
+1  A: 

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.

CurtainDog
A: 

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.

Chii
A: 

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.

deterb
A: 

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...)
   }
 }
Pierre
A: 

If you like XML, take a look to SWT/XML or jelly.

Oh, sorry, I have just read the question is about Swing.

Banengusk
+1  A: 

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.

Catchwa
A: 

You may look at my attempt to solve this problem: http://code.google.com/p/swing-formbuilder/

eav