Moderator (MDR): John Zukowski of the MageLang Institute
MDR-jaz: Hello and welcome to today's Office Hours for the
online Swing tutorial. We'll get started at the top of the hour. If you
have any questions, please feel free to enter them to get in the queue.
My name is John Zukowski, with the MageLang Institute, and I'll be your
host today for the next hour or so. If you are not familiar with the
tutorial, it is available at the
JavaTM Developer
ConnectionSM (JDC) at:
frasolo: Hi John! I'm very pleased to meet you
and to ask you a few questions as well...
MDR-jaz: For those not familiar with moderated forums,
questions appear privately to me first, and then I forward them into
the forum for everyone to see. I guess we'll let frasolo ask
questions first, then move on to glezy and anyone else who has
a question.
You're up frasolo, welcome.
frasolo: Are you very much familiar with JBuilder
2's Swing based, GridControl
component as well as KL Group's
JClass
Field components?
MDR-jaz: With a book out on JBuilder, I'm somewhat familiar
with the tool. I've tried to avoid the KL Group components though. Between
AWT, Swing components, and the JB components, that was enough for me. Not
quite sure what JB and KL Group has to do with the Swing tutorial though...
You still there fras?
frasolo: Fair enough...but I've been trying to
modify the navigation within a GridControl
by overriding the
MatrixSubfocusListener
implementation with no luck...
MDR-jaz: Have you posted anything to the JB-specific
newsgroups? Team B should be able to answer a question you post there.
frasolo: Yes I have, but no answer...In just a few
words, I want the subfocus in a grid to skip the non-editable cells...any
tip?
MDR-jaz:
news://forums.inprise.com/borland.public.jbuilder.jbcl
is the specific group for the component...can't think of something off
the top of my head, but I can look at it offline and try to get an answer
into the transcript.
yangtse: For KL group widgets, setFocus
method would help you skip the cells that you want.
frasolo: Thanks a lot. I've got questions but I
suspect my other colleagues are waiting for me to stop bothering you. Bye
then...
MDR-jaz: Thanks fras, just ask again and get into the
queue again. Thanks for the comment there yangtse.
glezy: Why are there packages named
com.sun.java.*
and other set using javax
.
Is there any difference?
MDR-jaz: Hi glezy, welcome.
glezy: Hi.
MDR-jaz: When Swing first came out, the Swing classes were in
java.awt.swing
in JDKTM 1.2 (now the Java® 2 Platform)
and com.sun.java.swing
for use with JDK 1.1.
Many people were very vocal about disagreeing with this setup as it required
one to maintain two source trees for anything to be delivered. One for JDK 1.1
code and another for Java 2 code. Sun then put the Swing classes in
com.sun.java.swing
for both JDK 1.1 and the Java 2 Platform,
so that the same program could run in both environments.
The Java community complained again, as this package implied it wasn't a
standard part of the Java platform, but a Sun private package...
So...it moved to the javax.swing
package, which is the
appropriate place for standard extensions. In JDK 1.1, Swing is an
extension, and you must get it separately from the standard Java runtime.
glezy: Can I just change the imports from
javax.*
to com.sun.java
.
MDR-jaz: In the Java 2 software, it is a standard part of
the libraries. If you are porting programs from Swing 1.0.3, with the
com.sun.java.swing
libraries, and moving to Swing 1.1, with
the javax.swing
libraries, for the most part you can just
change the import lines.
There are some differences and many bug fixes in the
Swing 1.1/javax.swing
classes.
glezy: So, in the future it will only be
javax.*
.
MDR-jaz: It is not supposed to change again from
javax.*
. However, I can't guarantee that it won't. Plus, some
of the packages are still in com.sun.java.swing
(motif/windows
look and feel). Mac look and feel too.
glezy: Thanks, now I understand this. I am new
in this forum, is all this information available for future reference?
MDR-jaz: The transcript for the forum will be posted online.
Don't know the specific URL yet.
glezy: Ok, I'll find out.
MDR-jaz: Should be linked off of the schedule page for the
office hours. Thanks.
franceslui: Hi John! I would like to make a
read-only JPanel
containing JComponents
. Can
you give me some ideas on making a read-only JPanel
?
MDR-jaz: Hi franceslui, welcome. What do you mean
by making a panel read-only?
franceslui: Hi! Can you give me some ideas on
making a read-only panel containing JComponents
?
MDR-jaz: If you can tell me what you mean by a read-only
panel, I can try.
franceslui: I would like to let users to turn
on/off read-only mode. Is it possible to make a read-only JPanel
.
MDR-jaz: You are still not saying what "read-only" is.
If you setEnabled(false)
a component, it will not be selectable.
franceslui? You still there? Let's move on to the next question,
if frances comes back to life, we'll continue the response later.
KSE: Would you disagree that Swing is memory
intensive (double-buffering, etc.)?
MDR-jaz: Hi KSE, welcome. Easy answer on this one...no
I don't disagree.
KSE: Hi.
MDR-jaz: Was there something more specific you wanted to know?
Or can I move on to the next question?
KSE: I've just done one project using Swing.
It's rather small, and the program takes 15 MB of memory when running,
which I think is pretty much. Other than that I don't have anything
specific.
MDR-jaz: The MVC design of the Swing components requires quite
a bit of overhead. While you may not be using it directly, the components
still do behind the scenes.
KSE: I love the design and architecture. I was
content with AWT, but now it's a whole new world.
MDR-jaz: Yes, there is quite a bit of difference between the
two.
KSE: But it's a lot to learn, if I ever get the
time. Over to someone else...
franceslui: I mean that any components inside the
read-only JPanel
cannot be edited but they are still enabled.
Thanks!
MDR-jaz: Welcome back. franceslui.
You would have to manually turn off editing of each component you didn't
want editable.
franceslui: Sometimes, my users only want to
view data inside a JPanel
and don't want to do any editing.
I don't want to disable any components inside a JPanel
because
data in components will be difficult to read.
MDR-jaz: For text components, it just involves setting the
text component to be read-only. Disabling the components is easiest, and
displaying in a different color/font. Let me move on to dan's
question as you contemplate a response.
danman1: I have a question about Layout types.
I couldn't convince my management to buy an IDE/builder, so I started
programming manually. I started with just the AWT (not Swing components).
I was trying to use a bunch of different Layout types - and everyone of
them had their problems. Without a builder, how can I easily create GUIs
and get them to layout correctly? I've used the Swing J components also,
but still am having problems.
MDR-jaz: Hi Dan, welcome.
danman1: Thanks! How's it going?
MDR-jaz: Fine here...A GUI builder will definitely help.
You can try out most of them for free. You can even try something like
FreeBuilder (open source) which is a JBuilder-clone.
danman1: OK. Thanks, I didn't know about
FreeBuilder. I was trying to lay out some simple labels and choice
buttons but I wanted them in certain places in the window.
MDR-jaz: Don't use absolute positioning. Definitely use
layout managers.
danman1: Do you know when JBuilder will fully
support the Java 2 Platform and Swing?
About the layout managers: I couldn't control the wrapping, even when I
specified the rows,columns.
MDR-jaz: FreeBuilder is available from
http://www.freebuilder.org.
Re: JBuilder, the current rev supports the Java 2 application and Swing,
just can't debug Java 2 code.
Re: layout managers, the row/columns of GridLayout
are just
a suggestion. They are only really valid if you add the same number of
components as row*column, or a couple less...GridBagLayout
provides the most control. Keep your eyes out for a tutorial on layout
managers here at the JDC soon.
danman1: OK. That explains why it didn't work even
with specifying - thanks for the tips. Thanks again, and next person can go.
mirion: I have just been working with Swing for a
short time. I am porting an app from 1.02 to 1.2. I have an existing hash (now
a HashMap
) that would like to use as a ListModel
,
but I don't have any idea how to implement this, because I must define (off
the top of my head, sorry) elementAt[index]
any idea/examples or
is this easy and I'm just missing something?
MDR-jaz: Hi mirion, welcome.
mirion: Hi guys. Thanks for the opportunity to
discuss some java today.
MDR-jaz: No problem, thanks for coming. The easiest way to use
a HashMap
as a ListModel
is to convert it to an
array (toArray())
. Of course, if it is changing, that isn't
really an option.
mirion: Correct, the HashMap
does
experience changes.
MDR-jaz: Let's see. Do you want your HashMap
to
also implement ListModel
, or can you create a new class to treat
as the list model and use your HashMap
as the data source?
mirion: I can do the latter, jaz.
MDR-jaz: If you subclass AbstractListModel
,
you would need to provide a getElementAt(int)
method and
getSize()
method. The problem with a HashMap
is the key is not necessarily an int.
mirion: Right. The key is a string. (Which is the
string displayed in the listbox).
MDR-jaz: What are you using the value part for then?
mirion: The key is an employee id. The value is a
MyEmployee
class.
MDR-jaz: Can you use a SortedMap(Tree)
instead of
a HashMap
? You need something that will always return the same
object at the same position.
mir?
glezy: Can you explain what exactly is a mnemonic?
MDR-jaz: Let me answer glezy's quick question while we
find mirion.
MDR-jaz: Hi glezy.
glezy: Hi again.
MDR-jaz: A mnemonic is a special keyboard accelerator that can
be associated to a component.
mirion: (Apologies) Boss called me in for a quick
meeting.
glezy: It is like say ctrl-b?
MDR-jaz: Hold on mir, quick q with glezy.
Normally, when you associate a mnemonic to a component, it is selected with
ALT-key and it displays a little _ (underscore) under the mnemonic.
glezy: How do I assign for example Alt-C to a
button.
MDR-jaz: So, if the Signoff button had a mnemonic of S
associated to it, you would see an _ (underscore) under the S and
ALT-S would select it. button.setmnemonic (KeyEvent.VK_S)
.
This is shown in the JRadioButton
section of the Tutorial and
discussed in the AbstractButton
section.
glezy: Oh, I see. I got it, thanks.
MDR-jaz: Oops, KeyEvent.VK_C, you said ALT-C.
MDR-jaz: Okay, mir, where were we? You're welcome
glezy.
mirion: Discussing alternative collections to
HashMap
. Yes, I could (and probably should) redesign.
MDR-jaz: Yes, last I said was can you use a sorted map/tree
instead of a hash map?
mirion: But once I started thinking about it..I got
curious to know if there was a general solution to this.
MDR-jaz: In order for something to be used as a
ListModel
, you must be able to always get the same
element back when you ask for an integer position. Well...same
element until the contents change. A sorted map would provide you
with that capability. If you need to maintain it in a Hash
table, you have to maintain the mapping yourself somehow.
mirion: I will look at a sorted map. I was using
HashMap
because I had a hash originally.
MDR-jaz: The Java 2 Collections API is much richer in terms
of available data structures.
mirion: Thanks! (next!)
KSE: I want a parent container to receive
keyboard events from all of its descendants. I've read about
registerKeyboardAction
. Will this do the trick? And if
it's very easy, can you show me the necessary code to do it?
MDR-jaz: Welcome KSE.
KSE: Thanks.
MDR-jaz: Do you need this for Swing components or AWT?
KSE: Is it possible in AWT?
MDR-jaz: Well..if registerKeyboardAction
then
Swing. To do this in AWT, it's a real pain but possible.
KSE: You have to register with every component, I
guess
MDR-jaz: There is an example of this at the JavaWorldTM
website- Tip #69
(http://www.javaworld.com/javaworld/
javatips/jw-javatip69.html)
KSE: I'm using AWT, but I'm more than willing
to switch. Ok.
MDR-jaz: For Swing, it is using the
registerKeyboardAction
.
KSE: I'll look there.
MDR-jaz: Part II of the Swing course will discuss events more.
Well, I guess I should just say "will discuss events" since they
aren't covered in Part I.
KSE: Next...
MDR-jaz: To use registerKeyboardAction
, here is a
sample to respond to SHIFT-F KeyStroke: