Results 1 to 8 of 8
  1. #1
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default Update content on a JTextArea.

    Hello.

    I am having a JTextArea that on startup is printing a line from a shoutbox.
    How would I update it, so it keeps writing lines on my textArea?

    This is what I have right now:
    Java Code:
    public void createAndShowGUI() {
            // Creates the JFrame
            JFrame myFrame = new JFrame("Inferno Shoutbox");
    
            //Creating the text area, and makes it non-editable
            JTextArea myArea = new JTextArea(5, 5);
            myArea.setEditable(false);
    
            //Writes a simple message to the JTexTArea
            myArea.append(get() + newLine);
    
            //Loads the textArea on our applet
            myFrame.getContentPane().add(myArea, BorderLayout.CENTER);
    
            //Sets the size of our applet
            myFrame.setPreferredSize(new Dimension(800, 600));
    
            // Sets the frame visible, and packs it
            myFrame.setVisible(true);
            myFrame.pack();
        }
    where get() is a string that gets the line from the website.


    How can I make it update so it keeps writing the get() string, over and over?
    - Masochist.

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    (giving a rough idea)

    Would a while loop with get().hasNext work?

    I am assuming you are trying to fill up the textarea with all the info right away
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Sno View Post
    (giving a rough idea)

    Would a while loop with get().hasNext work?

    I am assuming you are trying to fill up the textarea with all the info right away
    I want to get each line, as they appear (copying a chatroom).
    I dont know how a while loop would look :/ please explain this :)
    - Masochist.

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

    Default

    How are you getting the "line from a shoutbox"? You will likely have to do this in a background thread, perhaps a SwingWorker thread and then update the JTextArea in the SwingWorker's publish/process method pair. How much Java, Swing, and Network programming are you familiar with as the answers will likely depend on this?
    Last edited by Fubarable; 05-13-2010 at 07:35 PM.

  5. #5
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    I know about the bufferedreader, bufferedwriter, basic swing (creating and maintaining a JFrame and a few other components), I know about packaging, different kind of statements ;S

    I'm getting the line this way:
    Java Code:
    public String get() {
    		String urll = "http://www.rune-server.org/infernoshout.php";
    		String identifier = "return InfernoShoutbox.open_pm_tab";
    		String foundString = "WHAT THE???";
    		try {
    			URL url = new URL(urll);
    			URLConnection connection = url.openConnection();
    			connection.addRequestProperty("Referer", "http://google.com");
    			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    			String s = null;
    			while ((s = reader.readLine()) != null) {
    				if(s.contains(identifier)) {
    					foundString = s;
    					break;
    				}
    			}
    			reader.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		String[] parts = foundString.split("</a>:");
    		String toSend = parts[1].replaceAll("</div>", "");
    
                    return toSend;
        }
    - Masochist.

  6. #6
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    I feel like you would need to be constantly checking the chatroom somehow to get the line to a textarea
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  7. #7
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Sno View Post
    I feel like you would need to be constantly checking the chatroom somehow to get the line to a textarea
    Yes I have a timer, but how do i update the component?
    - Masochist.

  8. #8
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Have a method called once the timer done to keep appending?

    so at the end of the timer, each time it ends, call
    myArea.append(get() + newLine);
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

Similar Threads

  1. Replies: 10
    Last Post: 05-24-2010, 09:25 PM
  2. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 07:22 AM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 08:51 PM
  4. How to update JTextArea with JSlider Values
    By theen3my in forum AWT / Swing
    Replies: 7
    Last Post: 10-04-2009, 09:48 AM
  5. Jtextarea Content Display Problem
    By REVANSIDDHA in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2009, 07:53 PM

Posting Permissions

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