0

I am trying to add JButton to a JTable but instead of viewing the button it views(javax.swing......). What is the correct way to add a button?

public void setTableSize(ArrayList<CarRental> List)
{
    ArrayList<CarRental> ListToPrint= List;

    DefaultTableModel model=(DefaultTableModel) PrintListTable.getModel();

    model.setRowCount(0);

    EditButton n=new EditButton();
    Edit.addActionListener(n);

    for(int j=0;j<ListToPrint.size();j++)
    {           
        model.addRow(new String[]{j+"",ListToPrint.get(j).getName(),ListToPrint.get(j).getDays()+"",ListToPrint.get(j).getSize(),ListToPrint.get(j).getCarType(),ListToPrint.get(j).WithDriver(),ListToPrint.get(j).DailyFeesDetails(),ListToPrint.get(j).ComputeTotal()+"" });         
        model.setValueAt(Edit, j, 8);
    }
}
Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36

1 Answers1

1

If only it were that easy. Presumably, you want the button to do something when clicked, so you have to set it as a cell editor as well as setting it as a cell renderer (your example just sets the value of the cell, not changing its renderer, so the default renderer is calling toString() on it).

Check out this class written by frequent SO contributor @camickr.

Josh
  • 1,563
  • 11
  • 16