Frage

Hello I am having fun trying to build my own command line from a JTextArea.

I would like to have more control over the way the caret is handled depending on what key is pressed. I added to my JTextArea a KeyListener. My problem is: when the up-key is pressed and detected, the caret position should move. If I try to print the caret position in the KeyListener I get the previous one. The new freshly applied position will be taken into account after catching the keypressed event. Which concretely means, suppose the getCaretPosition() method returns 15. If I press the up arrow, I would still get 15 from getCaretPosition(). Only when I am out of the event listener the caret position would change.

I would like to get the new caret position inside the KeyListener. The current goal from this is to check whether the new caret position gets before the newline prompt. If so, then the caret should not move at all, because I don't want this area to be modifiable. So I would like to get the supposedly new caret position, then decide whether I want to apply this new position or not. How can I do that?

Here are chunks of my code:

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() + ".");

      }
}
War es hilfreich?

Lösung

Wrap your code called in the listener method in SwingUtilities.invokeLater()

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top