Can't append text to a JTextArea from another class
-
Found a similar topic on here but the solutions provided didn't work (or I'm just not seeing it). Here's my code reduced to the minimum: Here's the class I want to call the print method from: { HumanInterface gui = new HumanInterface(); gui.init(); gui.printToArea("from Main Class"); } and here's the HumanInterface class which extends JFrame { JTextArea area = createArea(); public void init() throws InvocationTargetException, InterruptedException { HumanInterface gst = new HumanInterface(); gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gst.pack(); gst.setVisible(true); printToArea("print from HumanInterface class"); } public HumanInterface() throws InvocationTargetException, InterruptedException { Container container = getContentPane(); container.setLayout(new GridLayout(2, 3)); container.add(new JScrollPane(area)); } public void printToArea(String string) { try { updateArea(area, string); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private JTextArea createArea() { final JTextArea area; area = new JTextArea(); area.setBackground(Color.GRAY); return area; } private void updateTextArea(final JTextArea textArea, final String string) throws InvocationTargetException, InterruptedException { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { textArea.append(string); } }); } When I call printToArea(...) from anywhere else than from within the HumanInterface class, it doesn't work. What am I missing?
-
Answer:
The GUI you see is not the GUI you "want" to see, it's a second ;-) In your init() function you create a second HumanInterface: HumanInterface gst = new HumanInterface(); gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gst.pack(); gst.setVisible(true); I think you probably want that (not tested): this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); this.setVisible(true);
user3548855 at Stack Overflow Visit the source
Related Q & A:
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
- Why can't i receive emails from another yahoo member?Best solution by Yahoo! Answers
- How can I export a game to another PC?Best solution by Yahoo! Answers
- Can I move a game from a pc to another?Best solution by Yahoo! Answers
- How can i send some one a text through my computer?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.