@Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c; if (value instanceof LiveTableDataCell) { LiveTableDataCell dataCell = (LiveTableDataCell) value; c = super.getTableCellRendererComponent( table, dataCell.getCurrent(), isSelected, hasFocus, row, column); // byte coloring // get the new byte values and old byte values String[] currentBytes = dataCell.getCurrent().split(" "); String[] prevBytes = dataCell.getPrevious().split(" "); String result = "<html>"; for (int i = 0; i < currentBytes.length; i++) { // out of bytes in previous data, all other bytes are new if (i >= prevBytes.length) { result = result + ("<font color='red'>" + currentBytes[i] + "</font> "); } else { // check if the byte has changed if (currentBytes[i].equals(prevBytes[i])) { // byte has not changed result = result + ("<font color='black'>" + currentBytes[i] + "</font> "); } else { // byte changed result = result + ("<font color='red'>" + currentBytes[i] + "</font> "); } } } result = result + "</html>"; setText(result); } else { c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } return c; }
public void run() { String dataString = ""; for (int i = 0; i < frame.getDlc(); i++) { dataString = dataString + String.format("%02X ", frame.getData()[i]); } DefaultTableModel liveModel = (DefaultTableModel) liveTable.getModel(); boolean inserted = false; for (int i = 0; i < liveModel.getRowCount(); i++) { if ((int) liveModel.getValueAt(i, 0) == frame.getId()) { liveModel.setValueAt((Object) frame.getDlc(), i, 1); // get the existing cell data try { LiveTableDataCell dataCell = (LiveTableDataCell) liveModel.getValueAt(i, 2); dataCell.swap(); // set current value to new data dataCell.setCurrent(dataString); // push to the table liveModel.setValueAt(dataCell, i, 2); inserted = true; } catch (ClassCastException e) { // dataCell has been edited and is now a string // remove that row liveModel.removeRow(i); } } } if (!inserted) { LiveTableDataCell dataCell = new LiveTableDataCell(); dataCell.setCurrent(dataString); Object[] rowData = {(Object) frame.getId(), (Object) frame.getDlc(), dataCell}; liveModel.addRow(rowData); } }