Search |
||
Passing Dialog Input to the Main View and DatabasePosted by pkeegan on May 27, 2008 at 8:33 PM PDT
This is the fourth in a series of posts on creating a Java database application. In my last few posts, I started with skeleton code generated by the IDE and provided my own customizations, including adding a dialog to use for data entry and binding those fields with a table on the main form. In this post, I finish coding the connection between the dialog and the main form. I'll also add an Edit Client button and its corresponding Action code to the main form. First let's hook up the buttons in the EditClient dialog with appropriate event-handling code. We already have save() and refresh() actions that are provided with the skeleton application. We will code the dialog so that the buttons reuse these actions. We can accomplish this by setting up a boolean property in the dialog that returns true when the Save Record button is pushed and returns false when Cancel is selected. Based on the value that is returned when the dialog is closed, the the To set up the property, do the following:
We'll set this property's value in event handling code for the buttons. Let's create the event listeners and handlers now:
Navigate to the
if (ec.isClientConfirmed()) {
save().run();
} else {
refresh().run();
}
In the RefreshTask inner class, Since the To make the dialog modal:
To make the main form's Clients table uneditable:
You can now run the application and click New Client to add a new record. When you press Save in the New Client dialog, the record is saved. When you press Cancel, the new record you have changed is rolled back. This is all well and good, but by disabling the editability of the table on the main form, we can no longer edit existing records. To solve this, we'll add an Edit button to the main client form so that we can edit existing records. For event-handling, we'll take advantage of the Swing Application Framework's Action facility. To add the button and its corresponding event-handling code, do the following:
Most of that code is copied straight from the newRecord action. The key difference is the line
The Client part of the application is almost completely set. You should be able to freely add, edit, and delete records from your CLIENTS table using the specialized GUI we have created. One last detail: the main form still has the title of Database Application Example, and it's not obvious where to change. Hint: it's not within the GUI Builder. To change the title of the main frame of the application:
Now when you run the application, most of the key elements are in place. In the screenshot below, you can see the Edit Client dialog as it appears after having selected a record and pressed the Edit button.
I could continue with customization of the bottom part of the main form and other fine tuning of the application, but I'll save most of those details for the tutorial and individual blog posts with more atomic examples. As always, keep your questions coming and I'll try to deal with as many of them as I can. »
Related Topics >>
Netbeans Comments
Comments are listed in date ascending order (oldest first)
Submitted by pkeegan on Wed, 2008-06-04 04:07.
Hi Dalibor. You can specify a validator in the Advanced tab of the binding dialog. The validator class must implement the validate method of org.jdesktop.beansbinding.Validator. More on that can be found in the Javadoc (from the IDE, choose Help -> Javadoc References -> Beans Binding).
Once you write the validator class, compile it, and then drag it onto the form (actually drag it to the white area around the form) to add it to the form as a bean. It will then become available to you in the Bind dialog to add as a validator.
The Client Editor example you mention is a great place to look to see how validators and converters work. The AgeValidator class handles the validation.
Also, I wrote about converters and validators here: http://weblogs.java.net/blog/pkeegan/archive/2007/11/beans_binding_c.html
and here: http://www.netbeans.org/kb/60/java/gui-binding.html
Submitted by dadox on Tue, 2008-06-03 14:36.
Hello Patrick,
Is the validator of a binding object notified the first time the binding is created? If not, is there a way to explicitly validate it?
I would like to bind a JTextField to a field in a POJO, and when the field is empty it should become colored (to mark that is invalid). Currently, it becomes colored only after I type something in it and cancel it with backspace. It would be nice to have it colored immediately after it gets initialized.
I looked at the example in NetBeans (ClientEditor) and I noticed that if one sets the person's age to 300 (an invalid value) after the initComponents() method, it will not be marked as invalid. Why is that?
Thank you for your time, I really don't know where to ask :) dalibor
Submitted by dadox on Wed, 2008-06-04 06:51.
Hi Patrick, thank you so much for answering. I already read your other texts before deciding to write you and thanks to them I learned how to quickly bind objects and use validators. It really is a fantastic feature !
To explain my question let's look at the ClientEditor example: if we put this line immediately after initComponents() is called : " client.setAge(300); " it will alter the age of the client object, and it will be synced with the gui... BUT it will NOT get validated the first time ! the valid age is between 1..200, 300 is obviously an invalid value. If one then manually deletes the 300 from the gui, only after that the validator seems to function. My question was if there is a way to trigger validation immediately, or to give an explicit order to validate ? Thank you again, keep up the great work ! dalibor
Submitted by jj2006h on Thu, 2009-02-19 23:19.
hi
i want to use the detail view only without using jtable to bind my object
i want directly bind the domain objects properties to the gui form
and of course i want to add create new object too....
netbeans uses following form of binding list > jtable > seledcted element > domain object
how to acheive this object > gui fiels
please help me on this
|
||
|