public static void showResult(JTextArea txt, String result) { try { if (txt.getText().length() == 0) { txt.setText(result); txt.setSelectionStart(txt.getText().length()); txt.setSelectionEnd(txt.getText().length() - 1); } else { if (txt.getText().length() > MAX_LOG_SIZE) txt.getDocument().remove(0, txt.getText().length() - MAX_LOG_SIZE); txt.setSelectionStart(txt.getText().length()); txt.getDocument().insertString(txt.getText().length(), result, null); txt.setSelectionEnd(txt.getText().length()); } } catch (Exception e) { e.printStackTrace(); } }