Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Reading the remark of Kleopatra (her 2nd time she suggested to have a look at <a href="http://javadoc.geotoolkit.org/external/swingx/org/jdesktop/swingx/JXTable.html" rel="nofollow">javax.swing.JXTable</a>, and now I Am sorry I didn't have a look the first time :) ) I suggest you follow the link</p> <p>I had this solution for the same problem: (but I suggest you follow the link above) On resize the table, scale the table column widths to the current table total width. to do this I use a global array of ints for the (relative) column widths):</p> <pre><code>private int[] columnWidths=null; </code></pre> <p>I use this function to set the table column widths:</p> <pre><code>public void setColumnWidths(int[] widths){ int nrCols=table.getModel().getColumnCount(); if(nrCols==0||widths==null){ return; } this.columnWidths=widths.clone(); //current width of the table: int totalWidth=table.getWidth(); int totalWidthRequested=0; int nrRequestedWidths=columnWidths.length; int defaultWidth=(int)Math.floor((double)totalWidth/(double)nrCols); for(int col=0;col&lt;nrCols;col++){ int width = 0; if(columnWidths.length&gt;col){ width=columnWidths[col]; } totalWidthRequested+=width; } //Note: for the not defined columns: use the defaultWidth if(nrRequestedWidths&lt;nrCols){ log.fine("Setting column widths: nr of columns do not match column widths requested"); totalWidthRequested+=((nrCols-nrRequestedWidths)*defaultWidth); } //calculate the scale for the column width double factor=(double)totalWidth/(double)totalWidthRequested; for(int col=0;col&lt;nrCols;col++){ int width = defaultWidth; if(columnWidths.length&gt;col){ //scale the requested width to the current table width width=(int)Math.floor(factor*(double)columnWidths[col]); } table.getColumnModel().getColumn(col).setPreferredWidth(width); table.getColumnModel().getColumn(col).setWidth(width); } } </code></pre> <p>When setting the data I call:</p> <pre><code> setColumnWidths(this.columnWidths); </code></pre> <p>and on changing I call the ComponentListener set to the parent of the table (in my case the JScrollPane that is the container of my table):</p> <pre><code>public void componentResized(ComponentEvent componentEvent) { this.setColumnWidths(this.columnWidths); } </code></pre> <p>note that the JTable table is also global:</p> <pre><code>private JTable table; </code></pre> <p>And here I set the listener:</p> <pre><code> scrollPane=new JScrollPane(table); scrollPane.addComponentListener(this); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload