Skip to content

Commit

Permalink
Table: fixed unstable grid line thickness when scaled on HiDPI screen…
Browse files Browse the repository at this point in the history
…s (issue #152)
  • Loading branch information
DevCharly committed Nov 28, 2020
1 parent aefed7c commit 5ef0c9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ FlatLaf Change Log
property `JTabbedPane.hideTabAreaWithOneTab` to `true`)
- Table: Do not paint last vertical grid line if auto-resize mode is not off.
(issue #46)
- Table: Fixed unstable grid line thickness when scaled on HiDPI screens. (issue
#152)


#### Fixed bugs
Expand Down
26 changes: 23 additions & 3 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.Graphics2D;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JTable;
Expand Down Expand Up @@ -210,25 +211,32 @@ private void toggleSelectionColors() {

@Override
public void paint( Graphics g, JComponent c ) {
if( table.getShowVerticalLines() ) {
boolean horizontalLines = table.getShowHorizontalLines();
boolean verticalLines = table.getShowVerticalLines();
if( horizontalLines || verticalLines ) {
// fix grid painting issues in BasicTableUI
// - do not paint last vertical grid line if auto-resize mode is not off
// - in right-to-left component orientation, do not paint last vertical grid line
// in any auto-resize mode; can not paint on left side of table because
// cells are painted over left line
// - fix unstable grid line thickness when scaled at 125%, 150%, 175%, 225%, ...
// which paints either 1px or 2px lines depending on location

boolean hideLastVerticalLine =
table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF ||
!table.getComponentOrientation().isLeftToRight();
int tableWidth = table.getWidth();

double systemScaleFactor = UIScale.getSystemScaleFactor( (Graphics2D) g );
double lineThickness = (1. / systemScaleFactor) * (int) systemScaleFactor;

// Java 8 uses drawLine() to paint grid lines
// Java 9+ uses fillRect() to paint grid lines
g = new Graphics2DProxy( (Graphics2D) g ) {
@Override
public void drawLine( int x1, int y1, int x2, int y2 ) {
// do not paint last vertical line
if( hideLastVerticalLine &&
if( hideLastVerticalLine && verticalLines &&
x1 == x2 && y1 == 0 && x1 == tableWidth - 1 &&
wasInvokedFromPaintGrid() )
return;
Expand All @@ -239,11 +247,23 @@ public void drawLine( int x1, int y1, int x2, int y2 ) {
@Override
public void fillRect( int x, int y, int width, int height ) {
// do not paint last vertical line
if( hideLastVerticalLine &&
if( hideLastVerticalLine && verticalLines &&
width == 1 && y == 0 && x == tableWidth - 1 &&
wasInvokedFromPaintGrid() )
return;

// reduce line thickness to avoid unstable painted line thickness
if( lineThickness != 1 ) {
if( horizontalLines && height == 1 && wasInvokedFromPaintGrid() ) {
super.fill( new Rectangle2D.Double( x, y, width, lineThickness ) );
return;
}
if( verticalLines && width == 1 && y == 0 && wasInvokedFromPaintGrid() ) {
super.fill( new Rectangle2D.Double( x, y, lineThickness, height ) );
return;
}
}

super.fillRect( x, y, width, height );
}

Expand Down

0 comments on commit 5ef0c9a

Please sign in to comment.