JTable values not refreshed after tables is sorted - java

I have JTable object, filled with data provided from my implementation of AbstractTableModel. I have mouse event listener and when I click on some cell I get its row and column position. With this values I call getValueAt(int row, int column) method from TableModel and I get my data. The problem is the data in the table can be sorted when I click on column name. When again I try to call getValueAt() I get the old value
Example:
col1|col2
val11|val21
val12|val22
When I click on col1
values are:
col1|col2
val12|val22
val11|val21
The I call getValueAt(0,0) and still get val11, and I should get val12. How can I fix this ? Thanks!

The row index in the view (that you probably get from your mouse listener - if you posted the code as requested, we would know instead of having to guess) is not the same as the row index in the model, once the table is sorted. To convert from the view index to the model index, use convertRowIndexToModel().
Note that the same care must be taken for columns, as the user can reorder them by drag and dropping them.

I've manage to do it. table.getValueAt() should be called direct to the table object. My previous attempt was table.getModel().getValueAt().

Related

Filling up cell when clicked

First of all thanks for your support. I'm creating an application with MySQL database to store User details (Names, Age..). Just want to send the Name value to a JTable cell clicked by the user.
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
Is this possible any how?
I'm able to store the value in a String, and show it in System.out.println, but cannot fill the cell up with the value.
It is unclear how do you get this String and what have you tried but you can use setValueAt(...) method to set this String as the selected cell's value. In order to get the selected cell row and column indexes you should use getSelectedRow() and getSelectedColumn() methods, respectively. Finally to detect user's "click" you have to attach a MouseListener to the table or a ListSelectionListener to the table's ListSelectionModel to listen for selection changes.
See the API:
JTable#setValueAt(Object value, int rowIndex, int columnIndex
JTable#getSelectedRow()
JTable#getSelectedColumn()
See also:
How to Use Tables tutorial.

How to hide the Jtable column data?

I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.

Get values of all columns in selected JTable row

so I have a application in which I have a JTable filled with values related to a process list on a computer (so it has things like process name, PID, memory offset, etc). As part of this I want to collect the process name and PID when a user clicks on a row for a certain process--but how do I do this? If I call "table.getSelectedRows()" or "table.getSelectedColumns()" with a row selected I just get one element representing the clicked field's column or row index. Thanks for any help.
You can get the data for each cell in the row by calling table.getValueAt(row, column) once for each column, with 'row' being the selected row index and the 'column' being the column's zero-based index.
Note that this can be somewhat problematic, however, because the user can re-order the columns, and this method references the column in display-order.
The better way to do this is to reference the JTable's TableModel via table.getModel(), and then using the TableModel's getValueAt() method to get the columns in model order (which does not change when the columns are re-ordered in the view).

java JTable how to track rows

I have a JTable which is connected to sqlite. The db table looks like this:
resource_id #primary_key, file, type
I have already implemented adding the rows from db, but the problem is i need to know the resource id when some row in jTable is selected (not the index). Is there a way to add rows with unique ids and not based on indexes (or something similar)?
The current solution adds the resource id as a table column, but that doesnt solve the problem completely.
Create a class say TableData that contains the data from the table. Use a custom TableModel and place the data for the JTable in Vector<TableData>.
You may find it useful to create a method such as addRow(TableData data) in your TableModel that process the data from the table and adds data to the Vector.
In the overridden method public removeRow(int row) you will need to remove the vector data where row can serve as the index.
The overridden method public Object getValueAt(int row, int col) which is used to display the data in the JTable will then just need to retrieve the data from the Vector<TableData>. You can also place the logic for other columns which not part of the TableData in this method.
Dont forget to call fireTableRowsUpdated(row,col) and fireTableCellUpdated(row,col) where ever applicable.
For further reference and how to handling the selections in JTable you can refer this tutorial

How can I hide a row in my JTable?

I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro.
I've written a desktop app, Swing based.
My app includes a JTable; it shows many records of type T, one row per record.
The table model points to Vector vect, named vect, containing all data to be shown in the JTable.
The app includes a combo, named sele, showing three values: 0, 1, 2.
When sele = 0, every record of vect has to be visible in the JTable.
When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.
So, here's my question: how can I make a row not visible in the JTable ?
I can't use the table model, because it points to vect that contains "all" data.
I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.
I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.
I could solve the problem by splitting the data, dividing vect in two, but I don't like that.
Does anybody have an idea ?
thanx in advance,
William
PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.
Mah, perhaps it's a good idea, but frankly I'm not able to understand it...
Use a TableRowSorter - which is:
An implementation of RowSorter that provides sorting and filtering using a TableModel. ..
See How to Use Tables & especially Sorting and Filtering for more info.

Categories