jeudi 29 juin 2017

Looking for a generic way to display and edit a POJO through a JTable

Short version:

.json ==> treeNode.userObject (general case) ==> editable JTable ==> save back to .json

long version:

I've been working on an editor to create the objects I will be using in a text-based game engine. The way the program works is to display a tree of JSON objects in a directory, and by selecting one display it's properties in a JTable. I already had it working with objects that I coded separate table models for, and populated each row with a specific field from that object, but want to implement a more flexible system of displaying and editing a POJO through a JTable.

I want to be able to edit the number/types of fields an object has in its .class file and not have to add new code to the relevant table model. Ideally I want to have just one table model that displays the selected object's fields by name and value, edits the object's fields when a cell changes value, and can display whatever other object the user selects from the JTree.

My old method required each class to have its own table model with a method that set every cell in the second column to the corresponding value. I want a table model that can acheive the same functionality dynamically for any object passed to it.

My latest attempt has been using reflection, and I can display the object, but am having difficulty figuring out how to get the info back from the table into the object to save as a .json again. Also I've seen lots of warnings about using reflection as it's harder to debug?

question:

Is reflection the best route to go for this, and if so what are some good resources for learning how to do what I'm trying to do? If not, what are some other methods that would work? Would my best bet be to stick to hardcoding the rows and tablemodels for each .class?

Also, I was trying to keep this whole question more general, but if you want me to provide some code for a (maybe?) better idea of what I'm trying to acheive, just let me know and I can get some "tidy" example in.





Aucun commentaire:

Enregistrer un commentaire