2013-01-19 21 views
8

Ho un JTable e il suo TableModel, funziona bene, ma quello che voglio fare ora è quello di ottenere le celle selezionate di esso. Ho pensato di fare qualcosa di simile:JTable: come ottenere le celle selezionate?

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() //... 
    } 
} 

Ma naturalmente qualcosa del genere non esiste. Cosa dovrei fare invece?

risposta

19

In JTable, si ha la

JTable.getSelectedRow() 

e

JTable.getSelectedColumn() 

si può provare a combinare questo metodo con un due MouseListener e KeyListener. Con il KeyListener si controlla se l'utente preme il tasto CTRL, il che significa che l'utente sta selezionando le cellule, poi con un listener del mouse, per ogni clic si memorizza forse in un vettore o ArrayList le celle selezionate:

//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); 
} 
+0

+1 per l'avvicinamento. – Amarnath

6

table.getSelectedRow() otterrà la riga selezionata.

table.getSelectedColumns() otterrà colonne selezionate.

getValueAt(rowIndex, columnIndex) darà il valore presente alla riga selezionata per ogni colonna.

+0

Penso che voglia catturare tutte le celle premute, non solo una cella stampata – BackSlash

+0

Ok, potrebbe essere che hai ragione. Aspettiamo la risposta all'OP. – Amarnath

+1

@Harlandraka ha ragione, ho una tabella come un foglio di calcolo in modo da poter scegliere molte celle. – Rob

0

È possibile utilizzare:

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

è possibile ottenere la riga e la colonna con (table.getSelectedRow() e table.getSelectedColumn()), ma se è stato selezionato più di una cella del metodo table.getSelectedRow() e la posizione table.getSelectedColumn() della cella di ritorno della prima cella che era cliccato.

D'altra parte, table.rowAtPoint(e.getPoint()) e table.columnAtPoint(e.getPoint()) restituiscono la tabella della cella esatta su cui è stato fatto clic per l'ultima volta.

Problemi correlati