ScrollPane: fixed/improved border painting at 125% - 175% scaling to avoid different border thicknesses (issue #743)

This commit is contained in:
Karl Tauber
2024-06-14 18:13:17 +02:00
parent 2a494b1d60
commit 0c0d4bffbf
8 changed files with 126 additions and 15 deletions

View File

@@ -40,10 +40,15 @@ public class FlatPaintingHiDPITest
FlatPaintingHiDPITest() {
initComponents();
reset();
sliderChanged();
}
@Override
public void addNotify() {
super.addNotify();
reset();
}
private void sliderChanged() {
painter.originX = originXSlider.getValue();
painter.originY = originYSlider.getValue();
@@ -212,7 +217,7 @@ public class FlatPaintingHiDPITest
scaleXSlider.setPaintTicks(true);
scaleXSlider.setMajorTickSpacing(50);
scaleXSlider.setSnapToTicks(true);
scaleXSlider.setMinorTickSpacing(10);
scaleXSlider.setMinorTickSpacing(5);
scaleXSlider.setMinimum(-100);
scaleXSlider.addChangeListener(e -> sliderChanged());
add(scaleXSlider, "cell 1 4");
@@ -228,7 +233,7 @@ public class FlatPaintingHiDPITest
scaleYSlider.setPaintLabels(true);
scaleYSlider.setMajorTickSpacing(50);
scaleYSlider.setSnapToTicks(true);
scaleYSlider.setMinorTickSpacing(10);
scaleYSlider.setMinorTickSpacing(5);
scaleYSlider.setMinimum(-100);
scaleYSlider.addChangeListener(e -> sliderChanged());
add(scaleYSlider, "cell 1 5");

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "8.0.0.0.122" Java: "17.0.2" encoding: "UTF-8"
JFDML JFormDesigner: "8.2.3.0.386" Java: "21" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -108,7 +108,7 @@ new FormModel {
"paintTicks": true
"majorTickSpacing": 50
"snapToTicks": true
"minorTickSpacing": 10
"minorTickSpacing": 5
"minimum": -100
auxiliary() {
"JavaCodeGenerator.variableLocal": false
@@ -131,7 +131,7 @@ new FormModel {
"paintLabels": true
"majorTickSpacing": 50
"snapToTicks": true
"minorTickSpacing": 10
"minorTickSpacing": 5
"minimum": -100
auxiliary() {
"JavaCodeGenerator.variableLocal": false

View File

@@ -17,14 +17,19 @@
package com.formdev.flatlaf.testing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.tree.*;
@@ -227,6 +232,38 @@ public class FlatRoundedScrollPaneTest
}
}
private void emptyViewportChanged() {
boolean empty = emptyViewportCheckBox.isSelected();
for( JScrollPane scrollPane : allJScrollPanes ) {
JViewport viewport = scrollPane.getViewport();
Component view = viewport.getView();
if( empty ) {
scrollPane.putClientProperty( getClass().getName(), view );
JComponent emptyView = new JComponent() {
};
emptyView.setBorder( new EmptyViewBorder() );
emptyView.setFocusable( true );
emptyView.addMouseListener( new MouseAdapter() {
@Override
public void mousePressed( MouseEvent e ) {
emptyView.requestFocusInWindow();
}
} );
viewport.setView( emptyView );
} else {
Object oldView = scrollPane.getClientProperty( getClass().getName() );
scrollPane.putClientProperty( getClass().getName(), null );
if( oldView instanceof Component )
viewport.setView( (Component) oldView );
else
viewport.setView( null );
}
viewport.setOpaque( !empty );
scrollPane.revalidate();
scrollPane.repaint();
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
splitPane2 = new JSplitPane();
@@ -265,6 +302,7 @@ public class FlatRoundedScrollPaneTest
viewportBorderCheckBox = new JCheckBox();
rowHeaderCheckBox = new JCheckBox();
verticalScrollBarCheckBox = new JCheckBox();
emptyViewportCheckBox = new JCheckBox();
//======== this ========
setLayout(new MigLayout(
@@ -420,6 +458,7 @@ public class FlatRoundedScrollPaneTest
"[]",
// rows
"[]" +
"[]" +
"[]"));
//---- arcLabel ----
@@ -468,6 +507,11 @@ public class FlatRoundedScrollPaneTest
verticalScrollBarCheckBox.setSelected(true);
verticalScrollBarCheckBox.addActionListener(e -> verticalScrollBarChanged());
panel3.add(verticalScrollBarCheckBox, "cell 4 1");
//---- emptyViewportCheckBox ----
emptyViewportCheckBox.setText("Empty viewport");
emptyViewportCheckBox.addActionListener(e -> emptyViewportChanged());
panel3.add(emptyViewportCheckBox, "cell 2 2");
}
add(panel3, "cell 0 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
@@ -509,6 +553,7 @@ public class FlatRoundedScrollPaneTest
private JCheckBox viewportBorderCheckBox;
private JCheckBox rowHeaderCheckBox;
private JCheckBox verticalScrollBarCheckBox;
private JCheckBox emptyViewportCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables
//---- class Corner -------------------------------------------------------
@@ -525,4 +570,29 @@ public class FlatRoundedScrollPaneTest
// do not change background when checkbox "explicit colors" is selected
}
}
//---- class EmptyViewBorder ----------------------------------------------
private static class EmptyViewBorder
extends EmptyBorder
{
public EmptyViewBorder() {
super( 0, 0, 0, 0 );
}
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
g.setColor( Color.red );
int x2 = x + width - 1;
int y2 = y + height - 1;
for( int px = x; px <= x2; px += 4 ) {
g.fillRect( px, y, 1, 1 );
g.fillRect( px, y2, 1, 1 );
}
for( int py = y; py <= y2; py += 4 ) {
g.fillRect( x, py, 1, 1 );
g.fillRect( x2, py, 1, 1 );
}
}
}
}

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "8.1.1.0.298" Java: "19.0.2" encoding: "UTF-8"
JFDML JFormDesigner: "8.2.3.0.386" Java: "21" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -169,7 +169,7 @@ new FormModel {
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "hidemode 3"
"$columnConstraints": "[fill][grow,fill]para[][][]"
"$rowConstraints": "[][]"
"$rowConstraints": "[][][]"
} ) {
name: "panel3"
add( new FormComponent( "javax.swing.JLabel" ) {
@@ -238,6 +238,13 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "emptyViewportCheckBox"
"text": "Empty viewport"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "emptyViewportChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 2"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
} )