JTextArea.select()不会选择任何内容

时间:2014-09-11 14:53:02

标签: java select user-interface jtextarea indexof

我在使用JTextArea的select函数时遇到了麻烦。如果变量填充正确,我用System.out.print()测试了变量。一切看起来都不错,但是选择功能根本不起作用。

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);
    }
}

3 个答案:

答案 0 :(得分:0)

自己解决了。 textarea需要在选择单词之前获得焦点!

textarea.requestFocus();

以下是带有新代码行的搜索功能:

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());
    }
}

答案 1 :(得分:0)

我测试了你的代码,它对我也没有用。问题是JComponent需要焦点才能显示任何光标选择。所以如果你只是打电话

textarea.requestFocusInWindow();

在选择&#39;之前方法,你应该是好的。如果您不希望突出显示依赖于焦点,我会使用我的评论中提到的荧光笔。

答案 2 :(得分:0)

textarea没有焦点。在选择文本之前添加textarea.requestFocus();

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