问题
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