[英]JTable single click select entire line of selected cell, double click edit row
我有一個項目,其中有一個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.