I am trying to add a JButton as the first column on the table I created. I did make a research and couldn't find any solution for the tables that use abstract table model.
Here, I create an object array for each record that has texts and boolean variables to have the table render check boxes. Then those object arrays saved into an ArrayList
Here's my code to create my table data.
public ArrayList<Object[]> setTableData(){
/*
* ItemInfo fields
**********************
* line[0] - ReferenceNo
* line[1] - Quantity
* line[2] - ItemNameDescriptionSKU
* line[3] - Cube
*/
//Setting the data for the table
ArrayList<Object[]> itemList = new ArrayList<>();
for (int i=0; i<this.itemInfo.size();i++){
Object[] tempArray = new Object[7];
tempArray[0] = this.itemInfo.get(i)[1]; //Quantity
tempArray[1] = this.itemInfo.get(i)[2].toUpperCase(); //Item description
tempArray[2] = this.itemInfo.get(i)[3]; //Cube
//This adds charges if the payment type is COD
//To not to write the charge amount for every row
//checks the COD type only at the first record of items
if (i==0 && this.invoice[8].equals("COD"))
tempArray[3] = this.invoice[22]; //Charges if the invoice type is COD, null otherwise
else
tempArray[3] = " ";
tempArray[4] = new Boolean (false); //Loaded
tempArray[5] = new Boolean (false); //Delivered (Will be ignored if pickup)
itemList.add(tempArray);
}
return itemList;
Here's my table model
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TicketTableModel extends AbstractTableModel {
private ArrayList<Object[]> data;
private boolean isDelivery;
private String[] columns;
public TicketTableModel(ArrayList<Object[]> itemInfo, boolean isDelivery){
super();
this.data = itemInfo;
this.isDelivery = isDelivery;
}
@Override
public String getColumnName(int i) {
return this.columns[i];
}
public void setColumns ( String[] columns1 ){
this.columns = columns1;
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
return columns.length;
}
@Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
if (col < 3)
return false;
else
return true;
}
@Override
public void setValueAt(Object value, int row, int col) {
data.get(row)[col] = value;
fireTableCellUpdated(row, col);
}
@Override
public Object getValueAt(int row, int col) {
return this.data.get(row)[col];
}
Take a look at Table Button Column.
This class implements the render/editor needed to make the button functional. You also provide the Action
to invoke when the button is pressed.