我有一个带有几行数据的JTable,但行不会在表的整个宽度上延伸。我希望能够看到所有的数据而不会被切断并让斑马条纹拉伸整个宽度,现在这就是我的表格看起来如此,到目前为止我已成功地将数据显示在没有被切断:
我已经尝试使用以下代码实现我想要的目标:
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);