1

I crated a Frame which have a panel inside it and the panel have a textarea inside it. Now i created a constructor which makes the frame visible for some time and after that it is set as invisible. Time for which it is visible it shows some message.

When i run the constructor code inside the main method of outputDisplay class it shows the text massage screen shot of output calling constructor inside the main method of class but when i call it inside other class by using new outputDisplay(String ip, int time) then only the frame appers but with no text inside it.screen shot of output when calling using new outputDisplay(String ip, int time) inside other class in same pakage

import java.awt.BorderLayout; 
import java.awt.Color;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class OutputDisplay {

JFrame frame;
JPanel  panel;
JTextArea area;
Font font;

    OutputDisplay(String ip,int time) throws InterruptedException{
    frame = new JFrame("Warning");
    frame.setLocation(400, 220);
    panel = new JPanel();
    area = new JTextArea();
    font = new Font("Aharoni", Font.BOLD, 16);
    area.setFont(font);
    area.setForeground(Color.RED);
    area.setSize(200, 200);
    int j=0;
    String[] t = {ip}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    //area.setText(ip);
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(600, 200);
    frame.setVisible(true);
    Thread.sleep(time);
    j++;
    if(j==1){
        frame.setVisible(false);
    }//if
    frame.setResizable(false);
    }//constructor
 }//Class
Cœur
  • 37,241
  • 25
  • 195
  • 267
Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56

2 Answers2

5

Thread.sleep(time); Dont do it (you're blocking the EDT). Use a Swing Timer

    Timer timer = new Timer(time, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
        }
    });
    timer.start();

See


"When i run the constructor code inside the main method of outputDisplay class it shows the text massage "

You are probably doing

public static void main (String[] args) {
    new OutputDisplay();
}

This is not run on the Event Dispatch Thread, (But wrong). If you ran it on the EDT, like you're supposed to (see Initial Threads, it will not work

public static void main (String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        new OutputDisplay();                     <==== Create on EDT
    });                                                WON'T WORK!!
}

"but when i call it inside other class by using new outputDisplay(String ip, int time) then only the frame appers but with no text inside it."

JBUtton button = new JButton("Button");
button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        new OurputDisplay();                     <===== Created on EDT!!
    }
});

Whether or not you start the application on the Event thread, all Component Events are dispatch on this thread, so when you press a button to try and open the new frame, this is create the frame on the EDT. Same problem as when running the OutputDisplay on its own with SwingUtilities.invokeLater

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3

The answer is that the second way, you are running this on the UI thread. So when you do your sleep call, nothing is displayed in the gui BECAUSE YOU'VE PUT THE GUI TO SLEEP. The solution is to use a javax.swing.Timer.

... Not meaning to yell, just want to highlight the source of the problem

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80