Platinum Partner
netbeans

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.

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!

 

{{ tag }}, {{tag}},

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

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}