2

I'm developing a small calculator widget that keeps a running log of calculations. It's supposed to scroll to the bottom of the log every time a new entry is added. This part seems to be working fine. The problem is, when I press a calculator button that does not add to the log, the log pane always scrolls back to the top, and the scrollbar disappears. How can I keep it from doing this?

The code that adds to the log is:

    private JTextPane logArea; //This is placed inside a JScrollPane
    private void log(String m, SimpleAttributeSet a) {
       int len = logArea.getDocument().getLength();
       logArea.setEditable(true);
       logArea.setCaretPosition(len);
       logArea.setCharacterAttributes(a, false);
       logArea.replaceSelection(m);
       logArea.scrollRectToVisible(new Rectangle(0,logArea.getBounds(null).height,1,1));
       logArea.setEditable(false);
   }

The code that seems to be messing with the scroll is:

  private void addDigit(char digit) {
       if (clearDisplayBeforeDigit) {
          clearNumDisplay();
       }
       if (numInDisplay.getText().length() < maxNumDigits) {
          if (digit == '.') { //Point
             if (!hasPoint) { //Only one point allowed
                hasPoint = true;
                String newText = numInDisplay.getText() + ".";
                numInDisplay.setText(newText);
             }
          } else { //New digit
             String newText = numInDisplay.getText() + digit;
             numInDisplay.setText(newText);
          }
       }
    }
4b0
  • 21,981
  • 30
  • 95
  • 142
Leonide
  • 235
  • 3
  • 11
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 12 '12 at 04:03
  • Where is `log` called from? You might also consider using the `JScrollPane`'s `JScrollBar` to scroll to the bottom. ([Link](http://stackoverflow.com/a/5150437/758280), I have no idea if that will solve your problem, but it can't hurt to try.) – Jeffrey Feb 12 '12 at 04:06

1 Answers1

3

The code you think is causing the problem doesn't even reference the logArea, so why would you think this causes the problem?

You don't need to use the scrollRectToVisible(...) method. The setCaretPosition(...) should do the trick. Although you should get the length of the document and invoke that method AFTER you update the document.

Check out Text Area Scrolling for more information.

Edit:

I also don't see any reason for changing the editability of the text area.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • +1, that setCaretPosition(...) has to be invoked at the end for the trick to work. – nIcE cOw Feb 12 '12 at 04:17
  • I thought it was the problem because every time addDigit is called, whether by pressing a button, or through a keyboard shortcut, the scrollpane goes back to the top. But maybe it's a coincidence, and the problem has to do with focus? The buttons are on a different JPanel that the log. Would the change of focus cause this problem? – Leonide Feb 12 '12 at 11:22
  • Here's a pared-down version that should show the effect I'm trying to get rid of: Here's a pared-down version that should show the effect I'm trying to get rid of: [link](http://www.tenkuukai.com/testing/JCalculator.java) [link](http://www.tenkuukai.com/testing/JCalculatorDemo.java) – Leonide Feb 12 '12 at 11:57