JTextArea is not displaying clearly for particular period.
I created a JFrame, inside that added a JButton, while clicking on JButton, JFrame will be expanded and it will display a JTextArea and it will add 2 lines of text with 2 seconds interval. Problem is while clicking on JButton, atonce JFrame is expanded but JTextArea is not displaying clearly (JTextArea is displayed in black color), like below:
after 2 seconds it will be displayed with 2 lines of text, like below:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ExpandJFrame {
public static JFrame frame;
public static JButton btn;
public static JTextArea textArea;
public static void main(String[] args) {
frame = new JFrame("JFrame Expand");
frame.setSize(400, 200);
btn = new JButton("Expand");
btn.setBounds(10, 10, 80, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
frame.setSize(400, 400);
textArea = new JTextArea();
textArea.setBounds(5, 210, 370, 150);
textArea.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
frame.add(textArea);
Thread.sleep(1000);
textArea.append("FirstLine\n");
Thread.sleep(1000);
textArea.append("SecondLine\n");
} catch (Exception e) {
}
}
});
frame.add(btn);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Expected scenario: While clicking on JButton, atonce JTextArea should be displayed clearly as empty, then user should be able to view the 2 lines entered one by one.
Can anyone help one this?