JTable update columns header height after multiple header insertion

早过忘川 提交于 2019-12-11 14:27:36

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/18817621/jtable-update-columns-header-height-after-multiple-header-insertion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!