I am trying to match the width of a JTable column depending on internal data. My code is:
for(int column = 0; column < gui.testsuiteInfoTable.getColumnCount(); column ++){ int width =0; for (int row = 0; row < gui.testsuiteInfoTable.getRowCount(); row++) { TableCellRenderer renderer = gui.testsuiteInfoTable.getCellRenderer(row, column); Component comp = gui.testsuiteInfoTable.prepareRenderer(renderer, row, column); width = Math.max (comp.getPreferredSize().width, width); System.out.println(width); } TableColumn col = new TableColumn(); col = gui.testsuiteInfoTable.getColumnModel().getColumn(column); System.out.println(width); col.setWidth(width); gui.testsuiteInfoTable.revalidate(); } }
The sizes are correct, I think, but the columns of the table still have the same width! The table is built into ScrollPane in GridBagLayout, is this a problem? Thanks for any suggestions.
java swing jtable tablecolumn preferredsize
Hans en
source share