Search code examples
swingjframejtextareaswingworker

Getting null / empty JtextArea


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 massage.

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

Get this output when runs inside the same method

Getting a empty frame when run it from other class by using new keyword

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class OutputDisplay {

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

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

    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
    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

    OutputDisplay(String dialogue,String path,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.MAGENTA);
    area.setSize(200, 200);
    int j=0;
    String[] t = {dialogue}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(500, 200);
    frame.setVisible(true);
    Thread.sleep(time);
    j++;
    if(j==1){
        frame.setVisible(false);
    }//if
    frame.setResizable(false);
    }//constructor

    OutputDisplay(String dialogue) throws InterruptedException{
    frame = new JFrame("Report");
    frame.setLocation(400, 220);
    panel = new JPanel();
    area = new JTextArea();
    font = new Font("Aharoni", Font.BOLD, 16);
    area.setFont(font);
    area.setForeground(Color.BLUE);
    area.setSize(200, 200);
    String[] t = {dialogue}; 
    for(int i=0;i<t.length;i++){
        area.append(t[i]+"\n");
    }//for
    panel.add(area);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    frame.getContentPane().add(BorderLayout.CENTER, panel);
    frame.pack();
    frame.setSize(600, 600);
    frame.setVisible(true);
    frame.setResizable(false);
    }//constructor

   public OutputDisplay() {
    }//no arg

public void dialogue (String massage) throws InterruptedException{
        frame = new JFrame("Massage");
        frame.setLocation(400, 220);
        JPanel panel = new JPanel();
        JTextArea area = new JTextArea();
        Font font = new Font("Aharoni", Font.BOLD, 16);
        area.setFont(font);
        area.setForeground(Color.GREEN);
        area.setSize(200, 200);
        int j=0;
        String[] t = {massage}; 
        for(int i=0;i<t.length;i++){
            area.append(t[i]+"\n");
        }//for
        panel.add(area);
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.pack();
        frame.setSize(400, 100);
        frame.setVisible(true);
        Thread.sleep(8*1000);
        j++;
        if(j==1){
            frame.setVisible(false);
        }//if
        frame.setResizable(false);
    }//dialogue

   static void warningPopUp (String massage){
       JOptionPane.showMessageDialog(null, massage);
    }//dialogue

   public static void main(String[] args){
   new OutputDisplay("We are checking for account validation"+"\n"
                + "If user id and password matches then we will goto websites for updation. "    +"\n"
            + "You will get a complete report once we are done", 4*1000);


   }//main
 }//OutputDisplay

Solution


  • Edit
    You're still using Thread.sleep, again you shouldn't use it, but instead should use a Timer. Also, never set a JTextArea's size or preferred size ever. Do this and the scrollbars of your JScrollPane won't work. Instead, set its rows and columns.

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Dialog.ModalityType;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TimeLimitedDialogTester {
       private static final int EB_GAP = 25;
       private JPanel mainPanel = new JPanel();
    
       public TimeLimitedDialogTester() {
          mainPanel.add(new JButton(new ShowDialogAction("Show Dialog")));
          mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, 2 * EB_GAP, 2 * EB_GAP, 2 * EB_GAP));
       }
    
       public JPanel getMainPanel() {
          return mainPanel;
       }
    
       @SuppressWarnings("serial")
       private class ShowDialogAction extends AbstractAction {
          public ShowDialogAction(String name) {
             super(name);
             int mnemonic = (int) name.charAt(0);
             putValue(MNEMONIC_KEY, mnemonic);
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             Window mainWin = SwingUtilities.getWindowAncestor(mainPanel);
             int seconds = 5;
             String dialogTitle = "WARNING";
             String text = "We are checking for account validation. If user id and password matches then we"
                   + " will go to websites for updating. You will get a complete report once we are done.";
             TimeLimitedDialog timeLimitedDialog = new TimeLimitedDialog(mainWin, seconds, dialogTitle, text);
             timeLimitedDialog.dialogShow();         
          }
       }
    
       private static void createAndShowGui() {
          TimeLimitedDialogTester timeLimitedDialog = new TimeLimitedDialogTester();
    
          JFrame frame = new JFrame("TimeLimitedDialog");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(timeLimitedDialog.getMainPanel());
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class TimeLimitedDialog {
       private static final int ROWS = 6;
       private static final int COLS = 30;
       private static final Color FG = Color.green.darker().darker();
       private JDialog dialog;
       private JTextArea displayArea = new JTextArea(ROWS, COLS);
       private int seconds;
    
       public TimeLimitedDialog(Window mainWin, int seconds, String dialogTitle, String text) {
          displayArea.setWrapStyleWord(true);
          displayArea.setLineWrap(true);
          displayArea.setFocusable(false);
          displayArea.setText(text);
          displayArea.setForeground(FG);
          displayArea.setFont(displayArea.getFont().deriveFont(Font.BOLD));
    
          this.seconds = seconds;
          dialog = new JDialog(mainWin, dialogTitle, ModalityType.APPLICATION_MODAL);
          dialog.add(new JScrollPane(displayArea));
          dialog.pack();
          dialog.setLocationRelativeTo(mainWin);
       }
    
       public void dialogShow() {
          new Timer(seconds * 1000, new TimerListener()).start();
          dialog.setVisible(true);
       }
    
       private class TimerListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent e) {
             if (dialog != null && dialog.isVisible()) {
                dialog.dispose();
             }
             ((Timer) e.getSource()).stop();
          }
       }
    }