-1

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:

enter image description here

after 2 seconds it will be displayed with 2 lines of text, like below:

enter image description here

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?

AbiSaran
  • 2,538
  • 3
  • 9
  • 15
  • 1
    Do read https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ – SirFartALot Apr 10 '19 at 11:42
  • Possible duplicate of [How can I set up my JProgressbar to show the progress of my program loading](https://stackoverflow.com/questions/54716324/how-can-i-set-up-my-jprogressbar-to-show-the-progress-of-my-program-loading) – SirFartALot Apr 10 '19 at 11:43
  • Already tried using SwingUtilities.invokeLater, but getting same issue. – AbiSaran Apr 10 '19 at 11:56
  • Maybe this helps: https://stackoverflow.com/questions/55593572/jprogressbar-not-visible-during-reading – SirFartALot Apr 10 '19 at 14:06

1 Answers1

3

after 2 seconds it will be displayed with 2 lines of text

Don't use Thread.sleep(...). This prevents the frame from repainting itself.

Instead you need to either:

  1. move the code in the ActionListener to a separate Thread. This can be done by using a Swing Worker as demonstrated in the Swing tutorial o Concurrency, linked to above.
  2. use a Swing Timer to schedule the updates to the text area. For this approach you could add the text to an ArrayList. Then you start the Timer. When the Timer fires you remove the text at location 0 and display it in the text area. When the ArrayList is empty you stop the Timer. This will allow you to easily display multiple lines of text in the text area.
camickr
  • 321,443
  • 19
  • 166
  • 288