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...
Related
I am seeking advice in persisting my JTable data in an elegant manner. So far my research has indicated I can iterate through the many columns and rows and extract the data for saving (seems convoluted) or that I can save the table and related data as an Object in an Object file.
I would love to hear some advice from those more versed in this area as I am quite new to JTables and their workings. Are there many other solutions available that may be a better choice?
It depends on what you want to do with the persisted data. If you only want to persist it so that you can display it again later, look at serializing (Java Serializable or Java Externalizable) it to a data-stream that you put somewhere. Later you can read it back (deserialize) and display it again.
If you want to put it in a database where the information is useable for other purposes, then you probably want to implement some object which models your data to keep it clear and simple. Then you can present this in a Swing Jtable by adapting your model to a table model. This still means you need to write the adaptation/transformation logic but it shouldn't be onerous and you get the most usable result. The TableModel is simply a way of looking at your data that a JTable is able to understand. Look at Adaptor patterns to get one idea about the mapping.
Hope that helps.
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
I know how to retrieve data (just text in a table) from a database in Java and how to show it in the console. But I want to load it in a JTable.
Is there a good (modern) way (tutorial), without using Vectors?
Check out this tutorial: How to Use Tables
Seems your question is similar with these two questions:
How to fill data in a JTable with database?
Displaying data from database in JTable
Check out GlazedLists it comes with ready made TableModels that are based on modern collection interfaces.
http://www.glazedlists.com/
If you don't want to use an extra library. You can easily implement your own javax.swing.table.TableModel. I like to implement TableModel and java.util.List so I'm working with just a simple List, and hooking up any List easily.
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.
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.