Why my codes failed to update JTextArea in real time?
-
21-06-2021 - |
Domanda
Hi I'm new to Java programming. I am trying to execute an external command from Java and then show the command prompt output in JTextArea in real time
. That external program will generate 1 line of output every second and then exit after 10 seconds.
The following are my Java codes:
original codes have been deleted to save space after reading Kumra's answer
When I run program.exe manually in command prompt window, the output is updated in real time like this:
<some warning message of the .exe program
which should not affect the output of the program> // shown at t=0
output line 1 //shown at t=1
output line 2 //shown at t=2
output line 3 //shown at t=3
output line 4 //shown at t=4
output line 5 //shown at t=5
output line 6 //shown at t=6
output line 7 //shown at t=7
output line 8 //shown at t=8
output line 9 //shown at t=9
output line 10 //shown at t=10
Done. //shown at t=10.
When I run my Java program at above, I thought JTextArea will be updated in real time to show the command prompt output. Unfortunately, it is not working. The actual output is like this:
<some warning message of the .exe program
which should not affect the output of the program> // shown at t=0
From t=0 until t=10, the JTextArea get stuck at the above output. At t=11, it suddenly show the complete output:
<some warning message of the .exe program
which should not affect the output of the program> // shown at t=0
output line 1 //shown at t=11
output line 2 //shown at t=11
output line 3 //shown at t=11
output line 4 //shown at t=11
output line 5 //shown at t=11
output line 6 //shown at t=11
output line 7 //shown at t=11
output line 8 //shown at t=11
output line 9 //shown at t=11
output line 10 //shown at t=11
Done. //shown at t=11
May I know what's wrong with my codes? Can anyone teach me how to display command prompt output in JTextArea in real time
? Thanks.
EDIT 1:
I have edited the codes based on Kumar's answer but it is still not working. Below are the latest codes.
MyUI.java
public class MyUI extends JFrame
implements ActionListener, KeyListener, ChangeListener, WindowListener
{
...
private JTextArea output;
public void showMessage(String message)
{
output.append(message + "\n");
output.setCaretPosition(output.getDocument().getLength());
}
...
public void actionPerformed(final ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
if (xxxxx)
{
myThreadInstance = new MyThread(xx,xxx,xx,xx,xx);
myThreadInstance.start();
}
}
}
}
}
MyThread.java
public class MyThread extends Thread
{
...
public MyUI myFrame;
...
public void run
{
try
{
String command = "program.exe arg1 arg2 arg3 arg4";
List<String> items = Arrays.asList(command.split("\\s+"));
builder = new ProcessBuilder(items);
builder.redirectErrorStream(true);
process = builder.start();
input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String inputline = null;
while ((inputline = input.readLine()) != null)
{
myFrame.showMessage(inputline);
}
}
catch(){}
finally{}
}
}
Soluzione
Try this...
- Always keep the UI work on the UI thread and Non-UI work on the Non-UI thread.
- Event Dispatcher Thread is the UI thread, which is responsible for the Gui.
- You are doing the process of counting from t=0 to t=10 on the UI thread, that's the reason you don't see any output till t=11.
- Create a separate thread to do the counting on the cmd, or use
SwingWorker
provided in Swing to synchronize the UI and Non-UI thread.