插入多个标头后,JTable更新列标头的高度 [英] JTable update columns header height after multiple header insertion

查看:263
本文介绍了插入多个标头后,JTable更新列标头的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向表中添加新列,但其标题标签(使用htmlbr元素)比已显示的标题中的行多时,应该调用/激发什么,以便标题会相应地调整大小? >

添加之前

添加后

如果在第一次绘制表时一列已经具有该行数(当标签为<html>Card<br>name</html>时),则不会发生这种情况.

添加新记录(因此添加新列)后,我在TableModel中触发fireTableStructureChanged().

解决方案

从@mKorbel的示例开始,以下按钮如图所示更改外观. DefaultTableModel的方法setColumnIdentifiers()代表您调用fireTableStructureChanged().如果扩展AbstractTableModel,则应从TableModel内部执行此操作.

代码:

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);

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

After adding

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>).

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

解决方案

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.

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);

这篇关于插入多个标头后,JTable更新列标头的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆