JTable table = new JTable(data, columnNames); // create JTable object with data and column names table.setRowHeight(25); // set each row's height to 25 pixels
JTable table = new JTable(data, columnNames); // create JTable object with data and column names for (int row = 0; row < table.getRowCount(); row++) { int cellHeight = table.getRowHeight(row); TableCellRenderer renderer = table.getCellRenderer(row, 0); // get renderer for first column Component comp = table.prepareRenderer(renderer, row, 0); // get component for cell cellHeight = Math.max(cellHeight, comp.getPreferredSize().height); // set height to max of current height and preferred height table.setRowHeight(row, cellHeight); // apply new height }This code iterates over each row in the table and calculates the height of the row based on the preferred height of the cell contents in the first column. The TableCellRenderer is used to get the cell component and its preferred height, and the row height is set to the maximum of the current height and the preferred height. Overall, these examples demonstrate how to customize the row height in a JTable using the setRowHeight method. This method is part of the javax.swing package.