Reputation: 1808
In my application, I use a JTable
with a few resizable columns. At closing, I want the app to store the size of the columns displayed.
My problem is that even if I manually resize the columns, the getWidth()
function always returns 75 (the default value), no matter the real size of the column. If I look at the TableColumn
object with the debugger, it's min size is 15, it's max size is 2147483648, it's preferred size is 75 and it's size is 75. But it's displayed size is clearly not 75!
How can I get the real size of my columns?
The code for getting the widths :
for(i=0;i<TableOpérations.getColumnCount();i++){
tc=TableOpérations.getColumn(TableOpérations.getColumnName(i));
width=tc.getWidth();
}
Upvotes: 1
Views: 5248
Reputation: 691765
I don't see the behavior you're seeing. The following program displays the correct width each time the button is pressed. You must be displaying the widths of the columns of another table or table column model.
public class TableColumnTest extends JFrame {
private JTable table;
public TableColumnTest() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
table = new JTable(5, 4);
p.add(new JScrollPane(table), BorderLayout.CENTER);
JButton b = new JButton("Test");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayWidths();
}
});
p.add(b, BorderLayout.SOUTH);
add(p);
pack();
}
private void displayWidths() {
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumnModel().getColumn(i);
System.out.println("Width of column " + i + " : " + column.getWidth());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableColumnTest().setVisible(true);
}
});
}
}
Upvotes: 2