1

I have made a table with my own model so cells are not editable

String[] colNames = {"Barcode","Name","Exp Date"};
    String[][] rowInfo = new String[mainSuper.getProductVector().size()][3];
    Iterator<Product> itr=mainSuper.getProductVector().iterator();
    int i=0;
    Product temp;
    while(itr.hasNext()){
        temp= itr.next();
        rowInfo[i][0]=temp.getBarcode();
        rowInfo[i][1]=temp.getName();
        if (temp.getExpDate()==null) {
            rowInfo[i][2]="None";
        }else{
        rowInfo[i][2]=temp.getExpDate().toString();
        }
        i++;
    }
    prodTable=new JTable(rowInfo, colNames){
        private static final long serialVersionUID = 1L;

        public boolean isCellEditable(int row, int column) {                
                return false;               
        }
    };

I am trying to make a button that will delete the selected row in the table (only one row can be selected) but I am having trouble making it work. I tried a few solution from the site but none of them worked. thanks for the help.

kfir648
  • 55
  • 2

1 Answers1

0

You can remove a row from a JTable like this:

((DefaultTableModel)yourJTable.getModel()).removeRow(rowindex);

Instead of "rowindex" put in your row you want to delete.

Pascal
  • 1,255
  • 5
  • 20
  • 44
  • 1
    I have tried it but I get this error: "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable$1 cannot be cast to javax.swing.table.DefaultTableModel at GUI.MainFrame$2.actionPerformed(MainFrame.java:70) – kfir648 May 18 '15 at 12:26
  • Yes, and if you catch this error? Does it work? – Pascal May 18 '15 at 12:27
  • 1
    I solved it by making an inner class that extends defaultTableModel. I tried it before but it didnt work because I forgot to add aconstructor. – kfir648 May 18 '15 at 12:48
  • Ok, then please set the question as answered. – Pascal May 18 '15 at 12:53