0

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

Community
  • 1
  • 1
ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • small hint fireTableStructureChanged() to reset all custom renderers/editors, because for this job, to reset (all to default) is this method designated – mKorbel Sep 15 '13 at 22:30
  • Sorry, but I have a problem fully understanding what you suggested. Especially the second post. Should `fireTableStructureChanged()` reset the renderer for headers? – ps-aux Sep 15 '13 at 22:46
  • [your SSCCE can be based on](http://stackoverflow.com/q/9851253/714968) – mKorbel Sep 15 '13 at 22:56
  • @mKorbel: Using your example, I didn't see any renderer problem; as the model owns the columns names, I can see the potential for other problems depending the choice of `TableModel`. – trashgod Sep 16 '13 at 00:00
  • @mKorbel Regarding your comments which you deleted. Yeah, it was a problem to post SSCCE as the code is complicated and broken and I did not have time for that and I was sure it was not the rendering problem but rather issue of the height of the tableheader. Regarding my understanding of your text: Sorry we both have the same mother tongue and I think I am quite proficient in English but I still was not sure I understood what you meant by your comment (strange structure of sentences). (I apologize for this meta off-topic, but just wnated to make myself clear). – ps-aux Sep 17 '13 at 22:04
  • @LeNoob AFAIK there isn't issue, bug in this area, that required some hacks, problem should be in your code, much luck – mKorbel Sep 18 '13 at 06:36
  • 1
    _I did not have time for that_ well, if you can't find the time to make it easy for us to answer, you might find that we don't have time to spare trying to solve _your_ problem. – kleopatra Sep 18 '13 at 08:20

1 Answers1

2

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);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • The question is if `fireTableStructureChanged()` forces to update the header. If yes, then I do call it in my implementation and do not need any other calls. The problem as I found out seems to be with the height of `JScrollPane`'s column header - I provided my own implementation with overridden `getPreferredSize()` to control the height and now it works. Another question is, why does this not updated itself after table header height change? (I will not post the code here as it is really broken and modular and kinda hard to put in in concise way). – ps-aux Sep 17 '13 at 21:58
  • Yes, `fireTableStructureChanged()` should update the header. `DefaultTableModel` invokes `fireTableStructureChanged()` for you in `setColumnIdentifiers()`. `JTable` implements `Scrollable`, so override `getPreferredScrollableViewportSize()` if needed. – trashgod Sep 17 '13 at 22:06