0

I have that code

import javax.swing.JFrame;
import javax.swing.JLabel;

    public class ChatClient extends JFrame {
        public static void main(String[] args) throws InterruptedException {
            JFrame frame=new JFrame("chat");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400,400);
            frame.setVisible(true);
            frame.setLayout(null);
            JLabel label=new JLabel();
            label.setText("HOR");
            label.setBounds(100,100,100,100);
            frame.add(label);
            Thread.sleep(1000);
            JLabel label1=new JLabel();
            label1.setText("SE");
            label1.setBounds(100,200,100,100);
            frame.add(label1);
        }
    }

I want write multiple strings in the frame but at different times. The problem is that "hor" is printed correctly, but "se" is printed only if I change the size of the window or if I minimize it. I think "se" is not printed until I update the frame. I know that i can resolve with frame.repaint() after Thread.sleep() , but is the more appropriate method?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

0

Setup the Frame completely before sleep and then change the text after sleep

private static void drawFrame() {
    JFrame frame=new JFrame("chat");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setVisible(true);
    frame.setLayout(null);
    JLabel label=new JLabel();
    label.setBounds(100,100,100,100);
    frame.add(label);
    JLabel label1=new JLabel();
    label1.setBounds(100,200,100,100);
    frame.add(label1);
    label.setText("HOR" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
    // frame elemets setup
    
    sleep();
    label1.setText("HOR" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
}
PrasadU
  • 2,154
  • 1
  • 9
  • 10
  • thanks, can you explain to me why with this method it works and if instead I set the text before adding the label it doesn't work – gianlucavallo Jul 01 '20 at 12:29
  • do you know any programs to graphically program the design of a jframe. For example in android studio you can create the activity design in a graphic way: for example you can drag a button to the position where you want it to be displayed, etc. If my request is not clear, tell me I'll try to make myself better understood – gianlucavallo Jul 01 '20 at 12:34
  • repaint is required if you add completely new UI elements to the JFrame after the initial render. but you can change / update existing elements with out repaint. see https://stackoverflow.com/questions/4279435/java-how-would-i-dynamically-add-swing-component-to-gui-on-click#4279481 – PrasadU Jul 01 '20 at 13:12
  • Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Sep 15 '20 at 10:05