Java JTextArea setText 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21838734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTextArea setText is not working
提问by Damian Lesniak
I have a problem with setting text in JTextArea, I tried setText(which I'd prefer) and append also. I do not know where the problem is, I got client-server app. I want to put message that server sends in JTextField but I am unable to here is my code:
我在 JTextArea 中设置文本时遇到问题,我尝试了 setText(我更喜欢)并附加。我不知道问题出在哪里,我得到了客户端 - 服务器应用程序。我想把服务器发送的消息放在 JTextField 中,但我无法在这里是我的代码:
client side code which is reciving the message properly:
正确接收消息的客户端代码:
try
{
Socket socket = new Socket("localhost", PORT);
BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.writeBytes(outputString + "\n");
inputString = serverInput.readLine(); // private String inputString
mymodel.setTextArea(inputString); // this is not working
System.out.println(inputString); // this is working
socket.close();
}
catch...
setTextArea method:
setTextArea 方法:
public void setTextArea(String string)
{
MyPanel mypanel = new MyPanel();
mypanel.textArea.setText(string); // debugger shows that the string contains message from server
}
I have set textarea to public since setter method weren't working, actually this one isn't working also. I do not know where the problem is, and debugger isn't helping me either.
由于 setter 方法不起作用,我已将 textarea 设置为 public,实际上这个方法也不起作用。我不知道问题出在哪里,调试器也没有帮助我。
Looking for your answers
寻找你的答案
EDIT:
编辑:
JTextTable code:
JTextTable 代码:
textArea = new JTextArea(1, 30);
textArea.setEditable(false);
panel.add(textArea, c);
采纳答案by mlethys
Try to get access through getter. Like
尝试通过 getter 获取访问权限。喜欢
public JTextArea getTextArea()
{
return jTextAreaField;
}
and then
进而
getTextArea().append("ur text");
回答by MadProgrammer
You're creating a new instance of MyPanel
each time the setTextArea
method is called, this means that what ever is on the screen isn't been used to apply the text you are sending it.
MyPanel
每次setTextArea
调用该方法时,您都会创建一个新实例,这意味着屏幕上的任何内容都不会用于应用您发送的文本。
Instead, you should use the original instance of MyPanel
that you created to show on the screen...
相反,您应该使用MyPanel
您创建的原始实例显示在屏幕上......
It's also impossible to determine if you are calling the blocking I/O from the content of the Event Dispatching Thread or interacting with the UI from a different thread. In either case, it's highly unrecommended
也无法确定您是从 Event Dispatching Thread 的内容调用阻塞 I/O,还是从不同线程与 UI 交互。在任何一种情况下,它都是非常不推荐的
Take a look at Concurrency in Swingfor more details
回答by MByD
There are two main problems:
主要有两个问题:
You perform the UI modification on the same thread as the IO, which you should never do. Consider working with
SwingWorker
for I/O operations.In
setTextArea
you are not accessing the instance ofMyPanel
that you already have, but creating a new one. So the changes are not done in theMyPanel
instance that you already have...
您在与 IO 相同的线程上执行 UI 修改,这是您永远不应该做的。考虑使用
SwingWorker
for I/O 操作。在
setTextArea
你没有访问的情况下MyPanel
,你已经有了,但创建一个新的。所以更改不会在MyPanel
您已经拥有的实例中完成...
回答by Benjamin
JTextArea t=new JTextArea();
t.append("Text");
no Method in java to set Text for JTextArea.
the append method to add text in JTextArea