Greetings,
I have been understanding how DefaultTableModel will work on my problem.
I had this JTable which already has the data from a query from my data using JDBC - MySQL.
I the role is that the index0 of the JTable always displays the primary key.
And there will always be an empty row at the end two-dimensional object array where the data of the cell will be retrieved.
As of now, I included in the model a setValueAt method which triggers a fireCellUpdated().
It worked but the downside is that the data would display on the JTable but is changed on the array.
How could I resolve this?
Your response is highly appreciated.
Thanks.
When I check the array, the changes were there. But it won't display on the JTable
You should never update the array directly. The array is used to populate the data in the DefaultTableModel when you create the model. The DefaultTableModel stores the data in a Vector of Vectors so all the data is copied from the Array to the model. After that all updates must to done directly to the TableModel.
If I understand correctly, your data model contains additional information that's not intended for display. DefaultTableModel may be a little awkward for this. In the alternative, your might want to consider AbstractTableModel, which should make it easier to expose only certain portions of the data to your JTable. There are examples in the article How to Use Tables: Creating a Table Model and here.
Related
I am writing an application that uses a JTable containing a model (DefaultTableModel). Now after working on it for some time I am not really sure if it's the best Idea to store all the data in the model.
What benefits are there if all the data is stored in Objects (in an ArrayList for example) and the model only shows the data from the objects of the list?
This came into my mind particularly after I need to write some sorting algorithms which would be way easier to handle on some kind of list. Also saving the "model" --> the list of objects (then) would be easier than saving a table model.
Is it more efficient to stay with my current concept (save data in model | serialize model to save table | sort model itself) or is it better to save everything in Objects each Object representing one row, which are stored in a list?
I need to write some sorting algorithms
JTable supports sorting of the data in the TableModel directly. Read the section from the Swing tutorial on Sorting and Filtering for more information. So this can be handled directly by the table so you don't need external code.
each Object representing one row, which are stored in a list?
Yes this is a valid approach, but the data must still reside in the TableModel. That is the List containing your custom Objects that represent a row in the table must be defined in the TableModel. If for some reason you need to access the List you add helper methods to the TableModel. Don't keep the data in two places.
For example, check out Row Table Model which was designed for this concept. The JButtonTableModel.java code shows what you would need to code for each custom object you want to store in a table.
Currently im doing on a project on ordering system. Let me explain the flow of the system. In ordering class, i have 2 jtable. One of the jtables shows the list of food retrieve from the database. I just have to click on one of the food and press"add", the selected food will transfer to another jtable . After i press"proceed" i will be led to a jtable, to the next jpanel, which consists of the food i have selected. However" i allow one cell to be editable,"quantity" is there any way on how to retrieve the data input by the user and store to database?
Depending on the implementation of the TableModel, you could get the data for an individual row. This would, generally, be the preferred solution, as the data for the row would be encapsulated within a POJO which maintains all the data that you need in a single place that easily manageable.
If you've just dumped a bunch of unassociated properties into the table, you will need to use something like JTable#getValueAt or TableModel#getValueAt
See How to Use Tables for more details
I created a database, in which one of the table name is Location which has 2 columns named Stadium and City. I want that data in each column should get retrieved in 2 JList, but I don't know how. Can you guys tell me how to do it?
The normal way to display a table from a database is of course a JTable. But if you want to opt for 2 JLists instead, it is basically the same principle.
Create a model based on the contents of the database (in this case a ListModel). You can probably just extend from AbstractListModel. Once you have your model, it is trivial to create a JList for it by just passing the model in the constructor.
See the How to use lists tutorial and pay special attention on the Creating a model part of that tutorial
using a jtable -
Can I get the object behind the row
The data displayed in a JTable should be backed by a TableModel, in general a class mapping from a set of business objects to a "table-like" display.
Have a look at the Swing tutorial.
If you tell us more about your usage scenario, one might come up with a good pattern...
As of now, #ChrisJ has the best tip.... YOu can return the POJO in one of the getValueAt columns...
I would like to implement a JTable which will only be edited and updated programatically. I dont want user interaction enabled.
Firstly the input will be from an array of string elements(after filling the array from a String split).
I then want to set the fields with the array data.
How are the column names stored as Final or whatever for the table life?
Do I need to implement a TableModel if I dont want interaction?
You should probably read through the Java documentation for How to Use Tables.
Things you seem interested in:
isCellEditable() - Have it return false means the user can not edit any cells.
getColumnName() - Look up the name in an array or list of strings.
Regarding editability: use JXTable - it supports layered programatic configuration control. Specifically,
supports per-column and per-table narrowing (that is, to read-only)
respects the cell editable property always (that is table.setValueAt does nothing if the cell is not editable)
Yes, the model will be useful but not necessary (also just a DefaultTableModel).
Actually you could just fill the JTable with static data using the right constructor:
public JTable(Object[][] rowData, Object[] columnNames)
but you won't have any control over the data inside the table.
The model is not used just to provide interaction with editing capabilities but to provide a data source for the table either to read either to write, that's why it's always good to have one.