changeset 8:88d02fa97d78

Got WasherDialog table laying out somewhat sanely.
author David Barts <n5jrn@me.com>
date Fri, 10 Apr 2020 15:09:36 -0700
parents 65d14d44bc3f
children 0a106e9b91b4
files src/name/blackcap/exifwasher/Misc.kt src/name/blackcap/exifwasher/WashDialog.kt
diffstat 2 files changed, 37 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/Misc.kt	Fri Apr 10 13:44:04 2020 -0700
+++ b/src/name/blackcap/exifwasher/Misc.kt	Fri Apr 10 15:09:36 2020 -0700
@@ -5,8 +5,13 @@
 
 import java.awt.Component
 import java.awt.Cursor
+import java.awt.Dimension
+import java.awt.Font
+import java.awt.FontMetrics
+import java.awt.Graphics
 import java.awt.Toolkit
 import javax.swing.*
+import javax.swing.table.TableColumnModel
 import kotlin.annotation.*
 import kotlin.properties.ReadWriteProperty
 import kotlin.reflect.*
@@ -163,3 +168,27 @@
     setVisible(false)
     dispose()
 }
+
+/**
+ * Set column width of a table.
+ */
+fun JTable.setColWidth(col: Int, width: Int, string: String?) {
+    val FUZZ = 4
+    columnModel.getColumn(col).preferredWidth = if (string.isNullOrEmpty()) {
+        width + FUZZ
+    } else {
+        maxOf(width, graphics.fontMetrics.stringWidth(string)) + FUZZ
+    }
+}
+
+/**
+ * Set overall width of a table.
+ */
+fun JTable.setOverallWidth() {
+    val tcm = columnModel
+    var total = 0
+    for (i in 0 .. tcm.columnCount - 1) {
+        total += tcm.getColumn(i).preferredWidth
+    }
+    preferredSize = Dimension(total, preferredSize.height)
+}
--- a/src/name/blackcap/exifwasher/WashDialog.kt	Fri Apr 10 13:44:04 2020 -0700
+++ b/src/name/blackcap/exifwasher/WashDialog.kt	Fri Apr 10 15:09:36 2020 -0700
@@ -83,16 +83,12 @@
                 } else {
                     myTable.run {
                         model = MyTableModel(tableData, COLUMN_NAMES)
-                        columnModel.run {
-                            getColumn(0).run {
-                                preferredWidth = 10
-                                maxWidth = 850000000 /* xxx - don't ask */
-                            }
-                            getColumn(1).preferredWidth = 100  /* key name */
-                            getColumn(2).preferredWidth = 5  /* type */
-                            getColumn(3).preferredWidth = 200  /* value */
-                        }
-                        autoResizeMode = JTable.AUTO_RESIZE_LAST_COLUMN
+                        autoResizeMode = JTable.AUTO_RESIZE_OFF
+                        setColWidth(0, 0, COLUMN_NAMES[0])
+                        setColWidth(1, 180, null)
+                        setColWidth(2, 72, "Undefined")
+                        setColWidth(3, 720, null)
+                        setOverallWidth()
                     }
                     setVisible(true)
                 }
@@ -200,7 +196,7 @@
                     alignmentX = JScrollPane.LEFT_ALIGNMENT
                     border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
                     verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
-                    horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
+                    horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
                     preferredSize = Dimension(WIDTH, HEIGHT)
                     background = Application.mainFrame.background
                 })
@@ -215,6 +211,7 @@
                 add(resetButton)
                 add(Box.createHorizontalGlue())
                 add(cancelButton)
+                add(Box.createHorizontalStrut(BW2))
                 add(washButton)
             })
         }