JTable: how to get selected cells?

I have a JTable and its TableModel, it works well, but now I want to get the selected cells. I was thinking of doing something like:

int rows = this.getTable().getRowCount(); int columns = this.getTable().getColumnCount(); for(int i = 0 ; i < rows ; i++) { for(int j = 0 ; j < columns ; j++) { if(table.getCell(i,j).isSelected() //... } } 

But, of course, something like this does not exist. What should I do instead?

+8
selected swing cell jtable
source share
4 answers

In JTable you have

 JTable.getSelectedRow() 

and

 JTable.getSelectedColumn() 

You can try combining this two methods with MouseListener and KeyListener. Using KeyListener, you check whether the user has pressed the CTRL key, which means that the user selects the cells, and then with the mouse listener, for each click that you store, possibly in Vector or ArrayList for the selected cells:

 //global variables JTable theTable = new JTable();//your table boolean pressingCTRL=false;//flag, if pressing CTRL it is true, otherwise it is false. Vector selectedCells = new Vector<int[]>();//int[]because every entry will store {cellX,cellY} public void something(){ KeyListener tableKeyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_CTRL){//check if user is pressing CTRL key pressingCTRL=true; } } @Override public void keyReleased(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_CTRL){//check if user released CTRL key pressingCTRL=false; } } }; MouseListener tableMouseListener = new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if(pressingCTRL){//check if user is pressing CTRL key int row = theTable.rowAtPoint(e.getPoint());//get mouse-selected row int col = theTable.columnAtPoint(e.getPoint());//get mouse-selected col int[] newEntry = new int[]{row,col};//{row,col}=selected cell if(selectedCells.contains(newEntry)){ //cell was already selected, deselect it selectedCells.remove(newEntry); }else{ //cell was not selected selectedCells.add(newEntry); } } } }; theTable.addKeyListener(tableKeyListener); theTable.addMouseListener(tableMouseListener); } 
+19
source share

table.getSelectedRow () will get the selected row.

table.getSelectedColumns () will get the selected columns.

getValueAt (rowIndex, columnIndex) will give the value present in the selected row for each column.

+6
source share

JTable has methods for getting selected rows and getting selected columns .

+3
source share

You can use:

 int row = table.rowAtPoint(e.getPoint()); int col = table.columnAtPoint(e.getPoint()); 

You can get the row and column using ( table.getSelectedRow() and table.getSelectedColumn() ), but if you have selected more than one cell, the table.getSelectedRow() and table.getSelectedColumn() method returns the cell position of the first cell to which has been pressed.

On the other hand, table.rowAtPoint(e.getPoint()) and table.columnAtPoint(e.getPoint()) return the exact cell table that was last clicked.

0
source share

All Articles