I am trying that if a checkbox in column0 is selected a double value will be displayed in column2. Not sure where I am going wrong. Please review the code and advise.....thx....
package JavaTutes;
import java.awt.; import javax.swing.; import javax.swing.table.*;
import TablewCheckBoxComboBox.HeaderRenderer;
public class TableWithCheckBoxorBlank {
enum TriState
{
VALUEA,
VALUEB,
NONE
}
private String[] columnNames = null;
private int noOfRows = 0;
private String[] hstIDs = null;
private Double[] hstTime = null;
private Double unitTime = 0.00;
private int frameHt = 0;
TCProcessData someData = new TCProcessData();
public void makeUI()
{
//Object[] columns = new Object[] {"Quick Est", "Detailed Est", "Ranked Time"};
Object[] columns = new Object[]
{
"<html><center>" + "Quick Est" + "</center></html>",
"<html><center>" + "Detailed Est" + "</center></html>",
"<html><center>" + "Ranked Time" + "</center></html>",
};
noOfRows = someData.getNoOfRows();
Object[][] data = new Object[noOfRows][3];
for (int i = 0; i<noOfRows; i++)
{
data[i][0] = new Boolean(false);
data[i][1] = new Boolean(false);
data[i][2] = TriState.NONE;
}
final DefaultTableModel model = new DefaultTableModel(data, columns)
{
@Override
public boolean isCellEditable(int row, int column)
{
switch (column)
{
case 0: return true;
case 1: return true;
case 2: return TriState.NONE != getValueAt(row, 2);
}
return false;
}
@Override
public Class<?> getColumnClass(int columnIndex)
{
switch (columnIndex)
{
case 0: return Boolean.class;
case 1: return Boolean.class;
case 2: return TriState.class;
//case 2: return double.class;
}
return null;
}
@Override
public void setValueAt(Object value, int row, int column)
{
if (column == 0)
{
if (Boolean.TRUE.equals(value))
{
setValueAt(someData.getUnitTime(row), row, 2);
setValueAt(Boolean.FALSE, row, 1);
}
else
{
setValueAt(TriState.NONE, row, 2);
}
}
else if (column == 1)
{
if (Boolean.TRUE.equals(value))
{
setValueAt(0.00, row, 2);
setValueAt(Boolean.FALSE, row, 0);
}
else
{
setValueAt(TriState.NONE, row, 2);
}
}
}
};
final JTable table = new JTable(model);
HeaderRenderer headerRenderer = new HeaderRenderer(32);
JTableHeader jtHeader = table.getTableHeader();
jtHeader.setDefaultRenderer(headerRenderer);
table.setRowHeight(25);
frameHt = noOfRows*25;
TableCellRenderer tristate = new TableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
if (value instanceof TriState)
{
Double unitTime = someData.getUnitTime(row);
Double theValue = map((TriState) value);
if (theValue != null)
{
TableCellRenderer delegate = table.getDefaultRenderer(Double.class);
return delegate.getTableCellRendererComponent(table, theValue, isSelected, hasFocus, row, column);
}
value = null;
}
TableCellRenderer delegate = table.getDefaultRenderer(Object.class);
return delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
protected Double map(TriState value)
{
if (TriState.VALUEB == value)
{
double defaultNull = 0.00;
return defaultNull;
}
if (TriState.VALUEA == value)
{
return unitTime;
}
return null;
}
};
table.setDefaultRenderer(TriState.class, tristate);
TableCellEditor editor = new DefaultCellEditor(new JCheckBox())
{
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column)
{
if (value instanceof TriState)
{
if (TriState.NONE != value)
{
value = (TriState) value == TriState.VALUEA ? Boolean.TRUE : Boolean.FALSE;
}
}
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
};
table.setDefaultEditor(TriState.class, editor);
JFrame frame = new JFrame("TimeRepTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(208, 230);
frame.setLocationRelativeTo(null);
frame.add(new JScrollPane(table));
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TableWithCheckBoxorBlank().makeUI();
}
});
}
}