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.


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.

  1. Bind DB table with JTable
    1. Add new JFrame file.
    2. Add new JTable component into JFrame component.
    3. Browse any suitable db with db tables in Service window.
    4. Drag table by mouse and drop it above JTable in designer.
    5. Build and run JFrame file.
    • EXPECTED RESULT: Table is filled with data from db table, columns has the same names like db table columns.
    1. Drag another table by mouse, hold CONTROL key, and drop it not above JTable but above JFrame component.
    2. 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.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.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.
  1. New persistence unit
    1. 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.
  2. New entity classes
    1. Create entity classses, each for example tables BOOK. New file -> Category: Persistence, File Type: Entity classes from Database -> Next.
    2. Select connection to DB and wait for list of tables. Add all tables into Selected list and press Next.
    • EXPECTED RESULT: "Added new persistence class Book.java into Source Package and into Persistence Unit XML into entity list."
  3. New persistence components, new tables and data binding
    1. Select MyFrame frame and add a few components from Palette from section Java Persistence ...
      • Add entity manager myEntityManager.
      • Add query bookQuery.
      • Add query result list bookList.
    2. Set property "entityManager" of bookQuery component to "myEntityManager" string.
    3. Set property "query" of bookQuery component to query string "SELECT b FROM Book b".
    4. Select bookList component and set query object bookQuery to its "query" property.
    5. Set coresponding value of property "Type Parameters" in property folder "Code" of bookList ...
      • bookList: Type Parameter is "<myapplication.Book>"
    6. Add "bookTable" into MyFrame frame.
    7. Select bookTable and invoke Binding dialog from popup menu (Bind -> elements).
    8. Select bookList as source from combo box and choose attributes you would like to in the table as table columns.
    • 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).
  1. Join jtable with database table using drag and drop
    1. In Runtime window open Database nodes and drag db table BOOK, hold CTRL key and drop it into frame
    2. Rename generated entity manager to "entityManager"
    3. Rename component "jTable1" to "bookTable"
    4. Add new jTextField component and rename it to "titleTextField"
    5. 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
  2. JPA Transaction stuff
    • Start db transaction using entityManager by adding code at the end of MyFrame contructor code ...
    • Add "Commit" button handling code (Events -> Action -> actionPerformed)
    • EXPECTED RESULT: Our application is ready to commit changes into database.
  3. Use beans binding
    1. Bind text of titleTextField component with selected cell in Title column in bookTable (Commit button -> Bind -> text)
    2. As binding source choose bookTable
    3. In binding expression treecombo doubleclick on selectedElement->title node, (It will generate expression string ${selectedElement.title})
    4. Go to Advanced tab and set ...
      • Write "bind" into name;
      • Update When : While typing
      • Check Unreadeble Path Value
    5. 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".
  4. Test application functionality
    1. Run the application, modify titles and exit without pressing Commit value.
    2. Run the application again, no changes were made inside db table, modify titles again, press Commit button, exit app.
    3. 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.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo