簡體   English   中英

JTable單擊選擇選中單元格的整行,雙擊編輯行

[英]JTable single click select entire line of selected cell, double click edit row

我有一個項目,其中有一個JTable和:

  • 單擊行時,必須從jtable中選擇整行,而不僅是單擊的單元格
  • 雙擊單元格,該行必須是可編輯的

我使用Netbeans IDE(如果相關)。

JTable代碼:

public class ModelTabelAbonati extends AbstractTableModel {

Abonat[] tabelAbonati = new Abonat[0];


public void Adauga (String nume, String prenume, String cnp, Integer telefon){
    tabelAbonati= Arrays.copyOf(tabelAbonati, tabelAbonati.length+1);
    tabelAbonati[tabelAbonati.length-1]=new Abonat (nume,prenume, cnp, telefon);
    fireTableRowsInserted(tabelAbonati.length-1, tabelAbonati.length-1);
}


public void Adauga(String nume, String prenume, String cnp, int telefon){
    tabelAbonati= Arrays.copyOf(tabelAbonati, tabelAbonati.length+1);
    tabelAbonati[tabelAbonati.length-1]=new Abonat (nume,prenume, cnp, telefon);
    fireTableRowsInserted(tabelAbonati.length-1, tabelAbonati.length-1);
}




@Override
public String getColumnName(int column){
    return new String[]{"Nr. ","Nume ","Prenume ","CNP ","Tel. Fix ","Tel. Mobil"}[column];
}


@Override
public int getRowCount() {
    return tabelAbonati.length;
}

@Override
public int getColumnCount() {
    return 6; 
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    Abonat a= tabelAbonati[rowIndex];

    switch (columnIndex){

    case 0: return rowIndex+1;
    case 1: return a.getNume();
    case 2: return a.getPrenume();
    case 3: return a.getCnp();
    case 4: return a.getTelefon().getTelFix();
    case 5: return a.getTelefon().getTelMobil();
    default: return "ERROR";


}

}

}

將此代碼添加到ModelTabelAbonati以允許編輯單元格:

public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

table = new Jtable()下添加此table.setRowSelectionAllowed(true) table = new Jtable()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM