samedi 3 septembre 2022

checkbox in jtable is not clickable

Hi i have a code that get pics and data from a database thin show it in a jtable with checkbox column then the user can choose which item he wants

the problem i made my own jtable put the chickboxs are not clickable from some research maybe i need setValueAt and GetValueAt methods but i have no idea how to implement it for my code thanks

my model code

 import javax.swing.Icon;
import javax.swing.table.AbstractTableModel;
 public class TheModel  extends AbstractTableModel {

public String[] columns;
public Object[][] rows;

 public TheModel (){}
 public TheModel(Object[][] data,String[] columnName){
    this.columns=columnName;
    this.rows=data;
}

@Override
 public Class getColumnClass(int column){
       if (column==0){
         return Boolean.class;
     }
       
       
     if (column==4){
         return Icon.class;
     }
    
     else 
    return getValueAt(0, column).getClass();
   }
 
 
  public int getRowCount( ) {
return this.rows.length;
  }
public int getColumnCount() {
    return this.columns.length;
  }

public Object getValueAt(int rowIndex, int columnIndex) {
    return this.rows[rowIndex][columnIndex];
    
   
}
public String getColumnName(int columnIndex){
    return this.columns[columnIndex];
}

public boolean isCellEditable(int row,int column){
   if(column==0)
       return true;
   return false;
}
}



   public class JtableWithImage extends javax.swing.JFrame {

public JtableWithImage() {
    initComponents();
    populateJTable();
}

   listOfProducts lp=new listOfProducts();
    ArrayList<product> list=lp.BindTable();
    String[] columnName={"اختيار","id", "أسم الصنف","وصف الصنف", "الصورة", "الابعاد", "السعر"};
    Object[][] rows = new Object[list.size()][7];

// to get the selected products
public void populateJTable(){
 
      for(int i = 0; i < list.size(); i++){
        rows[i][0] =  true ;
        rows[i][1] = list.get(i).getId();
        rows[i][2] = list.get(i).getName();
        rows[i][3] = list.get(i).getDescription();
       
        
        if(list.get(i).getImage()!= null){
            ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getImage()).getImage()
         .getScaledInstance(150, 120, Image.SCALE_SMOOTH) );   
            
        // System.out.println("image icon done");
            
        rows[i][4] = image;
        }
        
        else{
        rows[i][4] = null;
        }
       // System.out.print( list.get(i).getDimensions());
        rows[i][5] = list.get(i).getDimensions();
        rows[i][6] = list.get(i).getPrice();
          
    }
    TheModel model = new TheModel(rows, columnName);
    jTable1.setModel(model);
    jTable1.setRowHeight(120);

    jTable1.setRowMargin(0);
    jTable1.getColumnModel().getColumn(4).setPreferredWidth(150);



Aucun commentaire:

Enregistrer un commentaire