听JTextArea按下的键

时间:2014-04-09 10:43:06

标签: java swing jtextarea keylistener caret

您好,我很乐意尝试从JTextArea构建自己的命令行。

我希望能够更好地控制插入符号的处理方式,具体取决于按下的键。我在JTextArea中添加了一个KeyListener。我的问题是:当按下并检测到向上键时,插入位置应该移动。如果我尝试在KeyListener中打印插入位置,我会得到前一个。在捕获按键事件后,将考虑新应用的新位置。具体意味着,假设getCaretPosition()方法返回15.如果我按向上箭头,我仍然会从getCaretPosition()得到15。只有当我离开事件听众时,插入位置才会改变。

我想在KeyListener中获得新的插入位置。 目前的目标是检查新插入符号位置是否在换行符提示之前。如果是这样,则插入符不应该移动,因为我不希望这个区域可以修改。 所以我想获得所谓的新插入位置,然后决定是否要申请这个新职位。 我怎样才能做到这一点?

以下是我的代码块:

public MethodCommandLine() {
      JFrame mainFrame = new JFrame();
      mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
      mainFrame.setLocation(200, 200);

      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      prompt = new JTextArea(PROMPT);
      prompt.setCaretPosition(PROMPT.length());

      lastCmdPosition = prompt.getCaretPosition();

      prompt.setPreferredSize(new Dimension(300, 500));
      prompt.addKeyListener(new KeyMonitor());

      JScrollPane js = new JScrollPane(prompt);
      js.setViewportView(this.prompt);
      js.setVisible(true);

      mainFrame.getContentPane().add(js);

      mainFrame.pack();
      mainFrame.setVisible(true);

      this.formattedFields = new FormattedFields().getFField();

      init();
}



private class KeyMonitor extends KeyAdapter {
      String commandRcvd;

      public void keyPressed(KeyEvent e) {
             System.out.println("----key pressed: ." + e.getKeyChar() + ".");

          String bak = prompt.getText();
          System.out.println("\nprevious content: \n\t" + bak);

          System.out.println("\nCaret position: " + prompt.getCaretPosition());
                      // HERE: no matter which key is pressed, the former caret position will be returned. I would like to get there the NEW supposedly caret position.

          if(prompt.getCaretPosition() <= lastCmdPosition) {
              return;
          }
          if(e.getKeyCode() == KeyEvent.VK_TAB) {
              if(prompt.getText().charAt(prompt.getCaretPosition() -1) == ' ') {
                  prompt.setText(prompt.getText() + "\n" + printAvailableEntries());
              }
              else {
                  System.out.println("." + prompt.getText().charAt(prompt.getCaretPosition() -1 ) + ".");

                  System.out.println(getCurrentWord());
              }
          }

          if(e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
              if(prompt.getText().charAt(prompt.getCaretPosition() - 2) == '>') {
                  e.consume();
              }
          }
          if(e.getKeyCode() == KeyEvent.VK_ENTER) {


                /*
                int lines = prompt.getLineCount(); 
                System.out.println("getLineCount: " + lines);
                try {
                System.out.println("getLineEndOffset: " + prompt.getLineEndOffset(lines -1));
                System.out.println("getLineOfOffset: " + prompt.getLineOfOffset(lines -1));

                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }*/


              commandRcvd = prompt.getText().substring(lastCmdPosition);
              System.out.println("last command : ." + commandRcvd + ".");
              prompt.setText(prompt.getText() + "\n" + PROMPT);
              lastCmdPosition = prompt.getCaretPosition();
              e.consume();
         }
         if(e.getKeyCode() == KeyEvent.VK_LEFT) {
             if(prompt.getCaretPosition() -1 < lastCmdPosition) {
                 System.out.println("out : caret=" + prompt.getCaretPosition() + "\tlast=" + lastCmdPosition);
                 prompt.setCaretPosition(lastCmdPosition);
             }
             e.consume();
         }
         if(e.getKeyCode() == KeyEvent.VK_UP) {
             prompt.setCaretPosition(prompt.getCaretPosition());
         }

         System.out.println("\ncurrent content: ." + prompt.getText() + ".");

      }
}

1 个答案:

答案 0 :(得分:1)

SwingUtilities.invokeLater()

中的侦听器方法中包含您调用的代码