Table: Fixed painting of alternating rows below table if auto-resize mode is JTable.AUTO_RESIZE_OFF and table is smaller than scroll pane. Below alternating rows were not updated when table width changed and were painted on wrong side in right-to-left component orientation

This commit is contained in:
Karl Tauber
2024-06-04 13:34:49 +02:00
parent 9ade48d078
commit 1463723e52
2 changed files with 62 additions and 5 deletions

View File

@@ -23,6 +23,10 @@ FlatLaf Change Log
`<big>`, `<small>` and `<samp>` in HTML text for components Button, CheckBox, `<big>`, `<small>` and `<samp>` in HTML text for components Button, CheckBox,
RadioButton, MenuItem (and subclasses), JideLabel, JideButton, JXBusyLabel and RadioButton, MenuItem (and subclasses), JideLabel, JideButton, JXBusyLabel and
JXHyperlink. Also fixed for Label and ToolTip if using Java 11+. JXHyperlink. Also fixed for Label and ToolTip if using Java 11+.
- Table: Fixed painting of alternating rows below table if auto-resize mode is
`JTable.AUTO_RESIZE_OFF` and table width is smaller than scroll pane (was not
updated when table width changed and was painted on wrong side in
right-to-left component orientation).
- Theme Editor: Fixed occasional empty window on startup on macOS. - Theme Editor: Fixed occasional empty window on startup on macOS.
#### Incompatibilities #### Incompatibilities
@@ -88,8 +92,8 @@ FlatLaf Change Log
- Improved log messages for loading fails. - Improved log messages for loading fails.
- Fonts: Updated **Inter** to - Fonts: Updated **Inter** to
[v4.0](https://github.com/rsms/inter/releases/tag/v4.0). [v4.0](https://github.com/rsms/inter/releases/tag/v4.0).
- Table: Select all text in cell editor when starting editing using `F2` key. - Table: Select all text in cell editor when starting editing using `F2` key on
(issue #652) Windows or Linux. (issue #652)
#### Fixed bugs #### Fixed bugs

View File

@@ -25,6 +25,9 @@ import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
@@ -132,6 +135,7 @@ public class FlatTableUI
private TableCellRenderer oldBooleanRenderer; private TableCellRenderer oldBooleanRenderer;
private PropertyChangeListener propertyChangeListener; private PropertyChangeListener propertyChangeListener;
private ComponentListener outsideAlternateRowsListener;
private Map<String, Object> oldStyleValues; private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
@@ -266,6 +270,11 @@ public class FlatTableUI
table.removePropertyChangeListener( propertyChangeListener ); table.removePropertyChangeListener( propertyChangeListener );
propertyChangeListener = null; propertyChangeListener = null;
if( outsideAlternateRowsListener != null ) {
table.removeComponentListener( outsideAlternateRowsListener );
outsideAlternateRowsListener = null;
}
} }
@Override @Override
@@ -513,8 +522,6 @@ public class FlatTableUI
boolean paintOutside = UIManager.getBoolean( "Table.paintOutsideAlternateRows" ); boolean paintOutside = UIManager.getBoolean( "Table.paintOutsideAlternateRows" );
Color alternateColor; Color alternateColor;
if( paintOutside && (alternateColor = UIManager.getColor( "Table.alternateRowColor" )) != null ) { if( paintOutside && (alternateColor = UIManager.getColor( "Table.alternateRowColor" )) != null ) {
g.setColor( alternateColor );
int rowCount = table.getRowCount(); int rowCount = table.getRowCount();
// paint alternating empty rows below the table // paint alternating empty rows below the table
@@ -523,10 +530,56 @@ public class FlatTableUI
int tableWidth = table.getWidth(); int tableWidth = table.getWidth();
int rowHeight = table.getRowHeight(); int rowHeight = table.getRowHeight();
g.setColor( alternateColor );
int x = viewport.getComponentOrientation().isLeftToRight() ? 0 : viewportWidth - tableWidth;
for( int y = tableHeight, row = rowCount; y < viewportHeight; y += rowHeight, row++ ) { for( int y = tableHeight, row = rowCount; y < viewportHeight; y += rowHeight, row++ ) {
if( row % 2 != 0 ) if( row % 2 != 0 )
g.fillRect( 0, y, tableWidth, rowHeight ); g.fillRect( x, y, tableWidth, rowHeight );
} }
// add listener on demand
if( outsideAlternateRowsListener == null && table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF ) {
outsideAlternateRowsListener = new FlatOutsideAlternateRowsListener();
table.addComponentListener( outsideAlternateRowsListener );
}
}
}
}
//---- class OutsideAlternateRowsListener ---------------------------------
/**
* Used if table auto-resize-mode is off to repaint outside alternate rows
* when table width changed (column resized) or component orientation changed.
*/
private class FlatOutsideAlternateRowsListener
extends ComponentAdapter
{
@Override
public void componentHidden( ComponentEvent e ) {
Container viewport = SwingUtilities.getUnwrappedParent( table );
if( viewport instanceof JViewport )
viewport.repaint();
}
@Override
public void componentMoved( ComponentEvent e ) {
repaintAreaBelowTable();
}
@Override
public void componentResized( ComponentEvent e ) {
repaintAreaBelowTable();
}
private void repaintAreaBelowTable() {
Container viewport = SwingUtilities.getUnwrappedParent( table );
if( viewport instanceof JViewport ) {
int viewportHeight = viewport.getHeight();
int tableHeight = table.getHeight();
if( tableHeight < viewportHeight )
viewport.repaint( 0, tableHeight, viewport.getWidth(), viewportHeight - tableHeight );
} }
} }
} }