2015-01-03 191 views
0

我擴展了AbstractTableModel類以使我的JTable不可編輯,期望第一行。順便說一下,我已經重寫了isCellEditable(int row,int col)方法。通過放置一個整數變量,使nextRow激活,我得到了第一行尊重我的creterias.So,問題是使下一行當前行第零列中的單元格由用戶填充(數據已更改且必須具有值)時激活.Her是迄今爲止的代碼。將不可編輯的JTable行動態設置爲可編輯

package MODEL.tableModel; 

import MODEL.Produit; 
import MODEL.ProduitInBonDachat; 
import java.math.BigDecimal; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.event.TableModelEvent; 
import javax.swing.event.TableModelListener; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.DefaultTableModel; 


public class BonDachatTableModel extends AbstractTableModel implements TableModelListener { 

    private String headerTitle[] = {"Designation", "Qté", "Prix Unitaire", "Sous Total"}; 
    private List<ProduitInBonDachat> data; 
// variable that hold the next 
    private int nextActiveRow = 0; 

    public BonDachatTableModel() { 
     data = new ArrayList<ProduitInBonDachat>(); 
    } 

    @Override 
    public String getColumnName(int i) { 
     return headerTitle[i]; 
    } 

    @Override 
    public int getRowCount() { 
     return 10; 
    } 

    @Override 
    public int getColumnCount() { 
     return headerTitle.length; 
    } 

    public void tableChanged(TableModelEvent event) { 
     int col = event.getColumn(); 
     int fRow = event.getFirstRow(); 

     if ((col == 1) && (col == 2)) { 
      setValueAt(getValueAt(fRow, col), fRow, col); 
      fireTableCellUpdated(fRow, col); 
     } 
    } 

    public Object getValueAt(int row, int col) { 
     try { 
      data.get(row); 
     } catch (IndexOutOfBoundsException e) { 
      return null; 
     } 
     ProduitInBonDachat pInBonDachat = data.get(row); 

     switch (col) { 
      case 0: 
       if (pInBonDachat.getDesignationProduit() == null) { 
        return null; 
       } 
       if(!pInBonDachat.getDesignationProduit().isEmpty()){ 
        nextActiveRow++ ; 
       fireTableCellUpdated(row, col); 
       } 

       return pInBonDachat.getDesignationProduit(); 
      case 1: 
       if (pInBonDachat.getQte() == null) { 
        return null; 
       } 
       return pInBonDachat.getQte(); 
      case 2: 
       if (pInBonDachat.getPrixDeVente() == null) { 
        return null; 
       } 
       return pInBonDachat.getPrixDeVente(); 
      case 3: 
       if (pInBonDachat.getPrixDeVente() == null || pInBonDachat.getQte() == null) { 
        return null; 
       } 
       return pInBonDachat.getQte().multiply(pInBonDachat.getPrixDeVente()); 
      default: 
       return null; 

     } 
    } 



    public boolean isCellEditable(int row, int col) { 
    if(col == 1 || col == 2 || row == nextActiveRow) 
     return true; 
    else 
     return false ; 

} 

    @Override 
    public void setValueAt(Object value, int row, int col) { 
     ProduitInBonDachat tableEtry; 
     try { 
      tableEtry = data.get(row); 
     } catch (IndexOutOfBoundsException e) { 
      tableEtry = new ProduitInBonDachat(); 
      data.add(row, tableEtry); 
     } 

     switch (col) { 
      case 0: 
       tableEtry.setDesignationProduit((String) value); 
       nextRowActive(); 
fireTableCellUpdated(int row, int col); 
       break; 
      case 1: 
       tableEtry.setQte((BigDecimal) value); 
       break; 
      case 2: 
       tableEtry.setPrixDeVente((BigDecimal) value); 
       break; 
      default: 
       super.setValueAt(value, row, col); 

     } 

    } 

    @Override 
    public Class<?> getColumnClass(int col) { 
     switch (col) { 
      case 0: 
       return String.class; 
      case 1: 
       return BigDecimal.class; 
      case 2: 
       return BigDecimal.class; 
      case 3: 
       return BigDecimal.class; 
      default: 
       return super.getColumnClass(col); 
     } 
    } 

    public void nextRowActive(){ 
     nextActiveRow++; 
    } 



} 
+1

查看「isCellEditable」方法中的最後一個「返回true」嗎?這意味着如果它不是第一列而不是nextActiveRow,那麼單元格將可編輯... – MadProgrammer

+0

我已更改isCellEditable(int row,int col);方法,但我的問題是增加nextActiveRow變量只有當(nextActiveRow,0)中的單元格填充一些值,所以nextActiveRow ++變得可編輯。 –

+0

在這種情況下,當調用'setValueAt'時,您需要對當前行的有效性進行評估,並相應地更新'nextActiveRow' ... – MadProgrammer

回答

2

基本上,當前的方法將返回false如果當前列不是0,否則將返回true

也許像...

public boolean isCellEditable(int row, int col) { 
    boolean isEditable = false; 
    System.out.println("update cell edittable"); 
    if(col != 0 && row == nextActiveRow){ 
     isEditable = true; 
    } 
    return isEditable; 
} 

會做得更好...

要更新nextActiveRow值,您需要驗證當前的有效性行,什麼時候適合,例如更新變量...

public void setValueAt(Object value, int row, int col) { 
    ProduitInBonDachat tableEtry; 
    try { 
     tableEtry = data.get(row); 

     switch (col) { 
      case 0: 
       tableEtry.setDesignationProduit((String) value); 
       break; 
      case 1: 
       tableEtry.setQte((BigDecimal) value); 
       break; 
      case 2: 
       tableEtry.setPrixDeVente((BigDecimal) value); 
       break; 
     } 
     fireTableCellUpdated(row, col); 
     if (row == nextActiveRow && activeRowIsVaid()) { 
      nextRowActive(); 
     } 
    } catch (IndexOutOfBoundsException e) { 
     // IMHO, this is not an appropriate action for the 
     // setValueAt method, as the contract suggest that you are 
     // editing an existing row, instead provide a method in your model 
     // which is responsible for inserting/adding new rows 
     //tableEtry = new ProduitInBonDachat(); 
     //data.add(row, tableEtry); 
     // don't forget to fireRowInserted! 
    } 

} 
+0

謝謝你我工作。 –

0

這裏是解決方案,爲我工作,由行,當第一列被填滿啓動的行JTable中。

package MODEL.tableModel; 

import MODEL.Produit; 
import MODEL.ProduitInBonDachat; 
import java.math.BigDecimal; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.event.TableModelEvent; 
import javax.swing.event.TableModelListener; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.DefaultTableModel; 


public class BonDachatTableModel extends AbstractTableModel implements TableModelListener { 

    private String headerTitle[] = {"Designation", "Qté", "Prix Unitaire", "Sous Total"}; 
    private List<ProduitInBonDachat> data; 
    private int nextActiveRow = 0; 

    public BonDachatTableModel() { 
     data = new ArrayList<ProduitInBonDachat>(); 
    } 

    @Override 
    public String getColumnName(int i) { 
     return headerTitle[i]; 
    } 

    @Override 
    public int getRowCount() { 
     return 10; 
    } 

    @Override 
    public int getColumnCount() { 
     return headerTitle.length; 
    } 

    public void tableChanged(TableModelEvent event) { 
     int col = event.getColumn(); 
     int fRow = event.getFirstRow(); 

     if ((col == 1) && (col == 2)) { 
      setValueAt(getValueAt(fRow, col), fRow, col); 
      fireTableCellUpdated(fRow, col); 
     } 
    } 

    public Object getValueAt(int row, int col) { 
     try { 
      data.get(row); 
     } catch (IndexOutOfBoundsException e) { 
      return null; 
     } 
     ProduitInBonDachat pInBonDachat = data.get(row); 

     switch (col) { 
      case 0: 
       if (pInBonDachat.getDesignationProduit() == null) { 
        return null; 
       } 

       return pInBonDachat.getDesignationProduit(); 
      case 1: 
       if (pInBonDachat.getQte() == null) { 
        return null; 
       } 
       return pInBonDachat.getQte(); 
      case 2: 
       if (pInBonDachat.getPrixDeVente() == null) { 
        return null; 
       } 
       return pInBonDachat.getPrixDeVente(); 
      case 3: 
       if (pInBonDachat.getPrixDeVente() == null || pInBonDachat.getQte() == null) { 
        return null; 
       } 
       return pInBonDachat.getQte().multiply(pInBonDachat.getPrixDeVente()); 
      default: 
       return null; 

     } 
    } 

    public boolean isCellEditable(int row, int col) { 
     if (col == 0 && row <= nextActiveRow) { 
      return true; 
     } 
     if (col == 1 && row <= nextActiveRow) { 
      return true; 
     } 
     if (col == 2 && row <= nextActiveRow) { 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public void setValueAt(Object value, int row, int col) { 
     ProduitInBonDachat tableEtry; 
     try { 
      tableEtry = data.get(row); 
     } catch (IndexOutOfBoundsException e) { 
      tableEtry = new ProduitInBonDachat(); 
      data.add(row, tableEtry); 
     } 

     switch (col) { 
      case 0: 
       String valueCast = (String) value; 
       if (valueCast.isEmpty()) { 
        break; 
       } 
       nextActiveRow++; 
       tableEtry.setDesignationProduit((String) value); 

       break; 
      case 1: 
       tableEtry.setQte((BigDecimal) value); 
       break; 
      case 2: 
       tableEtry.setPrixDeVente((BigDecimal) value); 
       break; 
      default: 
       super.setValueAt(value, row, col); 

     } 
     fireTableCellUpdated(row, col); 

    } 

    @Override 
    public Class<?> getColumnClass(int col) { 
     switch (col) { 
      case 0: 
       return String.class; 
      case 1: 
       return BigDecimal.class; 
      case 2: 
       return BigDecimal.class; 
      case 3: 
       return BigDecimal.class; 
      default: 
       return super.getColumnClass(col); 
     } 
    } 

    public void nextRowActive() { 
     nextActiveRow++; 
    } 

}