I have a JTable with a few rows of data but the rows don't stretch across the full width of the table. I'd like to be able to see all of the data without it being cut off and have the zebra stripes stretch the whole width, right now this is how my table looks, so far I have been successful in having my data being shown without being cut off:
I have already attempted to achieve what I want with the following code:
orderTable = new JTable(orderTableModel ) {
public boolean isCellEditable(int row, int col) {
return false;
}
public Component prepareRenderer(TableCellRenderer r, int row, int col) {
Component c = super.prepareRenderer(r, row, col);
// Next 3 lines adapted from https://stackoverflow.com/questions/17858132/automatically-adjust-jtable-column-to-fit-content/25570812
int rendererWidth = c.getPreferredSize().width;
TableColumn tableColumn = getColumnModel().getColumn(col);
tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth())); // Sets width of columns to fill content.
// Rows alternate in colour for readability.
if (row % 2 == 0) {
c.setBackground(Color.WHITE);
} else {
c.setBackground(new Color(234, 234, 234));
}
if (isRowSelected(row)) {
c.setBackground(new Color(24, 134, 254));
}
return c;
}
};
orderTable.setFont(new Font("", 0, 14));
orderTable.setRowHeight(orderTable.getRowHeight() + 10);
orderTable.setAutoCreateRowSorter(true);
orderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);