Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Binding a JTable to Swing Controls in NetBeans IDE

DZone's Guide to

Binding a JTable to Swing Controls in NetBeans IDE

Here is an outline of a scenario that binds Swing controls to columns in a JTable, via the tools that NetBeans IDE provides.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Recently I blogged about the Beans Binding Framework and gave a few brief examples. Questions were asked about how those examples would be achieved in NetBeans IDE, via its tooling, instead of manual coding in the editor. Here is an outline of a scenario that binds Swing controls to columns in a JTable, via the tools that NetBeans IDE provides.

  1. Create the application. Create a new Java Application called "TableBindingDemo". Delete the "Main.java" class that is created for you.
  2. Create the JFrame. Create a JFrame called "CustomerFrame".
  3. Create the JTable. Drag a table from the palette onto the CustomerFrame.
  4. Start the server. Under the Tools menu, choose Java DB Database | Start Server.
  5. Bind the JTable to data. Right-click the table and choose "Table Contents". In the Customizer Dialog, click "Bound" in the "Table Model" tab. Click "Import Data to Form". In the "Import Data to Form" dialog, choose the last one of the three, "vir". Once the connection is made, choose the EMPLOYEE table. Click OK. Close the Customizer Dialog.
  6. Bind the columns to data. Right-click the table and choose Bind | elements. Click the arrow buttons to move the 'firstname' and 'lastname' from the 'Available' list to the 'Selected' list. Click OK.Let's now run the application! Right-click the project and choose Run Project. You should now see the JTable filled with data.
  7. Bind Swing controls to columns in the JTable. Drag and drop two JLabels and two JTextFields onto the JFrame. Remove the text in the JTextFields and change the text of the JLabels to "Name" and "Surname". Right-click the "Name" JTextField and choose Bind | text. In Binding Source, choose jTable1. in Binding Expression, choose selectedElement | firstName. Notice that the Binding Expression is now defined as ${selectedElement.firstname}. Click OK. Do the same for the other JTextField, so that the Binding Expression for the second JTextField is set as ${selectedElement.lastname}. Right-click the project and choose Run Project. You should now see that the JTable is synchronized with the JTextFields. When you change something in the JTextField, the JTable is updated and vice versa:
  8. Modify the update strategy. Not happy with the way the Swing controls are synchronized? Right-click the JTextField and choose Bind | elements again. Click Advanced. There you can set Update Properties to something different.

I hope this has answered some questions about the tooling that NetBeans IDE provides for the Beans Binding Framework. If not, feel free to leave questions here and I will try to answer them!

 

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:
netbeans

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}