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