Structure of a correctly implemented JTable with TableModel and Listeners?

Posted by bamboocha on Programmers See other posts from Programmers or by bamboocha
Published on 2012-11-11T21:22:48Z Indexed on 2012/11/11 23:12 UTC
Read the original article Hit count: 281

Filed under:
|
|

I am pretty new to Java and its JTables and this is where I am struggling at the moment.

I need to create a GUI which shows me results of a sql query like SELECT * FROM tblPeople WHERE name='Doe'. My idea was to create a a JFrame which displays a JTable with all found records.

Besides this, I need to also implement some code to handle when a user is double clicking a record or selecting it by using his arrow keys (additional feature: pressing 12(e.g.) should select the 12th record).

What is the best way to structure my program (what classes do I need and especially where do I store my logic)? I came up with structuring it the following way:

  • Main.java ("view")
  • SQLConnection.java
  • PeopleTableModel.java (only stores and returns data given by the passed ResultSet, "model" inherits from DefaultTableModel)
  • PeopleTable.java (stores basically all my logic including KeyListener and MouseListener, "controller", inherits from JTable)

Are there better ways to achieve my goals? If so, what are they?

© Programmers or respective owner

Related posts about java

Related posts about architecture