Results 1 to 4 of 4
  1. #1
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Question about appending jtextfield while program is running.

    Hey guys, it's been a while since I've been on the forums.

    I'm back at it and I have been working on a program, this program is working right now except for I do not know how to update the JTextField as the program is running. Right now I have two files, the core program that handles webpage manipulation and then the GUI program that sends commands to the core program structure.

    Java Code:
    jTextArea2.setText("");
    		SimpleMafiaBotCore runTimeBot = new SimpleMafiaBotCore();
    		jTextArea2.append("Building URL. \n");
    		String temp1 = (String) servCombo.getSelectedItem();
    		String mdUrl = "http://" + temp1.toLowerCase() + ".mafiadeath.com";
    		jTextArea2.append("URL: " + mdUrl + "\n");
    		System.out.println("URL: " + mdUrl);
    		boolean isLoggedOn = runTimeBot.logon(mdUrl, loginField.getText(), passField.getText());
    		if (isLoggedOn){
    			jTextArea2.append("Successfully Logged On.\n");
    		} else {
    			jTextArea2.append("Logging On Failed. \n");
    		}
    		
    		try {
    			Thread.sleep(1000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		
    		jTextArea2.append("Beginning Update Session \n");
    		runTimeBot.update(mdUrl);
    		jTextArea2.append("Update Session Ended. \n");
    		
    		jTextArea2.append("Beginning Training Session. \n");
    		runTimeBot.train("spd", mdUrl);
    		jTextArea2.append("Training Session Ended. \n");
    I know it's a bit choppy but this snippet is in my gui program, the only issue is that it doesn't append the text area until after the program completes. This is an issue because I need the text field to be appended during the program and I need the buttons in the gui program to be functional while the program is started. I'm not sure if I'm doing this right, or if I'm explaining this correctly. Let me know if more information is needed.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Question about appending jtextfield while program is running.

    Use a SwingWorker (see the API), or a self-written thread/runnable, and use SwingUtilities.updateAndWait or SwingUtilities.updateLater to actually perform commands that will modify the GUI.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Question about appending jtextfield while program is running.

    I stumbled across SwingWorker and Thread/Runnable last night, unfortunately I'm having a little trouble understanding them.

    So if I have Gui.java instantiating a Core.java object then manipulating it, where do I put the Worker class? Is this inside Gui.java? If jTextArea2 is within GUI.java, how can I get the SwingWorker to update it?

    All the examples and tutorials that I see only show the snippets of the SwingWorker, which doesn't help me understand how I translate it over to my program. I'm still digging for answers but maybe someone has the answer.

    From what I understand is that I have to write a custom class that extends SwingWorker and then instantiate it in my program? If that is the case then how would I be able to manipulate the jTextArea? Am I able to write a method inside Gui that extends SwingWorker? Will that create a separate thread that updates my jTextArea?

    I'll probably add Edits as I find more.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. JTextField question
    By gioeco in forum AWT / Swing
    Replies: 8
    Last Post: 05-01-2012, 09:10 PM
  2. JTextField Question.
    By dougie1809 in forum AWT / Swing
    Replies: 6
    Last Post: 04-01-2012, 06:53 AM
  3. JTextfield question
    By xyknight in forum New To Java
    Replies: 3
    Last Post: 04-08-2011, 05:25 PM
  4. JTextField Question
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 01:38 AM
  5. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 03:39 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
  •