Okay! Let me begin the post. I have come across this scenario while I was working on a Java Swing application project at my office.
The problem: How to populate JTable with list of objects.
Typical and painful solution: Loop through the list and populate data column by column.
But it's a real pain when it comes to retrieve data as objects again from the table. We have to extract each property from each column in the table and create a new object with that data.
An easy and less complex/code solution: Bind beans to the table as rows.
If we can bind objects as it is to the table then its really easy to retrieve them as well. No need to iterate through columns anymore. I found this solution from this blog and total credit goes to the author.
This is how it works. There are two ways to bind beans to JTable.
- Bean Table Model
- Row Table Model
Following steps explain how to do it.
Step 1: Create a bean class
Step 2: Create the bean table model
Step 3: Bind the table model with JTable
Step 4: Retrieve data from the table
Screenshot of the demo application: Yep. That's all. Its simple as that. Okay, time to go to bed :) I'll see you in another post. Triple gem bless you!
Git repository URL: JTableBeanDemo
P.S: You can also get the original source files from the blog link i have mentioned in the post.
No comments:
Post a Comment