Java Reference
In-Depth Information
more than one folder containing messages. For this we'll use a
JTree
widget, the Swing GUI
component for displaying text or icons in a tree-like view. The objects stored in a
JTree
must
be
Node
objects, but we also want them to be
Folders
and
Messages
. I handled this by sub-
classing
DefaultMutableNode
and adding a field for the folder or message, although you
could also subclass
Folder
and implement the
Node
interface. Arguably, the way I did it is
ar, but simpler in that its
toString()
method calls only the
Folder
's
getName()
method.
Example 17-8. MessageNode.java
public
public class
class
MessageNode
MessageNode
extends
extends
DefaultMutableTreeNode
{
Message m
;
StringFormat fromFmt
=
new
new
StringFormat
(
20
,
StringFormat
.
JUST_LEFT
);
StringFormat subjFmt
=
new
new
StringFormat
(
30
,
StringFormat
.
JUST_LEFT
);
MessageNode
(
Message m
) {
this
this
.
m
=
m
;
}
public
public
String
toString
() {
try
try
{
Address from
=
m
.
getFrom
()[
0
];
String fromAddress
;
iif
(
from
instanceof
instanceof
InternetAddress
)
fromAddress
= ((
InternetAddress
)
from
).
getAddress
();
else
fromAddress
=
from
.
toString
();
StringBuffer sb
=
new
new
StringBuffer
();
fromFmt
.
format
(
fromAddress
,
sb
,
null
null
);
sb
.
append
(
" "
);
subjFmt
.
format
(
m
.
getSubject
(),
sb
,
null
null
);
return
sb
.
toString
();
}
catch
return
catch
(
Exception e
) {
return
return
e
.
toString
();
}
}
}
These are all put together into a mail reader component in
Program: MailReaderBean
.