1,105,395 Community Members

JTable getColumnModel().getSelectionModel

Member Avatar
Newbie Poster
5 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
//Why do I have to use following code to get a column number: 
    ListSelectionModel ks = tabel.getColumnModel().getSelectionModel();
//Rowselection does not require a special row selection, I can get a proper row number with: 
    ListSelectionModel ss = tabel.getSelectionModel();
Member Avatar
... trying to help
10,380 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,750 [?]
Skill Endorsements: 47 [?]

Remember that columns may be re-ordered relative to the underlying data model. Similarly rows can be sorted and/or filtered before display, in which case to get a row number that refers to the actual row in the data model you need the convertRowIndexToModel method.
Those extra levels of indirection and method calls would be unnecessary for a simple table that just displays the data fully and in the same order, but are needed for the general case where the visible/selectable columns and rows do not correspond directly to the data model.

Member Avatar
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
  • everything depends of JTable.setSelectionMode

  • To retrieve the current selection, use JTable.getSelectedRows which returns an array of row indexes, and JTable.getSelectedColumns which returns an array of column indexes. To retrieve the coordinates of the lead selection, refer to the selection models for the table itself and for the table's column model. The following code formats a string containing the row and column of the lead selection:

    String.format("Lead Selection: %d, %d. ",

  • in other hand this is restiction (very bad, disatvantage for JTable) ListSelectionXxx is only one dimensional

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
Tags Related to this Article