Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with append text to main class JTextArea from another class.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    My Mood
    Relaxed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Problem with append text to main class JTextArea from another class through thread.

    I saw similiar posts but not solved, so i'm asking for help one's again.

    This is part of my applet.

    I have problem with append text from second class named TextOuterClassPut.
    "tta.textArea.append("text from second class")" from class TextOuterClassPut is not working.
    it should append this string to JTextArea in main frame.

    "textArea.append" from class TextOuterClassPut is called by method "otherClass.test()"



    Code of main class:

    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.List;
    import javax.swing.*;
     
    public class TestTextArea extends JApplet{
     
       public Container c = getContentPane();
       public static JFrame frame;
       public JButton startButton;
       public JTextArea textArea = null;
       public JPanel panel;
       public actListener act;
       private threadStart ts;
     
       public TestTextArea()
       {
          textArea = new JTextArea( 13 , 20 );
          textArea.setEditable( false );
          textArea.setLineWrap(true);
          textArea.setWrapStyleWord(true);
          startButton = new JButton("append text");
          act = new actListener();
          startButton.addActionListener(act);
          panel = new JPanel();
          panel.add( textArea );
          panel.add(startButton);
          c.add( panel );
          textArea.append("Text from main class \n");
       }
     
        public class actListener implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent zdarzenie) {
              ts = new threadStart();
              ts.execute();
            }
        }
     
        class threadStart extends SwingWorker<Void,Integer>{
     
            public threadStart(){}
     
            @Override
            protected Void doInBackground() throws Exception{
     
                textArea.append("Text from main class thread \n");
     
     
                // This 2 lines below is not working properly
     
     
                TextOuterClassPut otherClass = new TextOuterClassPut();
                otherClass.test();
     
                return null;
            }
            @Override
            protected void process(List<Integer> chunks){
            }
     
            @Override
            protected void done(){
            }
        }
     
        public static void main( String[] args ) {
     
          frame = new JFrame();
          frame.setSize( 300 , 300 );
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          frame.getContentPane().add(new TestTextArea());
          frame.setVisible(true);
       }
    }

    Code of second class:

    public class TextOuterClassPut {
     
        public TestTextArea tta =  new TestTextArea();;
     
        public TextOuterClassPut() {}
     
        public void test()
          {
     
            // !!! this line below is not working properly !!!
     
            tta.textArea.append( "text from second class" );
     
     
          }
       }


    please help
    Last edited by lush; October 10th, 2012 at 04:24 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem with append text to main class JTextArea from another class.

    I do not understand your question.


    This is working part of my applet - almost
    but text appended from another class is not.
    This is working, that is not... does not say much about what the problem is. Try to explain what the code should do, and what it is doing instead.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    My Mood
    Relaxed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with append text to main class JTextArea from another class.

    i'm sory i corrected main post. I hope its more clear now.
    Generaly problem is with "tta.textArea.append("text from second class"); - it is not working properly.
    it should be in JTextArea textArea in main frame and it is not.

    i'll be very grateful for the help
    thanks
    Last edited by lush; October 10th, 2012 at 04:28 PM.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem with append text to main class JTextArea from another class.

    You have several problems that I see, but most importantly is that yes, your code is in fact working in that you're appending text to a JTextArea that is held by TestTextArea object, but the problem you're having is that the TestTextArea is not displayed anywhere and nor can it be. It's a TestTextArea that was created for the sole purpose of doing your text manipulation, but since there's no way that it can be the displayed JApplet (you cannot display a JApplet that way), there's no way that you'll see any of the text that you're sending this object.

    To solve this, I suggest that you pass a reference from the actual displayed applet into your outer class, perhaps in the outer class's constructor, and then call methods on this object, not some completely unrelated TestTextArea that again is not the one being displayed.

    Other problems:
    • you're calling Swing methods from within a SwingWorker's doInBackground() method, something you never should be doing, nor would you ever have a reason for doing this. The doInBackground() method is for doing long-running non-Swing tasks. If you need to make Swing calls while this is running, use the publish/process method pair, or add a PropertyChangeListener and listen for changes to some bound property.
    • You're trying to fiddle directly with the field of another class. This breaks the OOP rule of encapsulation. Instead only call public methods on other objects.


    That's all for now.

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    lush (October 18th, 2012)

  6. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    My Mood
    Relaxed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with append text to main class JTextArea from another class.

    Thank you Now it's working.
    Last edited by lush; October 18th, 2012 at 05:43 AM.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem with append text to main class JTextArea from another class.

    You're welcome. Glad you've got it working!

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  3. append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 1
    Last Post: July 25th, 2012, 03:23 PM
  4. scope problem: append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 03:21 PM
  5. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 10:26 AM