Reporting Functionality - resizing columns to content width
[habeas.git] / src / habeas / NoticesReportViewDialog.java
index 2636cbc..8852409 100644 (file)
@@ -5,12 +5,17 @@
  */
 package habeas;
 
+import java.awt.Component;
 import java.util.ArrayList;
 import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
+import javax.swing.JTable;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.filechooser.FileNameExtensionFilter;
 import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumn;
+import javax.swing.table.TableColumnModel;
 
 /**
  *
@@ -77,7 +82,9 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
 
             }
         ));
+        tableReport.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
         tableReport.setCellSelectionEnabled(true);
+        tableReport.setComponentPopupMenu(popMenu);
         jScrollPane2.setViewportView(tableReport);
 
         checkFilterByClient.setText("Filter by Client");
@@ -103,9 +110,9 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
                 .addContainerGap()
                 .addComponent(checkFilterByClient, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(comboClients, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addComponent(comboClients, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(buttonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                .addComponent(buttonApply, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
         );
         layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@@ -228,7 +235,6 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
             }
         };
         tableReport.setModel(mdl);
-        tableReport.setComponentPopupMenu(popMenu);
         
         labelReportTitle.setText(title);
         populateReport ();
@@ -242,8 +248,27 @@ public class NoticesReportViewDialog extends javax.swing.JDialog {
             for (int j = 0; j < reportCols.length; j ++)
                 row[j] = reportData.get(i+j);
             mdl.addRow (row);
         }
+        // resize the column widths to content size
+        for (int i = 0; i < tableReport.getColumnCount(); i ++) {
+
+            TableColumn clm = tableReport.getColumnModel().getColumn (i);
+            int preferredWidth = clm.getMinWidth()+10;
+            int maxWidth = 300;
+            int minWidth = 100;
+            for (int r = 0; r < tableReport.getRowCount(); r ++) {
+                TableCellRenderer rend = tableReport.getCellRenderer(r, i);
+                Component c = tableReport.prepareRenderer(rend, r, i);
+                int width = c.getPreferredSize().width + tableReport.getIntercellSpacing().width;
+                preferredWidth = Math.max (preferredWidth, width+10);
+                if (preferredWidth >= maxWidth)
+                {
+                    preferredWidth = maxWidth;
+                    break;
+                }
+            }
+            clm.setPreferredWidth(Math.max(minWidth, preferredWidth));
+        }        
     }
 
     private void populateClients() {