JTextArea.select() does not select anything

泄露秘密 提交于 2019-12-01 12:34:53

问题


I am having troubles using the select function of a JTextArea. I tested the variables with System.out.print() if they are filled correctly. Everything seems good but the select function does not work at all.

public class exercises extends JFrame {
    JTextField tf_search;
    String searchstr;
    JTextArea textarea;
    String aktStr;
    int Index;

    public exercises(String title) {
        super(title);
        setLayout(new FlowLayout());
        JComboBox<String> combo = new JComboBox<String>();
        combo.addItem("Here stands the whole Shit");
        String[] systemfonts = new String[200];
        systemfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (String s : systemfonts) {
            combo.addItem(s);
        }

        textarea = new JTextArea(10, 40);
        String examplestr = "Here I only test how often the word olo is in the text. "
                + "\nI'll add olo as often as I like. olo sounds like lol olo";
        textarea.setText(examplestr);
        JPanel p_search = new JPanel();

        tf_search = new JTextField();
        JButton b_search = new JButton("Search");
        //JButton b_weitersuchen = new JButton("weiter suchen"); // I also want to implement a     function to keep on searching for more appereances of the word that is searched
        p_search.add(b_search);
        //p_suchen.add(b_weitersuchen);
        p_search.add(tf_search);
        p_search.setLayout(new GridLayout(3, 1));
        //b_weitersuchen.addActionListener(new MeinActionLauscher());
        b_search.addActionListener(new MyActionListener());
        add(p_search);
        add(textarea);
        add(combo);
    }

    class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String label;
            label = e.getActionCommand();
            if (label.equals("Search")) {
                search();
            }
    //  if(label.equals("weiter suchen"))
            //  weiterSuchen();
        }
    }

    void search() {
        searchstr = tf_search.getText();
        if (searchstr == null) {
            return;
        }

        aktStr = textarea.getText();
        Index = aktStr.indexOf(searchstr);

        if (Index == -1) {
            JOptionPane.showMessageDialog(null, "String not found", "Dialog", JOptionPane.INFORMATION_MESSAGE);
        } else {
            textarea.select(Index, Index + searchstr.length());
        }
    }

    public static void main(String[] args) {
        exercises bla = new exercises("ComboBox Test");
        bla.pack();
        bla.setVisible(true);
        bla.setSize(700, 700);
    }
}

回答1:


Solved it myself. The textarea needs to get the focus right before selecting a word!

textarea.requestFocus();

Here is the search function with the new line of code:

void search() {
    searchstr = tf_search.getText();
    if (searchstr == null) {
        return;
    }

    aktStr = textarea.getText();
    Index = aktStr.indexOf(searchstr);

    if (Index == -1) {
        JOptionPane.showMessageDialog(null, "String not found", "Dialog", JOptionPane.INFORMATION_MESSAGE);
    } else {
        textarea.requestFocus();
        textarea.select(Index, Index + searchstr.length());
    }
}



回答2:


I tested you code and it didn't work for me either. The issue is that the JComponent needs focus in order to show any cursor selections. So if you just call

textarea.requestFocusInWindow();

right before the 'select' method, you should be good. If you don't want the highlighting to be dependent on focus I would go with using the highlighter mentioned in my comments.




回答3:


The textarea has not the focus. Add textarea.requestFocus(); before you select the text:

    } else {
        textarea.requestFocus();
        textarea.select(Index, Index + searchstr.length());
    }


来源:https://stackoverflow.com/questions/25790702/jtextarea-select-does-not-select-anything

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