Jtable Renderer dilemma when there is no data intially
So I have a dilemma, I would like to create set a TableCellRender for a JTable that I have so that it renders a button component on the last column. I have a functional product already, when there is data, but when there isn't data in the table by default then it won't render. I have included a small self-contained code snippet below. To make it disfunctional, uncomment the TableModel that I have commented out, so that instead of the tableModel constructor DefaultTableModel(Object[][] data, Object[] columnNames), it uses constructor DefaultTableModel(Vector columnNames, int rowCount)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
publicclass TableButton3extends JFrame
{
private String[] transactionCols ={"Qty","Product","Cancel"};
private Object[][] data ={{5,"prod1",new JButton("Cancel")},
{6,"prod2",new JButton("Cancel")},
{7,"prod3",new JButton("Cancel")}};
public TableButton3()
{
TableCellRenderer defaultRenderer;
JTable table =new JTable(tableModel);
defaultRenderer = table.getDefaultRenderer(JButton.class);
table.setDefaultRenderer(JButton.class,
new StatusTableRenderer(defaultRenderer));
JScrollPane scrollPane =new JScrollPane( table );
getContentPane().add( scrollPane );
}
// comment the next lines out to make it dysfunctional
private DefaultTableModel tableModel =new DefaultTableModel(data, transactionCols){
// and uncomment next lines to make it dysfunctional
//private DefaultTableModel tableModel = new //DefaultTableModel(transactionCols, 10)
publicboolean isCellEditable(int row,int col){returnfalse;}
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
publicstaticvoid main(String[] args)
{
TableButton3 frame =new TableButton3();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
publicclass StatusTableRendererimplements TableCellRenderer{
private TableCellRenderer defaultRenderer;
public StatusTableRenderer(TableCellRenderer renderer){
defaultRenderer = renderer;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected,boolean hasFocus,int row,int column){
if (valueinstanceof Component){
return (Component)value;
}
return defaultRenderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}
}
}
As you can see it'll die now in the getColumnClass area because getValueAt(0, column) = null, but how do I avoid this? I want a blank table with 10 rows by default, but I thought there should be a way to get a handle to the TableColumn and setDefaultRenderer or something. I know the third column is going to have JButtons, but it doesn't at this point.

