TS 61 FormSimpleJTableDataBinding
Simple JTable Data Binding
Author: Jiri Vagner
Version: 6.1
Last update: 2008/01/29
Introduction: Binding JTables with tables from DB using persistence.
Comments: This documents shows how to use persistence and data binding to bind JTables with DB tables in java application project.
WARNING: Functionality tested in this test suites will be available in final version of Netbeans 6.1.
Contents |
Test suite: Drag&Drop JTable DataBinding
Purpose: Bind JTable with DB table in java application project using just a mouse.
Project Setup: Start IDE ad create new Java Application project.
- Bind DB table with JTable
- Add new JFrame file.
- Add new JTable component into JFrame component.
- Browse any suitable db with db tables in Runtime window.
- Drag table by mouse and drop it above JTable in designer.
- Build and run JFrame file.
- EXPECTED RESULT: Table is filled with data from db table, columns has the same names like db table columns.
- Drag another table by mouse, hold CONTROL key, and drop it not above JTable but above JFrame component.
- Build and run JFrame file.
- EXPECTED RESULT: In executed form, there is second binded table.
Test suite: JTable Binding Using Wizards
Purpose: Bind JTables with DB tables in java application project using wizards and a little bit of coding.
Database Setup:
- Setup DB and create example tables, use attached book-and-car.sql.
- You can use Java DB contained in JDK 6, or in the App Server installation, or install your own (http://db.apache.org/derby/).
- Java DB install dir and database location can be configured in Tools | Options -> Advanced Options -> IDE Configuration -> Server and External Tool Settings -> Java DB Database
- Important: make sure the path to the install dir contains no spaces (e.g. avoid Program Files folder on win).
- Create a new database (for Java DB use Tools | Java DB Database | Create Java DB Database...).
- In the Runtime window in the IDE connect to the database (make sure connecting with non-empty user name).
- Invoke Execute Command on the Tables node, paste in the content of book-and-car.sql and execute it.
Project Setup:
- Start IDE ad create new project, Java Application (e.g. MyApplication) with container (e.g. JFrame named MyFrame) which is in package myaplication.
- New persistence unit
- Add new persistence unit XML file named myPU. New file -> Category: Persistence, File Type: Persistence Unit -> Next -> Unit Name: myPU, Table Generation Strategy: None and choose or create database connection -> Finish
- EXPECTED RESULT: Added persistence configuration XML file persistence.xml in META-INF folder in Source Package.
- New entity classes
- Create entity classses, each for example tables BOOK and CAR. New file -> Category: Persistence, File Type: Entity classes from Database -> Next.
- Select connection to DB and wait for list of tables. Add all tables into Selected list and press Next.
- EXPECTED RESULT: "Added new persistence classes Car.java and Book.java into Source Package and into Persistance Unit XML into entity list."
- New persistence components, new tables and data binding
- Select MyFrame frame and add a few components from Palette from section Java Persistence ...
- Add entity manager myEntityManager.
- Add query carQuery.
- Add query bookQuery.
- Add query result list carList.
- Add query result list bookList.
- Set property "entityManager" of carQuery and bookQuery component to "myEntityManager" string.
- Set property "query" of bookQuery component to query string "SELECT b FROM Book b".
- Set property "query" of carQuery component to query string "SELECT c FROM Car c".
- Select bookList component and set query object bookQuery to its "query" property.
- Select carList component and set query object carQuery to its "query" property.
- Set coresponding value of property "Type Parameters" in property folder "Code" of carList and bookList ...
- carList: Type Parameter is "<myapplication.Car>"
- bookList: Type Parameter is "<myapplication.Book>"
- Add "bookTable" and "carTable" into MyFrame frame.
- Select bookTable and invoke Binding dialog from popup menu (Bind -> elements).
- Select bookList as source from combo box and choose attributes you would like to in the table as table columns.
- Do the same steps for binding carTable.
- EXPECTED RESULT: After running MyFrame.java file, tables are filled with data from database.
Test suite: Tiny DB Editor
Purpose: Bind JTables with DB tables in java application project and bind textfield for editing with table column.
Database Setup:
- Use database setup from previous suite.
Project Setup:
- Start IDE and create new project, Java Application with container (JFrame named MyFrame).
- Join jtable with database table using drag and drop
- In Runtime window open Database nodes and drag db table BOOK, hold CTRL key and drop it into frame
- Rename generated entity manager to "entityManager"
- Rename component "jTable1" to "bookTable"
- Add new jTextField component and rename it to "titleTextField"
- Add new jButton component and rename it to "commitButton"
- EXPECTED RESULT: Table columns names coresponds to db columns names, "Other components" section in Inspector contains persistance unit component, query component and query result list component
- JPA Transaction stuff
- Start db transaction using entityManager by adding code at the end of MyFrame contructor code ...
- Start db transaction using entityManager by adding code at the end of MyFrame contructor code ...
entityManager.getTransaction().begin();
- Add "Commit" button handling code (Events -> Action -> actionPerformed)
entityManager.getTransaction().commit(); entityManager.getTransaction().begin(); bindingContext.clearHasEditedTargetValues();
- EXPECTED RESULT: Our application is ready to commit changes into database.
- Use beans binding
- Bind text of priceTextField component with selected cell in Price column in bookTable (Commit button -> Bind -> text)
- As binding source choose bookTable
- In binding expression treecombo doubleclick on selectedElement->title node, (It will generate expression string ${selectedElement.title})
- Go to Advanced tab and set ...
- Update When : While typing
- Check Incomplete Path Value
- Press "..." button for Incomplete Path Value property and insert text "Select book"
- EXPECTED RESULT: Textfield is binded to selected cell in Title column and contains text "Select book".
- Test application functionality
- Run the application, modify titles and exit without pressing Commit value.
- Run the application again, no changes were made inside db table, modify titles again, press Commit button, exit app.
- Changes were commited, content of db table was changed. Run the application again and check edited titles again.
- EXPECTED RESULT: Application loads data from db and is able to edit book titles.

