Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In JTable, you have the</p> <pre><code>JTable.getSelectedRow() </code></pre> <p>and</p> <pre><code>JTable.getSelectedColumn() </code></pre> <p>You can try combine this two method with a MouseListener and a KeyListener. With the KeyListener you check if user is pressing the CTRL key, which means that user is selecting cells, then with a mouse listener, for every click you store maybe in a Vector or ArrayList the selected cells:</p> <pre><code>//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&lt;int[]&gt;();//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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload