0

I have a JtextArea(in a scrollPane) in which i have 20 lines of text.

The display capacity of the textArea is 10 lines.

I need to move the 20 lines of text up by one page leaving a blank screen to type if user press enter.So if user scrolls up he will be able to see the 20 lines.

The functionality is similar to clear command in linux which will move all the contents up by one page.

How can i achieve this in JtextArea??

Please help.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
madhu
  • 37
  • 1
  • 10

1 Answers1

0

Try by adding \n * 20 times every time the user presses enter. You can do this by implementing a KeyListener, but some would not agree on using KeyListener, I do not see why not.

jTextArea.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER)
                    for (int i = 0; i < 20; i++)
                       jTextArea.append("\n");
            }
        });
fasaas
  • 592
  • 1
  • 6
  • 23
  • if (evt.getKeyCode() == KeyEvent.VK_ENTER) wrong, btw ENTER is implemented in JTextArea in API, – mKorbel May 08 '14 at 11:53
  • `I do not see why not.` because Swing was designed to be used with `Key Bindings`, which is a higher level API. It is always better to use a higher level API when available so you are not dependent on low level events. – camickr May 08 '14 at 15:00