37119

JTable update columns header height after multiple header insertion

Question:

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 <img alt="Before adding" class="b-lazy" data-src="https://i.stack.imgur.com/26QmL.png" data-original="https://i.stack.imgur.com/26QmL.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

After adding <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/dHigy.png" data-original="https://i.stack.imgur.com/dHigy.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/nCp9M.png" data-original="https://i.stack.imgur.com/nCp9M.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

Answer1:

Starting from @mKorbel's <a href="https://stackoverflow.com/q/9851253/230513" rel="nofollow">example</a>, 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.

<img alt="before" class="b-lazy" data-src="https://i.stack.imgur.com/65Jcj.png" data-original="https://i.stack.imgur.com/65Jcj.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /><img alt="after" class="b-lazy" data-src="https://i.stack.imgur.com/fxZz3.png" data-original="https://i.stack.imgur.com/fxZz3.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

Recommend

  • FatalErrorException in routes.php line 22: Class 'Painting' not found
  • How to remove control box from MDIchild form
  • CSS: Overlapping DIVs issue
  • How are the painting of invisible controls handled in WinForms?
  • windows batch file combine csv in a folder by column
  • TFS edit build log using custom activity
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • WPF Run Animation in a separate Thread
  • Chrome breakpoint on radio doesn't fire
  • Cannot style mat-tab without ::ng-deep and !important
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Pyglet uses too much cpu
  • Encrypting credit card details using AngularJS in Braintree
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • c++ search a vector for element first seen position
  • Is there any purpose for h2-h6 headings in HTML5?
  • Android onKey w/ virtual keyboard
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • How to make R's read_csv2() recognise the text characters properly
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • iOS: Detect app start via notification press
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Is there a javascript serializer for JSON.Net?
  • CSS Linear-gradient formatting issue accross different browsers
  • Where to put my custom functions in Wordpress?
  • vba code to select only visible cells in specific column except heading
  • Do I've to free mysql result after storing it?
  • Transpose CSV data with awk (pivot transformation)
  • Buffer size for converting unsigned long to string
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal