"Message passing"?, and a GUI question.

J

Jan Danielsson

Hello all,

In OS/2 whenever I use an SQL database in a GUI program I use a separate
thread to collect data. This thread also preprares all the data
structures for a GUI widget that is about to display the data. Then,
when all preparations has been done, I would use WinSendMsg()
(synchronous) or WinPostMsg() (asynchronous) to let the main application
thread (where the GUI runs) know about the widget data. This means that
the GUI is "never" (never noticably) locked.

How would I do that in Java? AFAIK, it doesn't use message queues, at
least not visible, and in the same form that OS/2 and Windows uses.

This is what I know:

I can implement a thread using "Runnable"..

---------------------------
class GetMonthlyTable implements Runnable {
private Connection con;
Vector tableRows = new Vector();

GetMonthlyTable(Connection con) {
this.con = con;
}

public void run() {
Q = "SELECT ID,DATE,AMOUNT,DESCRIPTION FROM TRANSACTIONS";
...
ResultSet rs = stmt.executeQuery(Q);
while(rs.next())
{
// Add table row to Vector tableRows
}
}
}
---------------------------

That would ensure that I won't be holding up the GUI with my database
operations, now I need to get the tableRows reference to the GUI's main
thread (where I store a Vector referece which is used for all the JTable
data).

If I pass along a reference to JTable, I can easily set the rableRows
instance variable using a "set*()"-method. However, once this is done,
how do I "signal" (analogous to passing a message) to the main GUI
thread that it has new data, and the widget should be updated? AFAIK,
the GUI API:s aren't thread safe -- all GUI operations should be
performed on the GUI thread. ...or should that be: All GUI APIs are
executed on the main thread? (if so, how are asyncronous calls to the
GUI thread made?)


Also, could someone please explain what Swing is, or more specifically,
what it is not? As far as I can understand, Swing is "new" in the sense
that there is another GUI toolkit which was in Java 1.0, and that is
still a part of Java. How do I know which I am using? Are they entirely
separate (as in they can't be mixed?). I am using JTable, GridBagLayout,
JComboBox, et al. -- does that mean that automatically I'm using Swing?
 
C

Chris Smith

Jan Danielsson said:
In OS/2 whenever I use an SQL database in a GUI program I use a separate
thread to collect data. This thread also preprares all the data
structures for a GUI widget that is about to display the data.

Good plan. You will want to do the same thing in Java.
How would I do that in Java? AFAIK, it doesn't use message queues, at
least not visible, and in the same form that OS/2 and Windows uses.

Sure it does. In Java, they are universally called "events" (messages,
on the other hand, are a general OO term for what Java programmers might
call a polymorphic method call). There is a convenience method in
java.awt.EventQueue called invokeLater, which does exactly what you
want. It wraps a Runnable instance in an event and posts it to the GUI
event queue. When the GUI thread receives that message, it will
automatically unwrap it and run the code you provided. It looks like
this (of course with made-up variable/method names for your own code):

final SomeData myResult = doSomeWork();

EventQueue.invokeLater(new Runnable() {
public void run()
{
guiControl.updateWith(myResult);
}
});

The "final" is needed for the variable 'myResult' to be accessible from
within an inner class. You don't need to use inner classes for this,
but it's often convenient.
Also, could someone please explain what Swing is, or more specifically,
what it is not? As far as I can understand, Swing is "new" in the sense
that there is another GUI toolkit which was in Java 1.0, and that is
still a part of Java. How do I know which I am using? Are they entirely
separate (as in they can't be mixed?). I am using JTable, GridBagLayout,
JComboBox, et al. -- does that mean that automatically I'm using Swing?

Swing is a set of GUI components, implemented in Java, that can be used
with the AWT, or Abstract Windowing Toolkit. All Swing components
follow a number of common patterns, such as using a model interface to
abstract data from the controls, and following certain frameworks for
component focus, or look and feel.

The AWT (Abstract Windowing Toolkit) is the "old" GUI toolkit that
you're referring. It's not obsolete, though, since all Swing
applications use the AWT. It's best to think of the AWT as being
divided into two components. One half is a fairly fundamental interface
for interacting with the underlying windowing system, including the
event queue and creating and placing windows. The other half, which
Swing applications *don't* use, is a collection of native (that is, not
written in Java) widgets for things like radio buttons, combo boxes,
etc.

That's an oversimplification, but it's the best I can do in two
paragraphs.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

hilz

Jan Danielsson said:
Hello all,

In OS/2 whenever I use an SQL database in a GUI program I use a separate
thread to collect data. This thread also preprares all the data
structures for a GUI widget that is about to display the data. Then,
when all preparations has been done, I would use WinSendMsg()
(synchronous) or WinPostMsg() (asynchronous) to let the main application
thread (where the GUI runs) know about the widget data. This means that
the GUI is "never" (never noticably) locked.

How would I do that in Java? AFAIK, it doesn't use message queues, at
least not visible, and in the same form that OS/2 and Windows uses.

This is what I know:

I can implement a thread using "Runnable"..

---------------------------
class GetMonthlyTable implements Runnable {
private Connection con;
Vector tableRows = new Vector();

GetMonthlyTable(Connection con) {
this.con = con;
}

public void run() {
Q = "SELECT ID,DATE,AMOUNT,DESCRIPTION FROM TRANSACTIONS";
...
ResultSet rs = stmt.executeQuery(Q);
while(rs.next())
{
// Add table row to Vector tableRows
}
}
}
---------------------------

That would ensure that I won't be holding up the GUI with my database
operations, now I need to get the tableRows reference to the GUI's main
thread (where I store a Vector referece which is used for all the JTable
data).

If I pass along a reference to JTable, I can easily set the rableRows
instance variable using a "set*()"-method. However, once this is done,
how do I "signal" (analogous to passing a message) to the main GUI
thread that it has new data, and the widget should be updated? AFAIK,
the GUI API:s aren't thread safe -- all GUI operations should be
performed on the GUI thread. ...or should that be: All GUI APIs are
executed on the main thread? (if so, how are asyncronous calls to the
GUI thread made?)


Also, could someone please explain what Swing is, or more specifically,
what it is not? As far as I can understand, Swing is "new" in the sense
that there is another GUI toolkit which was in Java 1.0, and that is
still a part of Java. How do I know which I am using? Are they entirely
separate (as in they can't be mixed?). I am using JTable, GridBagLayout,
JComboBox, et al. -- does that mean that automatically I'm using Swing?

In addition to Chris's suggestion, you might also want to look at:
javax.swing.SwingUtilities.invokeAndWait and .invokeLater

and maybe the SwingWorker:
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

HTH
thanks
hilz
 
D

Dimitri Maziuk

Jan Danielsson sez:
....
If I pass along a reference to JTable, I can easily set the rableRows
instance variable using a "set*()"-method. However, once this is done,
how do I "signal" (analogous to passing a message) to the main GUI
thread that it has new data, and the widget should be updated?

In addition to other suggestions, you may want to look at fire*()
methods in AbstractTableModel.

Dima
 
A

Andrew Thompson

On Mon, 03 Jan 2005 18:41:01 +0100, Jan Danielsson wrote:

Since nobody else covered this aspect...
I am using JTable, GridBagLayout,
JComboBox, et al. -- does that mean that automatically I'm using Swing?

...'ask the JDocs'.
<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/package-summary.html>
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/package-summary.html>

And once you get used to what is in which package, the
light entry point to the JavaDocs is..
<http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.html>
...but if you have good broadband you might follow the 'frames'
links to a further indexes etcetera.

Of course, most developers have desktop access to the JDocs in one
form or another. They are invaluable.

Oh, and c.l.j.gui.
<http://www.physci.org/codes/javafaq.jsp#cljg>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top