Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2009, 02:57 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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::
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-29-2009, 03:17 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-29-2009, 03:21 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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

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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-29-2009, 03:37 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Your Controller class could possibly link the database calls and your GUI. As a quick fix, what if you use a SwingWorker?

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();

  }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-29-2009, 03:52 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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() {
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-29-2009, 03:54 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
Your code is not the same as mine. Please look at the line you posted vs. my code.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-29-2009, 04:24 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-29-2009, 04:37 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-29-2009, 04:46 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.,
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-29-2009, 06:45 AM
Member
 
Join Date: Jun 2009
Posts: 7
Rep Power: 0
Edward is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
gettext, gui, jtextfield, recordset, settext

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclass troubles xf021209 New To Java 12 04-21-2009 12:46 AM
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents sumit1mca AWT / Swing 1 01-30-2009 07:44 PM
superclass and subclass mr idiot New To Java 19 01-03-2009 08:29 AM
Subclass definition Java Tip java.lang 0 04-23-2008 09:03 PM
subclass vs inner class bugger New To Java 1 01-13-2008 08:31 PM


All times are GMT +2. The time now is 05:53 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org