Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Can't get my JTextArea output thread messages.

  1. #1
    Azaera is offline Member
    Join Date
    Jun 2012
    Location
    Gothenburg, Sweden
    Posts
    5
    Rep Power
    0

    Default Can't get my JTextArea output thread messages.

    Evening.

    Have been sitting with this problem for a few days now and can't get it to work.
    I'm supposed to get a code which works in console to work with a gui window and I've tried to get the edt to work (as I understood it's rules), but still the text just don't want to show up.

    The code is as below, and I just don't know where it is I loose the thread messages as it works nice in console window.

    Anyone who could point a little direction for me where to start look on it would be very apprecitated.

    Java Code:
    package p6;
    
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    
    public class GUIViewer extends JFrame implements MessageViewer {
    	private JFrame frame = new JFrame("GUIViewer");
    	private JPanel panel = new JPanel();
    	private JTextArea area = new JTextArea();
    	
    	public GUIViewer() {
    		setLayout(new FlowLayout());
    		panel.add(area);
    		frame.add(panel);
    		frame.setSize(400,200);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		area.setSize(400,200);
    		area.setLineWrap(true);
    		area.setWrapStyleWord(true);
    		area.setEditable(false);
    		area.setVisible(true);
    		area.setText("Update me please!");
    		
    		add(area);
    	}
    	
    	public void setText(Message message) {
    		area.setText(message.getText());
    	}
    	
    	
    	public void noMessage() {
    	}
    	
    	@Override
    	public void setMessage(Message message) {
    		message.getText();
    	}
    	
    	public static void main(String[] args) {
    		 SwingUtilities.invokeLater(new Runnable() {
    		      public void run() {
    		    	  MessageViewer viewer = new GUIViewer();
    		    	  
    		    	  try {
    		    			Message m1 = new Message("Nu är det november och sedan är det december", Color.yellow, Color.black,
    		    			new Font("SansSerif", Font.PLAIN, 24));
    		    			Message m2 = new Message("En adventskalender med godis piggar alltid upp", Color.white, Color.red,
    		    			new Font("Serif", Font.BOLD, 16));
    		    			Message m3 = new Message("Jultomte söker jobb\nring 0722-11223344", Color.red, Color.black,
    		    			new Font("Monospaced", Font.ITALIC, 20));
    		    			viewer.noMessage();
    		    			Thread.sleep(5000);
    		    			viewer.setMessage(m1);
    		    			Thread.sleep(5000);
    		    			viewer.setMessage(m2);
    		    			Thread.sleep(5000);
    		    			viewer.setMessage(m3);
    		    			Thread.sleep(5000);
    		    			viewer.noMessage();
    		    			} catch (MessageException e1) {
    		    			System.out.println(e1);
    		    			}
    		    			 catch (InterruptedException e2) {
    		    			System.out.println(e2);
    		    			 }
    		      		}
    		    });
    	}
    }

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

    Default Re: Can't get my JTextArea output thread messages.

    You're calling Thread.sleep(...) on the Swing event thread, something that is guaranteed to put the GUI completely to sleep and be nonresponsive, something I don't think you want. Instead use a background thread such as a SwingWorker for this. Read up on this by Googling "Concurrency in Swing".
    Azaera likes this.

Similar Threads

  1. Replies: 3
    Last Post: 04-27-2012, 03:41 PM
  2. Using JTextArea for output
    By africanhacker in forum New To Java
    Replies: 11
    Last Post: 03-23-2011, 09:36 AM
  3. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  4. Need help with printing console output to JTextArea
    By ShinTec in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2010, 11:10 AM
  5. Redirecting output at Thread level
    By beezerbutt in forum Advanced Java
    Replies: 8
    Last Post: 02-15-2009, 12:19 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
  •