Results 1 to 10 of 10
- 06-29-2009, 01:57 AM #1
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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::
-
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.
- 06-29-2009, 02:21 AM #3
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
Well if I set variables in my database class, I can reference them from my calling class.
The event in my calling class is
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?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(); } }
-
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(); }
- 06-29-2009, 02:52 AM #5
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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() {
-
Your code is not the same as mine. Please look at the line you posted vs. my code.
- 06-29-2009, 03:24 AM #7
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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.
So if I understand this correctly, this just splits off into it's own thread so the GUI is not tied up.Java Code:SwingWorker worker = new SwingWorker() { public Object construct() { getOrders LO = new getOrders(); LO.listOrders(); txtUsername.setText(LO.customerName); return 0; } }; worker.start();
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.
- 06-29-2009, 03:37 AM #8
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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.
-
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.,
Please read this too: Concurrency in SwingJava 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(); }
- 06-29-2009, 05:45 AM #10
Member
- Join Date
- Jun 2009
- Posts
- 7
- Rep Power
- 0
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
-
subclass troubles
By xf021209 in forum New To JavaReplies: 12Last Post: 04-20-2009, 11:46 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM -
Subclass definition
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:03 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks