无法获得JTable总宽度

时间:2018-01-02 13:54:23

标签: java swing jtable width

我尝试将JTable的列宽设置为timeLineDialog占总数的百分比。我被停止了,因为getWidth()返回 0

dialog.add(header,BorderLayout.NORTH);
dialog.add(timeLineDialog,BorderLayout.CENTER);
int panelSize=timeLineDialog.getWidth();
System.out.println("total width="+Integer.toString(panelSize));
for (int i=0; i<percentWidths.length; i++) {
    System.out.println("setting column "+Integer.toString(i)+" to "+Integer.toString((int) Math.round(panelSize*percentWidths[i])));
    timeLineDialog.getColumnModel().getColumn(i).setPreferredWidth((int) Math.round(panelSize*percentWidths[i]));
}
timeLineDialog.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
dialog.add(footer,BorderLayout.SOUTH);
dialog.pack();
dialog.setVisible(true);

调试输出显示panelSize=0。如何获得JTable的宽度? TIA。

0 个答案:

没有答案