Results 1 to 10 of 10
  1. #1
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Question JTextField won't update from subclass

    I have a JTextField and a function in the main class to update it.

    I created a separate class to query my database and populate the text field.

    I can println the values but when i .setText() to the values, the GUI text field does not update.

    However, if I println .getText, it prints the value.

    Now, if I call the function from the main class with a hard coded value passed to it, then it updates on the GUI.

    Could this be thread related?

    Any tips on the best methodology for updating a GUI from DB queries? The last thing I want to do is put all my database calls into the main class. ::shudder::

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

    Default

    I think that it's quite possibly thread related. Your database-related code should be performed on a background thread lest it tie up Swing's event dispatch thread or EDT, the single thread responsible for painting Swing apps and for allowing user interaction.

    Your database calls should be part of your persistence layer, probably not in a main class or in the GUI layer, and you may wish to consider using SwingWorker objects to help you keep background processes from freezing your GUI.

  3. #3
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    Well if I set variables in my database class, I can reference them from my calling class.

    The event in my calling class is

    Java Code:
    		public void actionPerformed(ActionEvent e)
    		{
    
    			txtCustomerName.setText("");
    			try {
    			getOrders LO = new getOrders();
    			LO.listOrders();
    			txtCustomerName.setText(LO.customerName);
    
    			} catch ( Exception x ) {
    			x.printStackTrace();
    			}
    		}
    I don't really like this though. Is this how it should be done? Why can't my getOrders class reference a function in the calling class that sets my GUI fields?

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

    Default

    Your Controller class could possibly link the database calls and your GUI. As a quick fix, what if you use a SwingWorker?

    Java Code:
      public void actionPerformed(ActionEvent e) {
    
        txtCustomerName.setText("");
        new SwingWorker<String, Void>() {
    
          @Override
          protected String doInBackground() throws Exception {
            getOrders LO = new getOrders();
            LO.listOrders();
            return LO.customerName;
          }
    
          @Override
          protected void done() {
            try {
              txtCustomerName.setText(get());
            }
            catch (InterruptedException e) {
              e.printStackTrace();
            }
            catch (ExecutionException e) {
              e.printStackTrace();
            }
          }
    
        }.execute();
    
      }

  5. #5
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    I'm still learning the basics of Java, but I downloaded SwingWorker and put your code in.

    I don't know anything about SW yet to know how to write/reference it properly, but I might as well learn things the right way from the get go than learn it the wrong way.

    Putting your code into play generated an error.

    .\MainFrame.java:74: > expected
    new SwingWorker<String, Void() {

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

    Default

    Your code is not the same as mine. Please look at the line you posted vs. my code.

  7. #7
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    I went through it, I only posted the subsection of code.

    I just read a tutorial on SwingWorker and just tried a very basic example without any additional code or exception handling so I could just focus on understanding SwingWorker and not worry about syntax or errors in other areas.

    Here is what I've taken away from the tutorial.

    Java Code:
                SwingWorker worker = new SwingWorker() {
                    public Object construct() {
    			getOrders LO = new getOrders();
    			LO.listOrders();
    			txtUsername.setText(LO.customerName);
    			return 0;
                    }
    };
    worker.start();
    So if I understand this correctly, this just splits off into it's own thread so the GUI is not tied up.

    The text field still does not update from a function being called in my getOrders class, so I still have to set it referencing a variable in the getOrders class.

    That kind of seems like it violates encapsulation but I'm still green on Java. (I started learning Java yesterday, but at least I can interact with my database and make a GUI).

    Thanks for your help so far! Can you above and beyond and explain what each line of code you have is doing? The whole "teach a man to fish and he'll eat forever" concept rather than just giving me the code.

    Reading the API, reference manual, etc., is very time consuming and much of it I don't fully grasp. I personally learn better jumping right in and using the API and reference manual as a go to book when I get stuck on something.

  8. #8
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    Just to give your code a solid shot, I blew my code away and copy/pasted yours. This is the error I get from it now.

    javac Application.java
    .\MainFrame.java:74: type SwingWorker does not take parameters
    new SwingWorker<String, Void>() {
    ^
    1 error

    I'll read up on the API. I still have to learn what a persistence layer is and how I create one for my database. I did a quick Google and that, along with SwingWorker, looks like something that would take me more than an hour to learn.

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

    Default

    My code is for Java 1.6, and I'll bet you are compiling with a previous version. You don't absolutely need a SwingWorker to create a background thread, but it does simplify the effort.

    If you'd like to avoid using SwingWorkers, then create a standard Runnable, place it into a Thread, do your database work, and then when you want to update the GUI, make sure that you do it on the EDT via SwingUtilities.invokeLater(...)

    e.g.,
    Java Code:
      public void actionPerformed(ActionEvent e) {
    
        txtCustomerName.setText("");
        
        // create a new background thread and start it
        // add a Runnable object that does our background work.
        new Thread(new Runnable() {
          public void run() {
            final getOrders LO = new getOrders();
            LO.listOrders();
            
            // use invokeLater and another Runnable object 
            // to call code on the EDT
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                txtCustomerName.setText(LO.customerName);
              }
            });
            
          }
        }).start();
      }
    Please read this too: Concurrency in Swing

  10. #10
    Edward is offline Member
    Join Date
    Jun 2009
    Posts
    7
    Rep Power
    0

    Default

    Good info. I was reading that on Sun's site early but not in detail.

    So it looks like SwingWorker is built in to 1.6 then eh?

    I'm on 1.6.0_14.

    I had downloaded SwingWorker.java off a web site and let the compiler find it. So your code would need import javax.swing.SwingWorker which may be different from SwingWorker.java, correct?

    I'm falling asleep so I'll dig into this later on.

    Thanks for all your help!

Similar Threads

  1. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-21-2009, 12:46 AM
  2. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 08:29 AM
  4. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:03 PM
  5. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM

Tags for this Thread

Posting Permissions

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