Results 1 to 5 of 5
  1. #1
    mtz1406 is offline Member
    Join Date
    Aug 2008
    Location
    Riyadh - Saudi Arabia
    Posts
    15
    Rep Power
    0

    Default How can I show my log class in jTextArea

    hi
    I hava a class Worker like this:
    public class Worker
    {
    private string log = "";

    public string getLog()
    {
    return this.log;
    }

    public void work()
    {
    this.log += "start";
    this.log += "I am working Now";
    this.log += "End";
    }
    }

    what I want is how to refresh my jTextArea while the worker working to show the worker log

    thank you

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    The task is to perform an action each time a string is appended to log.
    Actions are what methods do.
    So write a method to append to log.
    And inside this method write code to also send the text to the JTextArea.

    Java Code:
    	void appendToLog(String s) {
    		log += s;
    		myJTextArea.append(s);
    	}
    	...
    	public void work {
    		appendToLog("start");
    		appendToLog("I am working Now");
    		appendToLog("End");
    	}

  3. #3
    mtz1406 is offline Member
    Join Date
    Aug 2008
    Location
    Riyadh - Saudi Arabia
    Posts
    15
    Rep Power
    0

    Default

    thank you for reply zweibieren
    but think that the worker class must be pure (No swing variable)
    one solution may be implement custom event and listener. (but I dont know how I do that)
    thank you

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    There is no magic to events and listeners.
    An event object is a bundle of data.
    A listener is an object with methods.
    The argument to one of those methods
    is an event object with its bundle of data.

    In the case of the code I propose, the event data is the string to append to the log:
    Java Code:
    class [COLOR="SeaGreen"][B]LogEvent [/B][/COLOR]{
    	String message;    // the "bundle" of data
    	LogEvent(String s) {message = s;}   // create event bundle
    	String getMessage() { return message;}   // report the data
    }
    The LogListener class has the method occured(LogEvent ev);
    it must be overriden to do what needs to be done:
    Java Code:
    class [COLOR="RoyalBlue"][B]LogListener [/B][/COLOR]{  abstract void occurred(LogEvent [COLOR="SeaGreen"][B]ev[/B][/COLOR]);  }
    Now let's rewrite the code in my previous response to use a LogListener.
    Java Code:
    class Worker {
    	LogListener [COLOR="RoyalBlue"][B]destination[/B][/COLOR];  // remember destination from when Worker is constructed
    	Worker(LogListener d) { destination = d; }   // save the LogListener
    	void appendToLog(String s) {
    		log += s;     // (2)
    		LogEvent [COLOR="SeaGreen"][B]e[/B][/COLOR] = new LogEvent(s);   // (3)
    		// myJTextArea.append(s);  // do not refer to Swing object in Worker
    		[COLOR="RoyalBlue"][B]destination[/B][/COLOR].occurred([COLOR="SeaGreen"][B]e[/B][/COLOR]);     // (4) send the event data bundle to destination
    	}
    	...
    	public void work {
    		appendToLog("start");     // (1)
    		appendToLog("I am working Now");
    		appendToLog("End");
    	}
    }
    Now the client needs to create a Worker and provide a LogListener.
    The LogListener implements the occurred() method
    to extract the data from the LogEvent bundle
    and send it to the Swing object.
    Java Code:
    	LogListener [COLOR="RoyalBlue"][B]myListener[/B][/COLOR] = new LogListener() {
    		void occurred(LogEvent [COLOR="SeaGreen"][B]ev[/B][/COLOR]) {   // (5)
    			String msg = [COLOR="SeaGreen"][B]ev[/B][/COLOR].getMessage(); // (6)
    			myJTextArea.append(msg);  // (7)
    		}
    	}
    	Worker w = new Worker([COLOR="RoyalBlue"][B]myListener[/B][/COLOR]);
    So let us follow the execution of the first line in work():
    1. appendToLog is called with the string ("start") to enter in the log
    2. appendToLog appends the string to the log variable ( log += s; )
    3. appendToLog creates an event data bundle containing s ( new LogEvent(s) )
    4. appendToLog calls the occurred() method in the destination object
    5. the occurred() method in myListener is called
    6. occurred() fetches the string from the event data bundle
    7. occurred() appends the message to myJTextArea

    This is all very round-about and indirect. The result is a Worker class that does not know about the swing objects in the client program.

    Exercise to the reader:
    Extend the Worker class so it has an addListener() method.
    The Worker class must make a Vector of the listeners
    and call the occurred method in each one from appendToLog().
    (Typically the operation to call all the listeners
    is in a method called fire(). appendToLog() will create theLogEvent
    and then pass it to fire() for delivery to the listeners.)

  5. #5
    mtz1406 is offline Member
    Join Date
    Aug 2008
    Location
    Riyadh - Saudi Arabia
    Posts
    15
    Rep Power
    0

    Default

    Thank you very very much
    your answer is very clear
    I hope that I can help you as you help me

Similar Threads

  1. the component does not show
    By shruti in forum AWT / Swing
    Replies: 8
    Last Post: 07-08-2009, 12:21 PM
  2. Data from a model class won't show up in the table
    By ayampanggang in forum AWT / Swing
    Replies: 3
    Last Post: 11-27-2008, 08:20 PM
  3. How to Show Calendar
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 02-09-2008, 08:23 AM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM
  5. How to always show the last line in my JTextArea?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •