Table: switching theme looses table grid and intercell spacing (issues #733 and #750)

This commit is contained in:
Karl Tauber
2023-10-22 17:55:10 +02:00
parent cf364c1264
commit 34bd2d781c
4 changed files with 98 additions and 21 deletions

View File

@@ -1,6 +1,14 @@
FlatLaf Change Log
==================
## 3.3-SNAPSHOT
#### Fixed bugs
- Table: Switching theme looses table grid and intercell spacing. (issues #733
and #750)
## 3.2.5
#### Fixed bugs

View File

@@ -26,10 +26,12 @@ import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
@@ -151,19 +153,28 @@ public class FlatTableUI
if( rowHeight > 0 )
LookAndFeel.installProperty( table, "rowHeight", UIScale.scale( rowHeight ) );
if( !showHorizontalLines ) {
FlatTablePropertyWatcher watcher = FlatTablePropertyWatcher.get( table );
if( watcher != null )
watcher.enabled = false;
if( !showHorizontalLines && (watcher == null || !watcher.showHorizontalLinesChanged) ) {
oldShowHorizontalLines = table.getShowHorizontalLines();
table.setShowHorizontalLines( false );
}
if( !showVerticalLines ) {
if( !showVerticalLines && (watcher == null || !watcher.showVerticalLinesChanged) ) {
oldShowVerticalLines = table.getShowVerticalLines();
table.setShowVerticalLines( false );
}
if( intercellSpacing != null ) {
if( intercellSpacing != null && (watcher == null || !watcher.intercellSpacingChanged) ) {
oldIntercellSpacing = table.getIntercellSpacing();
table.setIntercellSpacing( intercellSpacing );
}
if( watcher != null )
watcher.enabled = true;
else
table.addPropertyChangeListener( new FlatTablePropertyWatcher() );
}
@Override
@@ -177,15 +188,25 @@ public class FlatTableUI
oldStyleValues = null;
FlatTablePropertyWatcher watcher = FlatTablePropertyWatcher.get( table );
if( watcher != null )
watcher.enabled = false;
// restore old show horizontal/vertical lines (if not modified)
if( !showHorizontalLines && oldShowHorizontalLines && !table.getShowHorizontalLines() )
table.setShowHorizontalLines( true );
if( !showVerticalLines && oldShowVerticalLines && !table.getShowVerticalLines() )
table.setShowVerticalLines( true );
if( !showHorizontalLines && oldShowHorizontalLines && !table.getShowHorizontalLines() &&
(watcher == null || !watcher.showHorizontalLinesChanged) )
table.setShowHorizontalLines( true );
if( !showVerticalLines && oldShowVerticalLines && !table.getShowVerticalLines() &&
(watcher == null || !watcher.showVerticalLinesChanged) )
table.setShowVerticalLines( true );
// restore old intercell spacing (if not modified)
if( intercellSpacing != null && table.getIntercellSpacing().equals( intercellSpacing ) )
table.setIntercellSpacing( oldIntercellSpacing );
if( intercellSpacing != null && table.getIntercellSpacing().equals( intercellSpacing ) &&
(watcher == null || !watcher.intercellSpacingChanged) )
table.setIntercellSpacing( oldIntercellSpacing );
if( watcher != null )
watcher.enabled = true;
}
@Override
@@ -467,4 +488,46 @@ public class FlatTableUI
}
}
}
//---- class FlatTablePropertyWatcher -------------------------------------
/**
* Listener that watches for change of some table properties from application code.
* This information is used in {@link FlatTableUI#installDefaults()} and
* {@link FlatTableUI#uninstallDefaults()} to decide whether FlatLaf modifies those properties.
* If they are modified in application code, FlatLaf no longer changes them.
*
* The listener is added once for each table, but never removed.
* So switching Laf/theme reuses existing listener.
*/
private static class FlatTablePropertyWatcher
implements PropertyChangeListener
{
boolean enabled = true;
boolean showHorizontalLinesChanged;
boolean showVerticalLinesChanged;
boolean intercellSpacingChanged;
static FlatTablePropertyWatcher get( JTable table ) {
for( PropertyChangeListener l : table.getPropertyChangeListeners() ) {
if( l instanceof FlatTablePropertyWatcher )
return (FlatTablePropertyWatcher) l;
}
return null;
}
//---- interface PropertyChangeListener ----
@Override
public void propertyChange( PropertyChangeEvent e ) {
if( !enabled )
return;
switch( e.getPropertyName() ) {
case "showHorizontalLines": showHorizontalLinesChanged = true; break;
case "showVerticalLines": showVerticalLinesChanged = true; break;
case "rowMargin": intercellSpacingChanged = true; break;
}
}
}
}

View File

@@ -18,7 +18,6 @@ package com.formdev.flatlaf.demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
@@ -116,15 +115,16 @@ class DataComponentsPanel
table1.setGridColor( redGridColorCheckBox.isSelected() ? Color.red : UIManager.getColor( "Table.gridColor" ) );
}
@Override
public void updateUI() {
super.updateUI();
private void showHorizontalLinesPropertyChange() {
showHorizontalLinesCheckBox.setSelected( table1.getShowHorizontalLines() );
}
EventQueue.invokeLater( () -> {
showHorizontalLinesChanged();
showVerticalLinesChanged();
intercellSpacingChanged();
} );
private void showVerticalLinesPropertyChange() {
showVerticalLinesCheckBox.setSelected( table1.getShowVerticalLines() );
}
private void intercellSpacingPropertyChange() {
intercellSpacingCheckBox.setSelected( table1.getRowMargin() != 0 );
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
@@ -333,10 +333,10 @@ class DataComponentsPanel
"Not editable", "Text", "Combo", "Combo Editable", "Integer", "Boolean"
}
) {
Class<?>[] columnTypes = new Class<?>[] {
Class<?>[] columnTypes = {
Object.class, Object.class, String.class, String.class, Integer.class, Boolean.class
};
boolean[] columnEditable = new boolean[] {
boolean[] columnEditable = {
false, true, true, true, true, true
};
@Override
@@ -383,6 +383,9 @@ class DataComponentsPanel
}
table1.setAutoCreateRowSorter(true);
table1.setComponentPopupMenu(popupMenu2);
table1.addPropertyChangeListener("showHorizontalLines", e -> showHorizontalLinesPropertyChange());
table1.addPropertyChangeListener("showVerticalLines", e -> showVerticalLinesPropertyChange());
table1.addPropertyChangeListener("rowMargin", e -> intercellSpacingPropertyChange());
scrollPane5.setViewportView(table1);
}
add(scrollPane5, "cell 1 3 3 1,width 300");

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "8.0.0.0.194" Java: "17.0.2" encoding: "UTF-8"
JFDML JFormDesigner: "8.2.0.0.331" Java: "21" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -333,6 +333,9 @@ new FormModel {
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.beans.PropertyChangeListener", "propertyChange", "showHorizontalLinesPropertyChange", false, "showHorizontalLines" ) )
addEvent( new FormEvent( "java.beans.PropertyChangeListener", "propertyChange", "showVerticalLinesPropertyChange", false, "showVerticalLines" ) )
addEvent( new FormEvent( "java.beans.PropertyChangeListener", "propertyChange", "intercellSpacingPropertyChange", false, "rowMargin" ) )
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3 3 1,width 300"