Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

java - how to create a JTable with POJOs with a single table model

I’m trying to create a GUI to display the content of a database. I’m using a JTable for that. I want a constructor that takes a list of POJOs and eventually column titles. I want also my JTable to be able to return the selected POJO (for example when I doubleclick on a row). Do I really need to create a table model for each POJO or is there a better way?

I’ve made some searchs on similar topics, but I didn’t find any answer that uses a single table model for all POJOs and none of them has a method that returns a POJO.

Here is the description of my database tables and POJOs:

Database tables:

  • students (id, firstname, lastname, birthdate, grade_id, phonenumber)
  • grades (id, grade)
  • authors (id, firstname, lastname, nationality_id)
  • nationalities (id, nationality)
  • books (id, title, author_id, student_id)

POJOs:

  • Grade (int id, String grade)
  • Student (int id, String firstname, String lastname, Grade grade)
  • Author (int id, String firstname, String lastname, Nationality nationality)
  • Nationality (int id, String nationality)
  • Book (int id, String title, Author author, Student student)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm guessing someone has already created a generic TableModel backed by bean getters and setters. Try googling ReflectionTableModel or BeanTableModel.

Failing that it would be quite simple to roll your own TableModel using the reflection api


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...