JTextField. Find and highlight the word in a JTextArea

不打扰是莪最后的温柔 提交于 2019-12-22 21:21:40

问题


I wrote a code to find and highlight a word in a JTextArea and I'm hitting an issue and I'm too tired and with an headache to see my mistake. I have a search bar(TextField) where I can enter a word and the word gets highlighter in my TextArea. Problem is, after I press my "ENTER" key, the TextField gets deselected and I have to click it again to find the next word. What am I missing?

findfieldpage1 = new JTextField();
findfieldpage1.setBounds(37, 295, 63, 24);
gtapage1.add(findfieldpage1);

findfieldpage1.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent evt) {
        int code = evt.getKeyCode();
        if(code == KeyEvent.VK_ENTER){
            String find = findfieldpage1.getText().toLowerCase();
            textpage1.requestFocusInWindow();
            if (find != null && find.length() > 0) {
                Document document = textpage1.getDocument();
                int findLength = find.length();
                try {
                    boolean found = false;
                    if (pos + findLength > document.getLength()) {
                        pos = 0;
                    }
                while (pos + findLength <= document.getLength()) {
                    String match = document.getText(pos, findLength).toLowerCase();
                    if (match.equals(find)) {
                        found = true;
                        break;
                    }
                    pos++;
                }
                if (found) {
                    Rectangle viewRect = textpage1.modelToView(pos);
                    textpage1.scrollRectToVisible(viewRect);
                    textpage1.setCaretPosition(pos + findLength);
                    textpage1.moveCaretPosition(pos);
                    pos += findLength;
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
        }
    }
});

回答1:


Youre not transfering the focus back to the text field after the search is done

Add at the end: Jtextfield.requestfocus()




回答2:


Line 10 in your method is textpage1.requestFocusInWindow();, that's why it loses focus, because you're transferring it to the JTextArea.




回答3:


In the end, to avoid the hassle. I added a JButton and changed the keyPressed to an actionPerformed. So everytime I click that button, it finds and highlights me the string I enter in the TextField. Thank you for your help guys, I appreciate it.




回答4:


Add one more listener to textarea. After the focus is shifted to text area, this way it'll remain in textarea and the event (enter key press) etc will make the search happen.

/search filed begins/

    JTextField findtext = new JTextField();
    //findtext.setBounds(112,549, 63, 24);
    frame.add(findtext,BorderLayout.BEFORE_FIRST_LINE);

    findtext.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            int code = evt.getKeyCode();

            if(code == KeyEvent.VK_ENTER){
                String find = findtext.getText().toLowerCase();
                tarea.requestFocusInWindow();
                //findtext.requestFocus();
                //findtext.requestFocusInWindow();
                if (find != null && find.length()> 0) {
                    Document document = tarea.getDocument();
                    int findLength = find.length();
                    try {
                         //int pos=0;
                         boolean found = false;
                        if (pos + findLength > document.getLength()) {
                            pos = 0;
                        }
                    while (pos + findLength <= document.getLength()) {
                        String match = document.getText(pos, findLength).toLowerCase();
                        if (match.equals(find)) {
                            found = true;
                            break;
                        }
                        pos++;
                    }
                    if (found) {
                        Rectangle viewRect = tarea.modelToView(pos);
                        tarea.scrollRectToVisible(viewRect);
                        tarea.setCaretPosition(pos + findLength);
                        tarea.moveCaretPosition(pos);
                        pos += findLength;
                        //Thread.sleep(2000);
                      //  findtext.requestFocusInWindow();

                    }

                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }

                 }//findtext.requestFocusInWindow();
        }
    });

    /*control back to textarea*/
    tarea.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent evt) {
            int code = evt.getKeyCode();

            if(code == KeyEvent.VK_ENTER){
                String find = findtext.getText().toLowerCase();
                tarea.requestFocusInWindow();
                //findtext.requestFocus();
                //findtext.requestFocusInWindow();
                if (find != null && find.length()> 0) {
                    Document document = tarea.getDocument();
                    int findLength = find.length();
                    try {
                         //int pos=0;
                         boolean found = false;
                        if (pos + findLength > document.getLength()) {
                            pos = 0;
                        }
                    while (pos + findLength <= document.getLength()) {
                        String match = document.getText(pos, findLength).toLowerCase();
                        if (match.equals(find)) {
                            found = true;
                            break;
                        }
                        pos++;
                    }
                    if (found) {
                        Rectangle viewRect = tarea.modelToView(pos);
                        tarea.scrollRectToVisible(viewRect);
                        tarea.setCaretPosition(pos + findLength);
                        tarea.moveCaretPosition(pos);
                        pos += findLength;
                        //Thread.sleep(2000);
                      //  findtext.requestFocusInWindow();

                    }

                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }

                 }//findtext.requestFocusInWindow();
        }
    });


    /*search filed ends*/


来源:https://stackoverflow.com/questions/21025165/jtextfield-find-and-highlight-the-word-in-a-jtextarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!