JTable update columns header height after multiple

2019-09-12 02:33发布

What should be invoked /fired when I added new column to the table but its header label has more lines (using html and br element) than in the already presents headers so the headers will resize accordingly?

Before adding Before adding

After adding enter image description here

This does not happen if when first painting the table a column already has that number of rows (when the label is <html>Card<br>name</html>).

enter image description here

I fire fireTableStructureChanged() in TableModel when new record is added (so new columns are added).

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-12 03:23

Starting from @mKorbel's example, the following button alters the appearance as shown. The method setColumnIdentifiers() of DefaultTableModel invokes fireTableStructureChanged() on your behalf. If you extend AbstractTableModel, you should do this from within your TableModel.

before after

Code:

private DefaultTableModel model = new DefaultTableModel(data, columnNames) {…}
…
frame.add(new JToggleButton(new AbstractAction("Toggle") {
    @Override
    public void actionPerformed(ActionEvent e) {
        JToggleButton b = (JToggleButton) e.getSource();
        if (b.isSelected()) {
            columnNames[0] = "<html>String<br>of pearls</html>";
        } else {
            columnNames[0] = "String";
        }
        model.setColumnIdentifiers(columnNames);
    }
}), BorderLayout.SOUTH);
查看更多
登录 后发表回答