I have a JTable and I am inserting rows to that JTable dynamically.
After some user intervention, a new row will be inserted .Then I will call a function with parameter as selected row number and inside that function, I have some code which will update the same row accordingly. Each row insertion and row value update code will run in a separate thread.
public void updateRow(int row,JTable myTable)
{
String text = "";
//After lot of processing, setting the table cell value at the 'row' on the 4th column
myTable.setValueAt(text, row, 4);
}
The issue I am facing is as follows,
If the user deleting any row, then row's position will change and at that time if the function updateRow() trying to update some other row, then it will fail because of change of row count.
Lets say I have 3 row at a time and each row's updateRow is in progress.
updateRow(0,userTable);//For the 1st row
updateRow(1,userTable);//For the 2nd row
updateRow(2,userTable);//For the 3rd row
and assume 2nd row's updateRow() is completed. This will cause issue in updateRow() function of 3rd row. Because, it has the row value as '2' . Since the 3rd row got deleted, there is no 3rd row and which in turn cause the following code to fail
myTable.setValueAt(text, row, 4);//Currently, row has the value as '2'
Can anyone suggest me how can I keep track of the row update with row values accordingly, even if the row's position got changed dynamically ?
Thanks in advance.
Instead of relying on the view's row number, operate on the model directly, as suggested in a comment by #mKorbel; if you alter the model, the view will follow. As discussed here, "JTable provides methods that convert from model coordinates to view coordinates — convertColumnIndexToView and convertRowIndexToView — and that convert from view coordinates to model coordinates — convertColumnIndexToModel and convertRowIndexToModel.
Related
I am iterating through the rows of a table, and with each iteration I want to update the "ID" column.
The problem is going through a table of points and updating the "Link ID" (the road link the point it located on). The examples I've seen with updating a specific row/column use the value in the row to identify what to change, however I want to just change the row based off knowing the row's position.
I have a very odd issue with JTable.
I put data in JTable from DB. When user double clicks on any cell, it copies the cell contents of the first column of the row where user double clicked. So far, it works perfect.
The problem arises when the user sorts the JTable by clicking on the header. when the table has been sorted and now when the user double clicks on any row, it doesn't what is currently stored on that row's first column. It copies what was originally stored on the first column of that row when the JTable was not sorted.
Any ideas?
Problem:
The problem here is that you are getting the initial rows indexes in the JTable TableModel, and not the relevants row indexes shown in the table view.
Solution:
You can map the shown indexes of a sorted jTable with their relevants ones in the dataModel using convertRowIndexToModel(index) method which takes in input the row index in the view and returns the index of the corresponding row in the model.
Let's say that you have the following jTable:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));
And then loop throught model indexes and use this method with each index to get its corresponding one in the TableModel:
table.getRowSorter().convertRowIndexToModel(0); // index 0 here
As suggested in How to Use Tables: Sorting and Filtering, "When using a sorter, always remember to translate cell coordinates." Most likely, you have neglected this in your event handler.
Try Sorting your JTable TableModel data too. Jtable -> TableModel is the one which hold the actual data. JTable is just a view.
I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.
This is my problem. Can any one give me an idea how to move rows downwards...
Regards...
Upendra.S
You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.
For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.
Consult the JTable tutorial for more information
have look at JTable tutorial
part TableModel
method from DefaultTableModel insertRow()
I have a jcombo box which has some items like "schoolbooks","collegebooks","historybooks".and i have dynamic ArrayList object of corresponding books...When i click the combo box item "schoolbooks" or "historybooks" ,it should display the contents in JTable from the arraylist.while every action performing,the JTable has to display the contents of the corresponding item of 'schoolbooks' or 'historybooks' .it should not append new rows when every action is performing ...i have used default table model in this.but when i add 3 or 4 rows using default table model,its appending the row with previous here..if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows ..i am not able to solve this..please if anyone know this,please help...
You might want to review How to Use Tables as a guide to preparing your sscce. As you are using DefaultTableModel, you'll need to show how you construct the Object[] passed to addRow() and how you calculate the index passed to removeRow().
if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows
When you remove multiple rows you need to remove the row from the end of the table down to 0:
for (int i = table.getRowCount() - 1; i > 0; i--)
{
// add logic here
}
I have a JTable which has 1st row blank. Now when I sort the table based on a column by clicking on that column then the blank row goes at the bottom. If I insert something in the blank row and do the sorting then the row is placed accordingly. How do I keep track of its row index even when it is sorted. I need to access that row but if user does the sorting then i loose the row index as it is no more the 1st row.
Assuming you're using the TableRowSorter stuff added in Java 6, I think what you need to look at are the methods convertRowIndexToModel and convertRowIndexToView in the RowSorter Class. You'd do something like
table.getRowSorter().convertRowIndexToView(0)
to find out which visible row index is actually row index 0 from your model.
Edit: As Tulskiy pointed out in the comments, this may cause a NullPointerException if no row sorter is assigned to the table. Better to use the methods directly on JTable instead, e.g. table.convertRowIndexToView(0)
JTable.convertRowIndexToView() will return you an index of the row in the view based on its index in the model. JTable.convertRowIndexToModel() will do the opposite.
SOLUTION 1
There is some issue when using jTable1.convertRowIndexToModel(jTable1.getSelectedRow()) if you sort the values according to date or time will return incorrect values from the convertRowIndexToView(0) therefore use convertRowIndexToModel(0).
SOLUTION 2
This will retrieve from the new model when sorted.
jTable1.getValueAt(jTable1.getSelectedRow(), 0);
This will retrieve value according to the original index even when sorted.
((DefaultTableModel)jTable1.getModel()).getValuAt(jTable1.getSelectedRow(), 0);