How to get the value of the cell (i; j) from the JTable table?
1 answer
From the table, call the getModel () method, which will return the table model to you, and from it, call the getValueAt (i, j) method, which will return the required element to you.
However, if you use sorting, filtering or something similar, you need to convert the indices using the methods convertRowIndexToModel () and convertCollumnIndexToModel () before calling getValueAt (), otherwise you can get completely different values. There are inverse transform methods - convertRowIndexToView () and convertCollumnIndexToView (), which are used, if I'm not mistaken, for programmatic row selection.
- If you answer a person, answer more fully. For if the View of the table is changed, then the TS will receive not the values that it expected, and you will have to pass as parameters the results of the methods that convert the view / model indexes. There is no difference only if the view displays the model as it is without filtering, changing columns and other things ... - smackmychi
- @smackmychi, thank you, now I just remembered that I burned it myself once again. Corrected the answer. - Yuri_Prime
- @Yuri_Prime, I myself also got caught on it. Because the documentation read how horrible. The saddest thing is that someone leaves such answers. I'm new to Java, and maybe once there was only this way of getting the value of cells. - smackmychi
|