插入多个标头后,JTable更新列标头的高度 [英] JTable update columns header height after multiple header insertion
问题描述
当我向表中添加新列,但其标题标签(使用html
和br
元素)比已显示的标题中的行多时,应该调用/激发什么,以便标题会相应地调整大小? >
添加之前
添加后
如果在第一次绘制表时一列已经具有该行数(当标签为<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屋!