Results 1 to 7 of 7
  1. #1
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Question [SOLVED] How to display a JTextArea in previously created GUI?

    How can I add a JTextArea to an already created GUI and have it re-display?

    I've tried using the code below, but it doesn't show the newly created JTextArea with the included Text; it doesn't show any error messages either.

    NOTE: thumbnailPanel is held within containerPanel, and both are already/previously created.

    Java Code:
    public void addText(LinkedList<String> fileNames){
    	for (String fileName : fileNames){
    		fileNameString = fileName + "\n";
    	}
    	thumbnailPanel.add(new JTextArea(fileNameString));
    	thumbnailPanel.repaint();
    	containerPanel.repaint();	
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Always revalidate() the container after adding or removing any component[s] from a visible parent.

    repaint() is sometimes also necessary (usually when removing the last child component) but it doesn't harm to invoke it anyhow (after revalidate, not before)

    Next time you have a Swing question, please post it in the Swing forum.

    db
    Last edited by DarrylBurke; 03-05-2009 at 09:49 PM.

  3. #3
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Always revalidate() the container after adding or removing any component[s] from a visible parent.

    repaint() is sometimes also necessary (usually when removing the last child component) but it doesn't harm to invoke it anyhow (after revalidate, not before)

    Next time you have a Swing question, please post it in the Swing forum.

    db
    Thanks for the suggestions. Unfortunately, the problem was more involved then using revalidate() or repaint().

    The problem was having one class (FileBrowser) update a GUI which was created by another GUI creator class (AppletGUI). I was trying to have the FileBrowser get file names and then update a JTextArea in a GUI - but the issue was accessing the correct instance of the GUI.

    To resolve the issue, I had to create a method in the GUI creator class that retrieved the file names and then displayed it via the setText method. Out of curiosity, if you know how it can be done another way, do let me know. If you need to see a condensed sample, I can post it.

    As for posting in this forum, I posted here because I am "new to java," but henceforth I will certainly post in the appropriate forum.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, posting some code would probably help clarify the problem.
    Also, what layouts are your components using, especially the one accepting the JTextAreas?

    You may wish to create and post an SSCCE: Short, Self Contained, Correct Example
    Last edited by Fubarable; 03-06-2009 at 06:54 PM.

  5. #5
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    I posted a response on Saturday; but for some reason it is no longer displayed in this thread. So, I'm re-posting.

    First and foremost, the root of problem was not Swing related, but rather how object instances interact.

    The solution to my problem was to have the GUI (inner) class get the desired information from another class (FileBrowser) and then display it. In the code below, note selectedFiles = browser.browse();

    The root of my problem was (incorrect) object instance interaction. Originally, I was trying to have the FileBrowser object independently add text to the GUI object; the problem was the only way to access the GUI attributes was to create a new GUI instance; thus when I added the text I wasn't adding it to the intended GUI, but rather to a new & different GUI.

    I hope my confusion/problem and the solution is now clearer.

    Java Code:
    class ButtonHandler implements ActionListener{
    		
    		public void actionPerformed(ActionEvent e){
    			
    			File[] selectedFiles;
    			
    			FileBrowser browser = new FileBrowser();
    			selectedFiles = browser.browse();
    			
    			Thumbnailer thumbnails = new Thumbnailer();
    			thumbnails.createThumbnails(selectedFiles);
    			
    			for (Image thumbnailImage : thumbnails.getThumbnails()){
    				thumbnailPanel.add(new JLabel(new ImageIcon(thumbnailImage)));
    			}
    			thumbnailScroll.revalidate();
    			thumbnailScroll.repaint();
    			
    		}
    		
    	}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So,.... hopefully with your changes you've now solved your problem?

  7. #7
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    So,.... hopefully with your changes you've now solved your problem?
    Sorry, I should have marked this thread as "resolved." Hopefully, my post yesterday clearly illustrates the problem and the solution; so in the future, should someone else have the same issue, they can read the read and resolve their problem.

Similar Threads

  1. Replies: 17
    Last Post: 07-13-2009, 05:44 AM
  2. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 PM
  3. Replies: 2
    Last Post: 03-30-2008, 08:38 PM
  4. returning to a previously saved view
    By gojava in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:11 PM
  5. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:21 AM

Posting Permissions

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