I ran into a problem when I have a JTable laid out using the GridBagLayout manager and the number of rows in the table is not fixed. I intend to fill the space assigned to the table by all its cells, however there is a large empty (and white) space between the last row of the table and the next component in the container.
As a potential solution, I am wondering if I can adjust the cell height to fill the space assigned to the table. So, for example, if there are three lines, then the height of the space should be divided into three lines. If there is only one row, then this row should occupy all the free space.
Any suggestion is welcome, and if there is a better way to achieve the desired effect, please enlighten me. Thank.
PS I use JTable inside JPanel instead of JScrollPane, if that matters.
Edit: So, I tried the following code, which of course adjusts the height of the lines depending on the number of lines present, but still leaves a blank space after the last line and before the next component. I wonder why?
int panelHeight = tPanel.getHeight();
int desiredRowHeight = panelHeight / (numOfRows + 1);
friendsInfo.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(), desiredRowHeight));
table.setRowHeight(desiredRowHeight);
source
share