简体   繁体   English

清除JTextArea不起作用

[英]Clearing JTextArea not working

I'm trying to clear a text area that has something written on it. 我正在尝试清除上面写有文字的文本区域。 I've been trying to use the repaint() method, since I think that kind of resets the text area, but it hasn't been working. 我一直在尝试使用repaint()方法,因为我认为这种复位会重置文本区域,但是它没有起作用。

I'm using the text area along with a list. 我正在使用文本区域以及列表。 When members of the list are clicked, the said members will display on the text area. 单击列表的成员时,所述成员将显示在文本区域上。 So when they are "deselected", I need the previously-written members to disappear from the text area. 因此,当“取消选择”它们时,我需要先前编写的成员从文本区域中消失。

Here is the code for the valueChanged, which is where things happen: 这是valueChanged的代码,这是发生情况的地方:

public void valueChanged(ListSelectionEvent e)
{
        Object source = e.getSource();
        int[] indices = songList.getSelectedIndices();
        DecimalFormat df = new DecimalFormat("#0.00");

        Song[] selection = new Song[indices.length];
        for(int i = 0; i < indices.length; i++)
        {
            selection[i] = songCollection[indices[i]];
        }
        if(e.getValueIsAdjusting() == false)
        {
            for(int i = 0; i < selection.length; i++)
            {
                textArea.repaint(); //Shouldn't this work?
                textArea.append(selection[i].getTitle() + " " + selection[i].getArtist() + "    " + df.format(selection[i].getPrice()) + "\n" );

            }

        }               

}

PS, I'm pretty new to Stack Overflow, so if I did something wrong, feel free to tell me. PS,我对Stack Overflow还是很陌生,所以如果我做错了什么,请随时告诉我。

According to JTextComponent#setText , 根据JTextComponent#setText

Sets the text of this TextComponent to the specified text. 将此TextComponent的文本TextComponent为指定的文本。 If the text is null or empty, has the effect of simply deleting the old text. 如果文本为null或为空,则可以简单地删除旧文本。 When text has been inserted, the resulting caret location is determined by the implementation of the caret class. 插入文本后,最终插入符号的位置由插入符号类的实现确定。

So, to clear text from a JTextArea component, either do setText(null) , or setText("") . 因此,要清除JTextArea组件中的文本,请执行setText(null)setText("")

As said above. 如上所述。

The setText(" ") also works for text fields btw. setText(“”)也适用于文本字段btw。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM