Such a simple topic but sadly google does not throw up good results on how to create a simple form in Java Swing. This definition is not needed but by form, I mean, a simple data entry utility like one where a user can add a contact to an address book application. This post assumes that you prefer coding out the form manually instead of using an IDE. This is essential when you want more control over the form code. Of course it is more complex and if you have an option always is an IDE like Netbeans, go for it. Manual coding, for example could be useful when coding in an existing application.There are about 3 ways that I researched on but tried just the first one:
1. Using A SpringLayout and SpringUtilities
I am not going to explain what a Spring Layout. My aim is just to provide a Java Developer for links to investigate and hopefully to save their time.
This is what a Spring Layout is: http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html
A Very Useful Utility:
http://java.sun.com/docs/books/tutorial/uiswing/examples/layout/SpringGridProject/src/layout/SpringUtilities.java
Here is a quick example, of course taken from Sun's javadoc. This creates a simple form with two columns (the left column contains labels such as name and the right column contains a text field to enter data):
//these are your labels in the form
String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
int numPairs = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < l =" new" textfield =" new">
where rows is number of items in the form, cols is the number of columns (here it is two one for label and other for textfield), initX is the initial X position from where you can start the form and initY is the initial Y position from where you can start the form. xPad is the amount of space between each row and ypad is the amount of space between each column.
There are other complex Layout such as GridBag Layout and GroupLayout which are not good for manual coding.
These layouts are used mainly in code that is auto generated by IDEs
2. If you have been a web developer like me and like to make use of CSS like styling properties to make UIs (as in Google Android, Flex), then this one is for you: http://www.miglayout.com/
3. Similarly, there is one called JGoodies Forms which seems to be very popular. http://www.jgoodies.com/freeware/forms/index.html. JGoodies has a wonderful demo where you can manually play around with form elements and view their properties.
2 and 3 are open source and free to use.
4. If you wish to use your own form layout utility, you can begin from here: http://www.javaworld.com/javaworld/jw-10-2002/jw-1004-dialog.html?page=2
Saturday, September 27, 2008
Sunday, September 21, 2008
My Tryst with Date/Calendar Pickers for Java Swing
For one of my recent projects, I had to work on Java Swing and check out some date/calendar pickers for Java Swing. My expectations were not stringnet. I could use any Date Picker as long it was free and open source :). Customization was not important either. So I narrowed down on the following three simple Date Picker which are available as Swing components:
1. JCalendar: https://jcalendar.dev.java.net/ - Seems to have a lot of options to customize
2. JxDatePicker - http://wiki.java.net/bin/view/Javadesktop/SwingXCalendar. It is part of a much larger Swing component Library called SwingX.
3. Finally Settled on Microba Date Picker - http://microba.sourceforge.net/
There are many more options. The free ones are fairly useful and each of them have many customization options such as timezone, setting the date at instantiation time, look and feel, number of years a user is allowed to navigate to, etc that must be looked into and exploited
1. JCalendar: https://jcalendar.dev.java.net/ - Seems to have a lot of options to customize
2. JxDatePicker - http://wiki.java.net/bin/view/Javadesktop/SwingXCalendar. It is part of a much larger Swing component Library called SwingX.
3. Finally Settled on Microba Date Picker - http://microba.sourceforge.net/
There are many more options. The free ones are fairly useful and each of them have many customization options such as timezone, setting the date at instantiation time, look and feel, number of years a user is allowed to navigate to, etc that must be looked into and exploited
Subscribe to:
Posts (Atom)