From 762fe89867d1eb1a04cf202dc7a29bc68c60fe18 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 8 Aug 2020 16:26:14 +0200 Subject: [PATCH] FlatSmoothScrollingTest: added JTree, JTable, JTextArea, JTextPane and JEditorPane for testing smooth scrolling --- .../testing/FlatSmoothScrollingTest.java | 201 ++++++++++++++++-- .../testing/FlatSmoothScrollingTest.jfd | 96 ++++++++- 2 files changed, 275 insertions(+), 22 deletions(-) diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.java index 2c8e915c..616f65ce 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.java @@ -16,11 +16,15 @@ package com.formdev.flatlaf.testing; +import java.awt.Point; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.util.ArrayList; import java.util.Arrays; +import java.util.stream.Collectors; import javax.swing.*; +import javax.swing.table.AbstractTableModel; +import javax.swing.tree.*; import net.miginfocom.swing.*; /** @@ -40,8 +44,23 @@ public class FlatSmoothScrollingTest FlatSmoothScrollingTest() { initComponents(); - scrollPane1.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "list vert" ) ); - scrollPane1.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "list horz" ) ); + listScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "list vert" ) ); + listScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "list horz" ) ); + + treeScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "tree vert" ) ); + treeScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "tree horz" ) ); + + tableScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "table vert" ) ); + tableScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "table horz" ) ); + + textAreaScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "textArea vert" ) ); + textAreaScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "textArea horz" ) ); + + textPaneScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "textPane vert" ) ); + textPaneScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "textPane horz" ) ); + + editorPaneScrollPane.getVerticalScrollBar().addAdjustmentListener( new AdjustmentHandler( "editorPane vert" ) ); + editorPaneScrollPane.getHorizontalScrollBar().addAdjustmentListener( new AdjustmentHandler( "editorPane horz" ) ); ArrayList items = new ArrayList<>(); for( char ch = '0'; ch < 'z'; ch++ ) { @@ -50,17 +69,56 @@ public class FlatSmoothScrollingTest items.add( new String( chars ) ); } - list1.setModel( new AbstractListModel() { + // list model + list.setModel( new AbstractListModel() { @Override public int getSize() { return items.size(); } - @Override public String getElementAt( int index ) { return items.get( index ); } } ); + + // tree model + DefaultMutableTreeNode root = new DefaultMutableTreeNode( items.get( 0 ) ); + DefaultMutableTreeNode last = null; + for( int i = 1; i < items.size(); i++ ) { + DefaultMutableTreeNode node = new DefaultMutableTreeNode( items.get( i ) ); + if( i % 5 == 1 ) { + root.add( node ); + last = node; + } else + last.add( node ); + } + tree.setModel( new DefaultTreeModel( root ) ); + for( int row = tree.getRowCount() - 1; row >= 0; row-- ) + tree.expandRow( row ); + + // table model + table.setModel( new AbstractTableModel() { + @Override + public int getRowCount() { + return items.size(); + } + @Override + public int getColumnCount() { + return 4; + } + @Override + public Object getValueAt( int rowIndex, int columnIndex ) { + if( columnIndex > 0 ) + rowIndex = (items.size() + rowIndex - ((items.size() / 4) * columnIndex)) % items.size(); + return items.get( rowIndex ); + } + } ); + + // text components + String text = items.stream().collect( Collectors.joining( "\n" ) ); + textArea.setText( text ); + textPane.setText( text ); + editorPane.setText( text ); } private void smoothScrollingChanged() { @@ -71,42 +129,137 @@ public class FlatSmoothScrollingTest // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents smoothScrollingCheckBox = new JCheckBox(); listLabel = new JLabel(); - scrollPane1 = new JScrollPane(); - list1 = new JList<>(); + label1 = new JLabel(); + label5 = new JLabel(); + listScrollPane = new FlatSmoothScrollingTest.DebugScrollPane(); + list = new JList<>(); + treeScrollPane = new FlatSmoothScrollingTest.DebugScrollPane(); + tree = new JTree(); + tableScrollPane = new JScrollPane(); + table = new JTable(); + label2 = new JLabel(); + label3 = new JLabel(); + label4 = new JLabel(); + textAreaScrollPane = new FlatSmoothScrollingTest.DebugScrollPane(); + textArea = new JTextArea(); + textPaneScrollPane = new FlatSmoothScrollingTest.DebugScrollPane(); + textPane = new JTextPane(); + editorPaneScrollPane = new FlatSmoothScrollingTest.DebugScrollPane(); + editorPane = new JEditorPane(); //======== this ======== setLayout(new MigLayout( "ltr,insets dialog,hidemode 3", // columns - "[]" + - "[200]", + "[200,fill]" + + "[200,fill]" + + "[200,fill]" + + "[200,fill]", // rows "[]" + - "[::200,grow,fill]")); + "[]" + + "[200,grow,fill]" + + "[]" + + "[200,grow,fill]")); //---- smoothScrollingCheckBox ---- smoothScrollingCheckBox.setText("Smooth scrolling"); smoothScrollingCheckBox.setSelected(true); smoothScrollingCheckBox.addActionListener(e -> smoothScrollingChanged()); - add(smoothScrollingCheckBox, "cell 0 0 2 1,alignx left,growx 0"); + add(smoothScrollingCheckBox, "cell 0 0,alignx left,growx 0"); //---- listLabel ---- listLabel.setText("JList:"); add(listLabel, "cell 0 1,aligny top,growy 0"); - //======== scrollPane1 ======== + //---- label1 ---- + label1.setText("JTree:"); + add(label1, "cell 1 1"); + + //---- label5 ---- + label5.setText("JTable:"); + add(label5, "cell 2 1"); + + //======== listScrollPane ======== { - scrollPane1.setViewportView(list1); + listScrollPane.setViewportView(list); } - add(scrollPane1, "cell 1 1,growx"); + add(listScrollPane, "cell 0 2,growx"); + + //======== treeScrollPane ======== + { + + //---- tree ---- + tree.setModel(new DefaultTreeModel( + new DefaultMutableTreeNode("root") { + { + add(new DefaultMutableTreeNode("a")); + add(new DefaultMutableTreeNode("b")); + add(new DefaultMutableTreeNode("c")); + } + })); + treeScrollPane.setViewportView(tree); + } + add(treeScrollPane, "cell 1 2"); + + //======== tableScrollPane ======== + { + tableScrollPane.setViewportView(table); + } + add(tableScrollPane, "cell 2 2 2 1,width 100,height 100"); + + //---- label2 ---- + label2.setText("JTextArea:"); + add(label2, "cell 0 3"); + + //---- label3 ---- + label3.setText("JTextPane:"); + add(label3, "cell 1 3"); + + //---- label4 ---- + label4.setText("JEditorPane:"); + add(label4, "cell 2 3"); + + //======== textAreaScrollPane ======== + { + textAreaScrollPane.setViewportView(textArea); + } + add(textAreaScrollPane, "cell 0 4"); + + //======== textPaneScrollPane ======== + { + textPaneScrollPane.setViewportView(textPane); + } + add(textPaneScrollPane, "cell 1 4"); + + //======== editorPaneScrollPane ======== + { + editorPaneScrollPane.setViewportView(editorPane); + } + add(editorPaneScrollPane, "cell 2 4"); // JFormDesigner - End of component initialization //GEN-END:initComponents } // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables private JCheckBox smoothScrollingCheckBox; private JLabel listLabel; - private JScrollPane scrollPane1; - private JList list1; + private JLabel label1; + private JLabel label5; + private FlatSmoothScrollingTest.DebugScrollPane listScrollPane; + private JList list; + private FlatSmoothScrollingTest.DebugScrollPane treeScrollPane; + private JTree tree; + private JScrollPane tableScrollPane; + private JTable table; + private JLabel label2; + private JLabel label3; + private JLabel label4; + private FlatSmoothScrollingTest.DebugScrollPane textAreaScrollPane; + private JTextArea textArea; + private FlatSmoothScrollingTest.DebugScrollPane textPaneScrollPane; + private JTextPane textPane; + private FlatSmoothScrollingTest.DebugScrollPane editorPaneScrollPane; + private JEditorPane editorPane; // JFormDesigner - End of variables declaration //GEN-END:variables //---- class AdjustmentHandler -------------------------------------------- @@ -141,4 +294,22 @@ public class FlatSmoothScrollingTest } } } + + //---- class DebugViewport ------------------------------------------------ + + private static class DebugScrollPane + extends JScrollPane + { + @Override + protected JViewport createViewport() { + return new JViewport() { + @Override + public Point getViewPosition() { + Point viewPosition = super.getViewPosition(); + System.out.println( " viewPosition " + viewPosition.x + "," + viewPosition.y ); + return viewPosition; + } + }; + } + } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.jfd index 39231291..a2937f27 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatSmoothScrollingTest.jfd @@ -5,8 +5,8 @@ new FormModel { root: new FormRoot { add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$layoutConstraints": "ltr,insets dialog,hidemode 3" - "$columnConstraints": "[][200]" - "$rowConstraints": "[][::200,grow,fill]" + "$columnConstraints": "[200,fill][200,fill][200,fill][200,fill]" + "$rowConstraints": "[][][200,grow,fill][][200,grow,fill]" } ) { name: "this" add( new FormComponent( "javax.swing.JCheckBox" ) { @@ -15,7 +15,7 @@ new FormModel { "selected": true addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "smoothScrollingChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 0 2 1,alignx left,growx 0" + "value": "cell 0 0,alignx left,growx 0" } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "listLabel" @@ -23,17 +23,99 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 1,aligny top,growy 0" } ) - add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { - name: "scrollPane1" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label1" + "text": "JTree:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 1" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label5" + "text": "JTable:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 1" + } ) + add( new FormContainer( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$DebugScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "listScrollPane" add( new FormComponent( "javax.swing.JList" ) { - name: "list1" + name: "list" auxiliary() { "JavaCodeGenerator.typeParameters": "String" "JavaCodeGenerator.variableLocal": false } } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 1 1,growx" + "value": "cell 0 2,growx" + } ) + add( new FormContainer( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$DebugScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "treeScrollPane" + add( new FormComponent( "javax.swing.JTree" ) { + name: "tree" + "model": new javax.swing.tree.DefaultTreeModel( new javax.swing.tree.DefaultMutableTreeNode { + userObject: "root" + add( new javax.swing.tree.DefaultMutableTreeNode { + userObject: "a" + } ) + add( new javax.swing.tree.DefaultMutableTreeNode { + userObject: "b" + } ) + add( new javax.swing.tree.DefaultMutableTreeNode { + userObject: "c" + } ) + } ) + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 2" + } ) + add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "tableScrollPane" + add( new FormComponent( "javax.swing.JTable" ) { + name: "table" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 2 2 1,width 100,height 100" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label2" + "text": "JTextArea:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 3" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label3" + "text": "JTextPane:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 3" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "label4" + "text": "JEditorPane:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 3" + } ) + add( new FormContainer( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$DebugScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "textAreaScrollPane" + add( new FormComponent( "javax.swing.JTextArea" ) { + name: "textArea" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 4" + } ) + add( new FormContainer( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$DebugScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "textPaneScrollPane" + add( new FormComponent( "javax.swing.JTextPane" ) { + name: "textPane" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 4" + } ) + add( new FormContainer( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$DebugScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) { + name: "editorPaneScrollPane" + add( new FormComponent( "javax.swing.JEditorPane" ) { + name: "editorPane" + } ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 4" } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 )