Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Thank you @Martijn Pieters for you grammar & spell checks! You know, my bad English is very! ;) ^^ –  bamboocha Nov 11 '12 at 22:22
add comment

1 Answer 1

up vote 1 down vote accepted

I recommend you to use model-view-controller design pattern. In the class view you will have the GUI components. In the class model you will have the logic of your program. In the controller you choose what data to pass from model to view and vice versa.

I also recommend you to use a class for each table from DB, like in hibernate. And you will have a class DBAccess, where you access the DB and make queries. Be aware that in Java it is recommended that a class to have a single responsibility. That will make your program more easy to test.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.