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.
Related
[i need help in java programming in netbeans.
suppose i have 2 jTable. the first table contains the imported data from the excel file (I do not use data in the database). And the second table serves to display data calculation results from the first table earlier.
questions asked are:
how to find the value of MAXIMUM and MINIMUM in each column, starting from column two, third and so on. and displayed eg in jTextfiield?
how, for example I have a sum formula, which serves to add all the values in row 1 in columns 2,3,4, and so on in the first table. then the formula will be reused on the second line, then and so on for each row in the first table, according to the number of rows in the first table. such as in excel, (which when we have a certain formula we live just copy and drag the cursor down, then automatically all the values on all rows summed)?
lastly how to display all the data in the first table along with the result of the sum in each line before the previous formula, in the second jTable?
please help, just info I use java in netbeans. and for the image of his table I attach also along with this post. thanks.]1
I have implemented a JTable, on which I can search entries with a textField.
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found.
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel.
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up:
Given a DefaultTableModel A full of non-sorted data about a JTable B, where B.setAutoCreateRowSorter() is true , how does one update B after several/any cloumn-sortings of A?
I have read the docs and also looked into this:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener, but it can only tell me that a column was sorted, not really useful. Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case.
Refer to this for the info provided by a RowSorterEvent:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
So don't search the TableModel. You can use the table.getValueAt(...) method to search for the element in each row/column of the table. The data will be accessed in the currently sorted order of the table.
because the sorting doesn't seem to update the DefaultTableModel.
Correct, only the View (JTable) is updated.
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). This is done by using the following JTable methods:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values.
I have a JTable that I am populating from a database. When a user clicks on a cell and edits it I would like to update the database but I don't know which database row index that information came from.
I know that I could add the database index as a column in my JTable and get the index from there when a cell is updated but is there a way that I can do the same thing without showing the index column to the user? Thanks
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.
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);