CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    32

    writing to JTextArea from another object

    Hi, Java Eclipse windows

    I have a calling class and a class that converts a file. I want to call the file converting class and do a conversion. While thats happening I want to print a number indicating progress in the jTextArea of the calling class...so


    Code:
    class Object 1 {
    
    public void actionPerformed(ActionEvent e)
    {
    InnerObject.doStuff(OuterFile, txtArea);
    }
    }
    
    
    public static File doStuff(File InnerFile, JTextArea InnerTextArea)
    {
      try {
                      
                      
                    while (somecondition) {
    
    
                            //Do converting stuff here
    
                      
                            convertstepcounter2=convertstepcounter;
                            Integer.toString(convertstepcounter2);
                            InnerTextArea.append(""+convertstepcounter2 + "\n");
                            InnerTextArea.setCaretPosition(InnerTextArea.getText().length());
                            convertstepcounter++;
                    }
              
                    }
                    catch(IOException ie){
                            ie.printStackTrace();
                       }
              
                   
    
                    return convertedFile;
    }
    I get nothing, probably since you have to exit the while loop to print a message. Would there be a solution to this problem so I can get a counting txtArea while in InnerObject?

    Thanks

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: writing to JTextArea from another object

    It's probably not updating the text area until the method returns because Swing is single threaded and I guess you are running the method on the Event Queue Dispatch thread.

    When performing this sort of task you should use a javax.swing.SwingWorker. The API docs have usage examples alternatively look at the Java tutorials
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured