What are the benefits of using a ResultSetTableModel for a JTable? - java

I am writing a desktop application in Java and it uses two JTables as the main output.
I have a working version of the program where I use DefaultTableModels to manage the underlying data of the JTables and I use an ArrayList to store the data in each model.
However, I've come across the ResultSetTableModel which stores the data in a ResultSet and a ResultSetMetaData object to implement the TableModel methods.
But I am not clear on the benefits of using a ResultSetTableModel as opposed to a DefaultTableModel or AbstractTableModel. I have searched and I cannot find any discussion on this.
Does anyone know why one would favor a ResultSetTableModel over the other options?

if you use DefaultTableModel you have to store the data into model manually, and if you ResultSetTableModel data automatically loads into JTable this is the difference.

Related

How to display data in a jTable using linked lists

I have a table from my SQL server and I have connected to and and stored all the data in a linked list. I now want to take that data from the linked list created (everything works fine) but when I try to store it in my table named customerTable it doesn't recognize my table. (My GUI is on a different class, so is my linked list and my customer object that I use to initialize get, set methods, so I think it has something to do with that)
Can anybody help me understand how I do this?
I use this code:
DefaultTableModel model = (DefaultTableModel) customerTable.getModel();
You should implement interface TableModel, to use your existing list as the back of the table model.
See How to Use Tables of the Java Tutorials, on creating table models.
so is my linked list and my customer object that I use to initialize get, set methods
If you are storing the data in a custom Object, then you need to create a custom TableModel for that Object.
Check out Row Table Model for a step-by-step example on how to create this custom TableModel for your object.

JTable missing .addRow()?

I have been wondering why a jTable hasn't got an .addRow() method by default. Why do you have to set a Model before this is possible?
JTable table = new JTable();
table.addRow();
The above is not possible, however:
JTable table2 = new JTable();
table2.setModel(new DefaultTableModel());
table2.addRow(...);
After setting the new model, it IS possible - why?
First of all, by default, the TableModel is not mutable (other then being able to, potentially, modify the existing data), that is, there are no methods within TableModel that provide any means to add or delete rows.
It is up to implementations of TableModel to decide if that functionality is possible. Take a look at TableModel for details about what the default interface provides
Secondly, it is the responsibility of the model to manage the data. It makes no sense for the table to suddenly provide add/delete functionality, when that functionality may or may not exist. Modifications to the data should be done directly via the model - IMHO
Thirdly, there is no JTable#addRow method

Why do we have to use a TableModel for a JTable?

If I want to use a JTable in Java it seems to me for adding rows and doing alters from behind a button or so I always have to use a TableModel (this could be the default one or one created by your own) But my question is: Why do we have to use this. I can't find this in any of the posts I saw. Can someone explain how this works and why it is necessary? And why we can't just add rows to the JTable without a model.
It seems to me that if you want to just show a few records but at creation you don't know all the rows yet it would be easier to just do something like a table.add() to add the row.
You can create the table with data inside without a model attached to it. So why not add data?
Or am I just wrong and can you add also data without a model?
The TableModel interface defines the minimum methods needed by a JTable (view) to render its content (model). AbstractTableModel is an abstract implementation that provides the event plumbing and leaves just three methods that must be overridden. DefaultTableModel goes on to include an internal data model based on Vector and convenient methods to alter that internal model. See Creating a Table Model for a comparison and these contrasting examples.

Java Databinding: how to display data in a JTable?

I'm a .net developer that would like to try develop some simple apps in Java. I would like to know how to do databinding in Java.
How can I show a query result in a JTable?
GustlyWind's comment is the best place to look on how to use a JTable. The key for the data is getting the items in a model. You can use the DefaultTableModel which would require that your table results are put into a 2D Array or Vector. Or you could implement your own model that uses other custom objects from your application or a different underlying data structure.
Either way, as you loop through the ResultSet of your query you are going to have to pull the relevant data and stick it in some sort of Collection.

How to enable GUI behaviors for sorting a JTable when SQL does the sorting?

How do I enable JTable icons and behaviors for sorting table rows by a column, without letting it use a comparison predicate to do the sorting? That is to say, how do I tell the table headers to show the arrow for ascending/descending sort order in the column being used, and get it to call appropriate methods when sort order/column change?
I am trying to create an (editable, filterable, sortable) JTable backed by an SQL query or view. The rows may not fit in memory, and may not map cleanly to java objects, so I want to do all sorting/filtering within SQL. I have already written the code for changing a query to accommodate sorting by column, filtering by values, and visible columns.
To use this, I am planning to write a JTableModel based on a ResultSet with TYPE_SCROLL_SENSITIVE, and CONCUR_UPDATABLE, so changes to the DB get propagated to the ResultSet. I will periodically (several times a second) force a refresh of the visible JTable from the ResultSet, so changes to the database become visible to the user. User changes to the table will be passed to the updateable ResultSet after validation.
I've looked a little bit at how sorting is done normally, but most implementations seems to rely on the JTable creating a javax.swing.RowSorter with a Comparator predicate, or on maintaining a sorted list of rows that fires events when changed. So, my questions:
ORM frameworks are NOT an answer to this question, because the data do not map well to entity objects. Also, the DBMS I am using is H2.
EDIT: Sortable JTable libraries based on applying Comparators or sorting predicates to row objects are also unsuitable, unfortunately. I do not believe I will be able to hold all objects in memory in order to perform sorting. This problem prevents me from using the SwingX JXTables, GlazedLists, or similar libraries. I wish I could, but I can't. Period.
** I will be dealing with many thousand rows, potentially millions, with numerous columns. Yes, I really DO need to use SQL to do the sorting and filtering.**
Questions: (in descending importance)
How do I show indicators for which column is used to sort rows?
How do I get the JTable to fire appropriate events when the column headers are LEFT-clicked to change sort order?
Is there an easier way to force the JTable to update when the database changes?
Is there a library that would make all this considerably easier (connecting DB queries or views and JTables)?
Am I going to run into horrible, horrible problems when I design the system like this?
I have never used it myself but JIDE Data Grids provides a DatabaseTableModel that provides filtering and sorting support using SQL WHERE and ORDER BY.
In answer to 1 and 2, check out SwingX, which already includes a table class with built-in sorting (and filtering). You may be able to adapt this.
Am I going to run into horrible, horrible problems when I design the system like this?
From experience, yes. I worked on a project almost exactly the same as this, where someone had designed a JTable that supposedly 'magically' bound to a database table. This coupled display logic and database access together in one big horrible mess, which we replaced entirely with reflection-driven table models and separate record CRUD operations.
You say that ORM is not the answer...
If the format of the data doesn't change, then it's worth considering anyway. Your 'entity' classes need not represent real-world entities.
If (as I suspect) your entity format changes, it might be worth considering:
A flexible map-based Record class which stores records as key-value pairs;
Dynamically-built table models for your display logic, built by querying record keys, plugged into SwingX tables to get sort and filter for free;
A similarly-designed Repository class which encapsulates your database access separately from the table itself, responsible for loading and saving Records. This acts as an adapter between your updateable ResultSet and the view (although I'd check whether using a ResultSet this way is going to require an open database connection whilst data is visible...).
This separation into 'a table that displays and sorts records' and 'a repository that manages the data' means:
You can reuse the table for non-database-bound data;
You can display database-bound records in things other than tables;
You won't go mad trying to build and test the thing :)
You should be able to subclass javax.swing.RowSorter in order to create a row sorter that does the sorting in the database. From the API docs:
"RowSorter implementations typically don't have a one-to-one mapping with the underlying model, but they can. For example, if a database does the sorting, toggleSortOrder might call through to the database (on a background thread), and override the mapping methods to return the argument that is passed in."
http://docs.oracle.com/javase/6/docs/api/javax/swing/RowSorter.html
Leaving aside the database stuff there's a class called SortableTable that's a part of JIDE Grids. It displays the sorting with a little ^ or v in the table header, and supports sorting by more than 1 column (1v, 2v, etc.).

Categories