Browse Source

Fixed autoscrolling in the textpanes

git-svn-id: http://svn.dmdirc.com/trunk@142 00569f92-eb28-0410-84fd-f71c24880f
tags/0.1
Gregory Holmes 17 years ago
parent
commit
7b1ce06ba2

+ 21
- 9
src/uk/org/ownage/dmdirc/ui/ChannelFrame.java View File

@@ -52,9 +52,17 @@ public class ChannelFrame extends javax.swing.JInternalFrame {
52 52
      */
53 53
     private Border myborder;
54 54
     /**
55
-     * The northframe used when the frame is not maximised
55
+     * The dimensions of the titlebar of the frame
56 56
      **/
57
-    private Dimension titlebarSize;    
57
+    private Dimension titlebarSize;
58
+    /**
59
+     * whether to auto scroll the textarea when adding text
60
+     */
61
+    private boolean autoScroll = true;
62
+    /**
63
+     * holds the scrollbar for the frame
64
+     */
65
+    private JScrollBar scrollBar;
58 66
     
59 67
     /** Creates new form ChannelFrame */
60 68
     public ChannelFrame(Channel parent) {
@@ -65,14 +73,16 @@ public class ChannelFrame extends javax.swing.JInternalFrame {
65 73
         setMaximizable(true);
66 74
         setClosable(true);
67 75
         setVisible(true);
68
-        setResizable(true);        
76
+        setResizable(true);
77
+        
78
+        scrollBar = jScrollPane1.getVerticalScrollBar();
69 79
         
70 80
         jTextField1.addActionListener(new ActionListener() {
71 81
             public void actionPerformed(ActionEvent actionEvent) {
72 82
                 ChannelFrame.this.parent.sendLine(jTextField1.getText());
73 83
                 jTextField1.setText("");
74 84
             }
75
-        });        
85
+        });
76 86
         
77 87
         addPropertyChangeListener("maximum", new PropertyChangeListener() {
78 88
             public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
@@ -91,7 +101,7 @@ public class ChannelFrame extends javax.swing.JInternalFrame {
91 101
                     .setPreferredSize(ChannelFrame.this.titlebarSize);
92 102
                 }
93 103
             }
94
-        });        
104
+        });
95 105
     }
96 106
     
97 107
     /**
@@ -106,10 +116,12 @@ public class ChannelFrame extends javax.swing.JInternalFrame {
106 116
             ex.printStackTrace();
107 117
         }
108 118
         
109
-        jScrollPane1.invalidate();
110
-        JScrollBar bar = jScrollPane1.getVerticalScrollBar();
111
-        bar.setValue(bar.getMaximum());
112
-    }    
119
+        autoScroll = ((scrollBar.getValue() + scrollBar.getVisibleAmount())
120
+        != scrollBar.getMaximum());
121
+        if(autoScroll) {
122
+            jTextPane1.setCaretPosition(doc.getLength());
123
+        }
124
+    }
113 125
     
114 126
     public void updateNames(ArrayList<ChannelClientInfo> newNames) {
115 127
         nicklistModel.replace(newNames);

+ 20
- 5
src/uk/org/ownage/dmdirc/ui/ServerFrame.java View File

@@ -52,9 +52,17 @@ public class ServerFrame extends javax.swing.JInternalFrame {
52 52
      */
53 53
     private Border myborder;
54 54
     /**
55
-     * The northframe used when the frame is not maximised
55
+     * The dimensions of the titlebar of the frame
56 56
      **/
57 57
     private Dimension titlebarSize;
58
+    /**
59
+     * whether to auto scroll the textarea when adding text
60
+     */
61
+    private boolean autoScroll = true;
62
+    /**
63
+     * holds the scrollbar for the frame
64
+     */
65
+    private JScrollBar scrollBar;
58 66
     
59 67
     private ServerCommandParser commandParser;
60 68
     
@@ -70,6 +78,7 @@ public class ServerFrame extends javax.swing.JInternalFrame {
70 78
         setResizable(true);
71 79
         
72 80
         this.parent = parent;
81
+        scrollBar = jScrollPane1.getVerticalScrollBar();
73 82
         
74 83
         commandParser = new ServerCommandParser(parent);
75 84
         
@@ -103,7 +112,7 @@ public class ServerFrame extends javax.swing.JInternalFrame {
103 112
     
104 113
     /**
105 114
      * Adds a line of text to the main text area, and scrolls the text pane
106
-     * down so that it's visible
115
+     * down so that it's visible if the scrollbar is already at the bottom
107 116
      * @param line text to add
108 117
      */
109 118
     public void addLine(String line) {
@@ -114,9 +123,15 @@ public class ServerFrame extends javax.swing.JInternalFrame {
114 123
             ex.printStackTrace();
115 124
         }
116 125
         
117
-        jScrollPane1.invalidate();
118
-        JScrollBar bar = jScrollPane1.getVerticalScrollBar();
119
-        bar.setValue(bar.getMaximum());
126
+        autoScroll = ((scrollBar.getValue() + scrollBar.getVisibleAmount())
127
+        != scrollBar.getMaximum());
128
+        System.out.println("Value: \t\t"+scrollBar.getValue()+
129
+                "\r\nVisible \t"+scrollBar.getVisibleAmount()+
130
+                "\r\nMax: \t\t"+scrollBar.getMaximum()+
131
+                "\r\nAutoScrolling: \t"+autoScroll);
132
+        if(autoScroll) { 
133
+            jTextPane1.setCaretPosition(doc.getLength());
134
+        }
120 135
     }
121 136
     
122 137
     /** This method is called from within the constructor to

Loading…
Cancel
Save