I am under the impression that after column 4 [index 3] the rest of my jtable does react to formatting or data validation.
As you can see in this image the colour just stops after column 4:
Declaration:
private final JTable table = new JTable(new TableModelDB("all"));
private final JScrollPane scrollPane = new JScrollPane(table);
Here is all my code relating to the jtable:
JFormattedTextField ftext = new JFormattedTextField();
try {
MaskFormatter mask = new MaskFormatter("####-##-##");
mask.setPlaceholderCharacter('_');
mask.setAllowsInvalid(false);
mask.install(ftext);
} catch (ParseException e) {
System.out.println("error: " + e);
}
table.setDefaultEditor(Object.class, new MyCellEditor());
table.getColumnModel().getColumn(3).setCellEditor(new MyCellEditor(ftext));
table.setRowHeight(30);
table.setFillsViewportHeight(true);
table.getModel().addTableModelListener(this);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultRenderer(Object.class, cr);
table.setAutoCreateColumnsFromModel(false);
resizeColumnWidth();
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 20));
table.setFont(new Font("Arial", Font.PLAIN, 20));
Data Validation using CellEditor (The validation works just fine until column=4, using println() I figured out that the stopCellEditing method was never called):
private boolean editValidation(String data) {
System.out.println("editValidation");
int column = table.getSelectedColumn();
if (data == null || data.trim().isEmpty()) {
System.out.println("c0");
JOptionPane.showMessageDialog(null, "Cannot leave a field blank");
return false;
}
if (column == 1 && data.length() > 255) {
JOptionPane.showMessageDialog(null, "The Income name is too long (Max 255 characters)");
return false;
}
if (column == 2 && data.length() > 255) {
JOptionPane.showMessageDialog(null, "The Category name is too long (Max 255 characters)");
return false;
}
if (column == 3) {
if (data.contains("_")) {
JOptionPane.showMessageDialog(null, "Invalid date");
return false;
} else {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
df.parse(data);
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, "Invalid date");
return false;
}
}
}
if (column == 4) {
System.out.println("c4");
if (!data.matches("^[0-9]+")) {
JOptionPane.showMessageDialog(null, "Amount contains non-number elements");
return false;
} else if (Integer.parseInt(data) <= 0) {
JOptionPane.showMessageDialog(null, "Income cannot be less than or equal to zero");
return false;
}
}
return true;
}
public class MyCellEditor extends DefaultCellEditor {
public MyCellEditor() {
super(new JTextField());
}
MyCellEditor(JFormattedTextField ftext) {
super(ftext);
}
@Override
public boolean stopCellEditing() {
System.out.println("stopCellEditing");
final JTextField field = (JTextField) getComponent();
if (editValidation(field.getText())) {
System.out.println("case t");
field.setBackground(Color.WHITE);
return super.stopCellEditing();
}
System.out.println("case f");
Toolkit.getDefaultToolkit().beep();
field.setBackground(Color.RED);
return false;
}
}
Cell Renderer (even though I made everything light grey, the columns after index 3 weren't applied):
public class CustomCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
c.setBackground(Color.LIGHT_GRAY);
// if (col == 0) {
// c.setBackground(Color.LIGHT_GRAY);
// } else {
// c.setBackground(Color.WHITE);
// }
return c;
}
}
Table Model:
public final class TableModelDB extends AbstractTableModel {
private final WorkingClass wc = new WorkingClass();
private final int startRows = 1;
private final String[] columnNames = {"Income ID", "Income Name", "Category", "Entry Date", "Amount", "Delete"};
private Object[][] data;
public TableModelDB(String type) {
try {
ResultSet rs = wc.searchIncomeRS(type);
int rowCount = wc.rowCount();
int count = 0, amount, total = 0;
Object[][] temp = new Object[rowCount + startRows][6];
while (rs.next()) {
temp[count][0] = rs.getString("ID");
temp[count][1] = rs.getString("EntryName");
temp[count][2] = rs.getString("Category");
Date date = new Date(rs.getDate("EntryDate").getTime());
temp[count][3] = wc.strDate(date);
amount = rs.getInt("Amount");
total += amount;
temp[count][4] = amount;
temp[count++][5] = false;
}
temp[count][4] = total;
data = temp;
} catch (SQLException ex) {
System.out.println("error: " + ex);
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int col) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row, col);
if (o != null) {
return o.getClass();
}
}
return Object.class;
}
public boolean isCellEditable(int row, int col) {
return !(data[row][col] == null || data[row][0] == null || col == 0);
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
If any kind soul could tell me why the columns don't respond it would be greatly appreciated, any other errors or criticism are welcomed too
Thank you very much :)
After creating a few test classes I realised only columns with Strings got the formatting and the DV, so I changed it to:
table.setDefaultEditor(String.class, new MyCellEditor());//DV
table.setDefaultEditor(Integer.class, new MyCellEditor());//DV
table.setDefaultEditor(Boolean.class, new MyCellEditor());//DV
table.getColumnModel().getColumn(3).setCellEditor(new MyCellEditor(ftext));//Mask
table.setDefaultRenderer(String.class, new CustomCellRenderer());//color
table.setDefaultRenderer(Integer.class, new CustomCellRenderer());//color
table.setDefaultRenderer(Boolean.class, new CustomCellRenderer());//color
Now everything works