changeset 13:ecc0cb60b845

Scroll to top when opening a card to edit
author Fox
date Sat, 09 Apr 2022 14:11:43 +0200
parents b4fd74d0680d
children d90a9b1065d1
files src/junotu/TabEdit.java
diffstat 1 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabEdit.java	Fri Apr 08 23:58:44 2022 +0200
+++ b/src/junotu/TabEdit.java	Sat Apr 09 14:11:43 2022 +0200
@@ -5,6 +5,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;
 
@@ -17,6 +18,7 @@
 import javax.swing.JTextField;
 import javax.swing.JTextArea;
 import javax.swing.JScrollPane;
+import javax.swing.JScrollBar;
 
 import junotu.Main;
 import junotu.Database;
@@ -28,17 +30,19 @@
     private boolean newCard = true;
     private long identifier = -1;
 
+    private JScrollPane scroll;
+    
     private JTextField title;
     private JTextArea content;
 
-    JButton delete;
+    private JButton delete;
     
     public TabEdit()
     {
 	this.setLayout( new BorderLayout() );
 
 	Box scrollContent = Box.createVerticalBox();
-	JScrollPane scroll = new JScrollPane( scrollContent );
+	scroll = new JScrollPane( scrollContent );
         title = new JTextField();
 	content = new JTextArea();
 
@@ -120,6 +124,19 @@
 	
     }
 
+    private void scrollTop()
+    {
+	JScrollBar scrollbar = scroll.getVerticalScrollBar();
+	scrollbar.setValue(0);
+    }
+
+    private void scrollBottom()
+    {
+	JScrollBar scrollbar = scroll.getVerticalScrollBar();
+	int maximum = scrollbar.getMaximum()-scrollbar.getVisibleAmount();
+	scrollbar.setValue(maximum);
+    }
+    
     public void cardCreate()
     {
 	newCard = true;
@@ -136,6 +153,15 @@
 	content.setText( card.content );
 	updateTitle();
 	delete.setVisible(true);
+	SwingUtilities.invokeLater(
+				   new Runnable()
+				   {
+				       public void run()
+				       {
+					   scrollTop();
+				       }
+				   }
+				   );
     }
 
     private void clearWidgets()