JTextArea.select()不选择任何内容 [英] JTextArea.select() does not select anything

查看:226
本文介绍了JTextArea.select()不选择任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JTextArea的选择功能时遇到麻烦.我用System.out.print()测试了变量是否正确填充.一切似乎都不错,但是select函数根本无法正常工作.

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

推荐答案

我自己解决了这个问题.选择一个单词之前,textarea需要获得焦点!

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

这篇关于JTextArea.select()不选择任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆