Structure of a correctly implemented JTable with TableModel and Listeners?
- by bamboocha
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?