changeset 19:c9961a1e1479

Scroll to top on new search
author Fox
date Sun, 10 Apr 2022 19:50:45 +0200
parents 60104bfcffff
children 981d634a05ac
files src/junotu/TabSimpleSearch.java
diffstat 1 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabSimpleSearch.java	Sun Apr 10 05:37:05 2022 +0200
+++ b/src/junotu/TabSimpleSearch.java	Sun Apr 10 19:50:45 2022 +0200
@@ -7,6 +7,7 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
+import javax.swing.SwingUtilities;
 import javax.swing.event.DocumentListener;
 import javax.swing.event.DocumentEvent;
 
@@ -18,6 +19,7 @@
 import javax.swing.JButton;
 import javax.swing.JTextField;
 import javax.swing.JScrollPane;
+import javax.swing.JScrollBar;
 
 import junotu.Main;
 import junotu.Window.Tab;
@@ -28,6 +30,8 @@
 
     private JTextField field;
     private Box results;
+
+    JScrollPane scroll;
     
     public TabSimpleSearch()
     {
@@ -37,7 +41,7 @@
 	field = new JTextField();
 	JButton create = new JButton("+");
 	results = Box.createVerticalBox();
-	JScrollPane scroll = new JScrollPane( results );
+	scroll = new JScrollPane( results );
 
 	field.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
 	create.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
@@ -94,6 +98,19 @@
     }
 
     public void search() {
+
+	if( !javax.swing.SwingUtilities.isEventDispatchThread() ) {
+	    SwingUtilities.invokeLater(
+				       new Runnable()
+				       {
+					   public void run()
+					   {
+					       search();
+					   }
+				       }
+				       );
+	    return;
+	}
 	
 	Card[] cards;
 
@@ -118,8 +135,25 @@
 	}
 	results.validate();
 	results.repaint();
+
+	/* Otherwise scrollup doesn't work. Perhaps because GUI needs to redraw first? */
+	SwingUtilities.invokeLater(
+				   new Runnable()
+				   {
+				       public void run()
+				       {
+					   scrollTop();
+				       }
+				   }
+				   );
     }
 
+    private void scrollTop()
+    {
+	JScrollBar scrollbar = scroll.getVerticalScrollBar();
+	scrollbar.setValue(0);
+    }
+    
     private void updateTitle()
     {
 	Window window = (Window)this.getTopLevelAncestor();