Hi Nick,
I'm kind of in the same position at the moment, the tutorial on
java.sun.com tells you how to use individual components but doesn't
give good guidance about overall best practice. The Swing Tips and
Tricks book is worth reading but doesn't quite hit the nail on the
head. The Filthy Rich Clients book is a fine book but not really
relevant to your question, it's mostly about creating pleasing effects
with shadows, transparancy etc. which is not really a priority when
you just want to assemble a solidly engineered GUI app. Apart from
that, I haven't come across anything that gives best practice advice.
Here's a few tips though:
1. Break your GUI into logical components. Most of the time your
component will extend JPanel, that is, it will be a JPanel with some
stuff added to it
2. If you use netbeans, try the built-in GUI builder (matisse). You
can drag and drop standard swing components from the palette but more
importantly, you can drag and drop your own components, just drag your
class onto the form and it will render it for you
3. There's really only one game in town as regards the standard layout
managers and that's GridBagLayout, it's futile to try to avoid it. On
the other, there are some nice layout managers out there which are not
part of the standard JRE, most notably MigLayout. If you find yourself
hating GridBagLayout, give Mig a try.
Now, on to design...
Try to avoid a situation where the GUI stores the state of your
program. What I mean by this is that the GUI should reflect the state
of your system rather *be* the state of your system. The way I
generally approach this is to have a system wide singleton (called
Application) which stores that state of the program. When the state of
Application changes, your GUI should react to this. Consider the
following example (some of the more experienced posse members might
comment on this approach):
You have created a swing based mail client. Your client has a
background thread that constantly checks that it can connect to the
mail server (not necessarily a good idea for a mail client in general
but may be acceptable in some limited scenarios). In the event that
the server is not reachable, a number of things happen in the GUI, a
status icon will change, some buttons will become disabled along with
other things that your customer will think of later.
The connection thread should not know or care anything about the GUI
components, its job will just be to call setOnline() in Application
Your Application object could dutifully make all sorts of changes to
the GUI whenever setOnline is invoked but to do this, it would need to
know about the internal structure of your GUI and that's what we want
to avoid. Instead, you might construct your Application class using
the following template:
public class Application {
private boolean online;
private String foo;
private static Application application;
private List<ApplicationListener> applicationListeners;
// Make the constructor private, we don't want anyone calling it
directly
private Application() {
}
// We only ever want a single instance of Application
public static Application getInstance() {
if (this.application == null) this.application = new
Application();
return application;
}
public boolean isOnline() {
return online;
}
public void setOnline(boolean online) {
boolean oldOnline = this.online;
this.online = online;
fireOnlineChanged(online, oldOnline);
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
String oldFoo = this.foo;
this.foo = foo;
fireFooChanged(foo, oldfoo);
}
private void fireOnlineChanged(boolean newOnline, boolean
oldOnLine) {
for (ApplicationListener listener : applictionListeners) {
listener.onlineUpdated(newOnline, oldOnline);
}
}
private void fireFooChanged(String newFoo, String oldFoo) {
for (ApplicationListener listener : applictionListeners) {
listener.fooUpdated(newFoo, oldFoo);
}
}
public void addApplicationListener(ApplicationListener listener) {
applicationListeners.add(listener);
}
}
public class ApplicationListener {
public void onlineUpdated(boolean newOnline, boolean oldOnline) {
// Do nothing
}
public void fooUpdated(String newFoo, String oldFoo) {
// Do nothing
}
}
Now, if a the enabled state of a JButton depends on whether the
application is online, you can do something like:
JButton btn = new JButton("Click Me");
Application myApp = Application.getInstance();
ApplicationListener myAppListener = new ApplicationListener() {
public void onlineChanged(boolean newOnline, boolean oldOnline) {
btn.setEnabled(newOnline);
}
}
myApp.addApplicationListener(myAppListener);
You can repeate this process every time you create a GUI component
whos state depends on the application.
Cheers,
-phil
On Mar 4, 2:32 pm, "[email protected]"
<[email protected]> wrote:
> Hi
>
> I'm a fairly experienced Java developer but haven't done much with
> Swing beyond wrting some simple tools for my own use. I'm looking for
> a Swing book that will show me best practice for setting up and
> implementing a medium to large scale Java desktop application. Any
> recommendations welcome.
>
> Cheers
>
> Nick
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.