5
I’m having trouble using a Jtextarea. I need that every time something happens on my system, information is added in real time. However, he appears nothing.
I’ve tried using the setText()
and the append()
, but neither worked. However, if I do it here
JOptionpane.showMessageDialog(null, JtextArea.getText())
It returns to me everything that I had written before but that was not appearing in it.
I’ve tried to use repaint()
also and nothing.
Someone’s been through it?
Here’s a piece of code. When I click on a button, it has this call:
if (!isStartServer()) {
area.append("Iniciando servidor de aplicação...\n");
Integer portaSocket = Integer.parseInt(tfPorta.getText());
Integer limite = Integer.parseInt(tfLimite.getText());
setStartServer(true);
StartServidor s = new StartServidor(portaSocket, limite, this);
new Thread(s).start();
btStart.setText("Stop Server");
}else{
System.exit(0);
}
In this way, Startservidor became a Thread, which is usually where I write messages in the Jtextarea. When I didn’t have this "Startservidor" as a Thread, it didn’t update the Jtextarea when I called writeTextArea(String msg);
Code of the Startservidor:
public void run() {
setPortaSocket(getPortaSocket());
setLimite(getLimite());
try {
setServerSocket(new ServerSocket(getPortaSocket()));
writeTextArea("Ouvindo a porta " + getPortaSocket() + "...");
while (servidor.isStartServer()) {
setSocketCliente(getServerSocket().accept());
setUser(new User());
getUser().setCliente(getSocketCliente());
scanner = new Scanner(getUser().getCliente().getInputStream());
if (scanner.hasNext()) {
getUser().setNome(scanner.nextLine());
}
writeTextArea(getUser().getNome() + " conectou-se.");
if (!clientes.containsKey(getUser().getNome())) {
clientes.put(getUser().getNome(), getUser());
getUser().setIpComputador(getUser().getCliente().getInetAddress().getHostAddress());
getUser().setNomeComputador(getUser().getCliente().getInetAddress().getHostName());
ServidorReadMessage read = new ServidorReadMessage(getUser(), this);
new Thread(read).start();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (getServerSocket() != null) {
getServerSocket().close();
}
if (getSocketCliente() != null) {
getSocketCliente().close();
}
if (scanner != null) {
scanner.close();
}
} catch (IOException e) {
}
}
}
This way, it is writing correctly, however, I do not know if it is the best way, I do not know nor if it is correct to do so. That is why I would like your help. I have looked at the documentation and it says nothing about it.
It is possible that you have two Jtextarea in your project, however one has been added to Jframe and the other has not. You could show us your code?
– Math
@Math When I click on a button, it does the following:
writeTextArea("Iniciando servidor...");
Nothing else is here:private void writeTextArea(String msg){
 area.append(msg+"\n");
 };
– Cristiano Bombazar
and you’re sure you didn’t instantiate two Jtextarea objects?
– Math
@Math Tenho. I only have one instance of Jtextarea. The funny thing is that if I give a gextText to show in a Joptionpane for example, it shows all the text.
– Cristiano Bombazar
@Math http://www.guj.com.br/java/308638-jtextarea-recebe-o-append-mas-nao-exibe-o-texto-added Here he highlighted better. But I’m in the same trouble.
– Cristiano Bombazar
I read the link you passed. Unfortunately there he did not give the solution right? Anyway.. Your code is too big? Because I believe that making it available would make it easier to help you. Maybe if you make one Minimum, Complete and Verifiable Example help as well.
– Math
Your text area is inside a
JScrollPane
such as the linked example? The problem remains if you take it out of there? And your model -Document
- is the default? (if you don’t know what I’m talking about then yes, it’s the default) P.S. I agree with Math, put a full example,.– mgibsonbr
By the way, you’re doing heavy processing on Event Dispatcher Thread? (ex.: when you click on a button, the button itself
actionPerformed
this button starts a loop that takes time to finish) If the answer is yes, then it is explained your problem... See that my answer in another question for more details (the context is different, but the root cause would be the same).– mgibsonbr
@Math I updated the code up there if you can take a look. Thanks.
– Cristiano Bombazar
@mgibsonbr I updated the code up there. Before I put "Startservidor" as a Thread, within this class it has basically a while(true) inside it to accept socket connections. Is that why? But why after I implemented Runnable in "Startservidor" did he update Jtexarea correctly? Thanks for the help.
– Cristiano Bombazar
@Cristianobombazar Exactly! I will post a reply with more details.
– mgibsonbr