How to show message by messages from main class in UI window?

Tags: java swing swt awt
By : May12
Source: Stackoverflow.com
Question!

Unfortunately I can't do step by step output in the main program window (object g).

Part of main class:

/* Create a main window  of GUI*/  
    Gui g = new Gui();        
    g.showitself(); /*object is shown on the screen*/

    /*Check folder existing*/
    boolean inOutFilesFolderExists = (new File(vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder).exists());
         if (inOutFilesFolderExists)
            {
             System.out.println("Folder exists."); /*it is just for understanding if-operator works*/
             String textMessage = "\n"+"Folder " + vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder + "exists."+"\n" ; /*the message that I want to show in the main screen*/
             g.printmessage(logCurDateTime, textMessage); /*I am trying to show textMessage in the main GUI window by this method - g.printmessage  */
             }
         else
             { System.out.println("Folder doesn’t exists.");
            String textMessage = "Folder " + vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder + " doesn’t exist and will be created." ;
             g.printmessage(logCurDateTime, textMessage);
             new File(vars.diskLetter+"//"+vars.ppmRootFolder+"//"+vars.inOutFilesFolder).mkdirs();
             }
/** Now I am trying to create a new text message that should follow after previous text message */
            String textMessage = "Hello, Java Log.";
           // get to g object variables for showing 
            g.printmessage(logCurDateTime,  textMessage);

GUI class:

        public class Gui {
                        Border solidBorder = BorderFactory.createLineBorder(Color.GRAY, 6);
                        Font font = new Font("Verdana", Font.PLAIN, 12);
                       //method which shows the main window of GUI
                        JFrame main_Window = new JFrame("PPM v.2.0");
                        public void showitself ()
                       {
                            main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            main_Window.setSize(800, 400);
                            main_Window.setVisible(true);
                       }
                        /*This method should show messages from main class */
                        void printmessage (String logCurDateTime, String textMessage)
                       {
                       JLabel label = new JLabel("      " + logCurDateTime + " - " + textMessage +"\n");
                        main_Window.getContentPane().add(label);
                        }
        }

As result I receive GUI main screen only with the last message "Hello, Java Log.". As I understand it means than the previous message is rewritten by the next, but I need a serial output of messages (message by message) like this:

logCurDateTime - Folder exists. (or Folder doesn’t exist and will be created.)

logCurDateTime - "Hello, Java Log."

Also I am trying to recode (using eclipse.swt library) printmessage method as

void printmessage (String logCurDateTime, String textMessage)
             {

             JTextArea mainTextArea = new JTextArea();
             mainTextArea.append(logCurDateTime + textMessage +"/n");
             }

But it doesn’t help (no any message at all).

How to make a code which show messages from main class one by one?

By : May12


Answers

Thank you everybody! This class show message by message in the frame:

public class Gui {
    Border solidBorder = BorderFactory.createLineBorder(Color.GRAY, 6);
    Font font = new Font("Verdana", Font.PLAIN, 12);


    JFrame main_Window = new JFrame("PPM v.2.0");
    JTextArea mainTextArea = new JTextArea(5, 200);

    public void showitself ()
    {

            main_Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            main_Window.setSize(800, 400);
            main_Window.setVisible(true);
            main_Window.add(mainTextArea);


    }


    public void printMessage(final String textMessage) {
        if (EventQueue.isDispatchThread()) {
            appendMessage(textMessage);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    appendMessage(textMessage);
                }
            });
        }
    }

    private void appendMessage(String message) {
        mainTextArea.append(message);
        mainTextArea.append("\n");
    }

}
By : May12


This video can help you solving your question :)
By: admin