We help IT Professionals succeed at work.

Reading byte array into JTable using DefaultTableModel

Juniper786 asked
Hi Experts,

I have an array of bytes that I want displayed in a JTable - very much like what a Hex Editor would display - every 16 bytes are displayed as a row in the table. The following are my questions:

1) How do I read every 16th Byte as a row in the JTable - ensuring the last cell is equivalent to the last byte in the array?
2) When I select a particular cell in the table how can I ensure I can obtain the equivalent byte in the array?
3) I have tried using a DefaultTableModel but I keep missing the last few cells in the array because the table only displays in rows of 16 if the array does not equate to an exact division of 16 I miss a row or I have an extra row in the table.
Watch Question


You could pad the array with new zero elements to make it a multiple of 16.

int oldLength = array.size();
// resize array to array.size() + array.size() % 16.
// here fill up the new added elements with 0.
Array.fill(array, oldLength, array.size(), 0);



You might be using null instead of zero.
And then you could display the null ones with a greyed collor (maybe).

Forced accept.

EE Admin

Explore More ContentExplore courses, solutions, and other research materials related to this topic.