Moderator (MDR): John Zukowski
MDR-jaz:
Welcome to the Office Hours for April 15 for the
Fundamentals of JFC Swing: Part II. My name
is John Zukowski, with the MageLang Institute and starting at the top of
the hour, I'll be taking your Swing questions to help you through the
tutorial. As this session is moderated, your questions do not appear
immediately in the forum, but within a queue on my side. I'll forward
the questions one at a time into the forum and invite the responsible
person in for additional input. Welcome.
If you are not familiar with the tutorial, it is available at the
JavaTM Developer
ConnectionSM (JDC) at:
MDR-jaz: Seems we're a bit light for attendees so far.
If you have any questions regarding the tutorial, feel free to ask. I
can try to answer other Java questions, too.
axlrosen: Hi, here's one I haven't seen
anywhere: what's the main difference between JTextPane
and JEditorPane
? They seem to be very similar.
MDR-jaz: Welcome axlrosen.
Our first question for the day....
The JTextPane
is a subclass of JEditorPane
so they should be very similar. The JTextPane
is primarily
geered towards providing support for multi-attributed text editing/display.
The JEditorPane
is the general case and can be used for
any type of content. It provides built-in support for HTML and RTF,
besides text.
axlrosen: I'm not clear on what JTextPane
adds over
JEditorPane
, if JEditorPane
supports styled
text.
MDR-jaz: The way you specify the content is different.
JEditorPane
relies on tagged content. JTextPane
allows you to create "attributes" and tag the content when
added into the document. You don't actually see what the tags are with
JTextPane
, as the system manages them for you.
axlrosen: OK, thanks!
dhewitt: Hi. I'm working with 1.2. I've
noticed some problems with Drag and Drop. I can get it to work and drop
some cells onto Excel, but the icon does not seem to change away from
the "don't drop here symbol" I've also noticed problems where
java/my program seems to get stuck in drag-n-drop and I am unable to
release it. Any comments or
suggestions?
MDR-jaz: Welcome dhewitt
[looking for URL resource]
http://java.miningco.com/library/weekly/aa011299.htm
contains a working example of Drag and Drop where the cursor changes
properly. There is also an article at JavaWorld at
http://www.javaworld.com/javaworld/
jw-03-1999/jw-03-dragndrop.html.
It's a little tricky with the cursor, as it took me some time to get it
right. The getting stuck part seems to be related to how fast you move the
cursor in drag mode. I think hitting ESC clears things up.
dhewitt: Thanks. I'm reading them now. Do you
have any suggestions for the sticky scroll down problem?
MDR-jaz: What sticky scroll down problem? Have you
tried Java 2, v 1.2.1?
dhewitt: When you click on the scroll down
button, it often starts scrolling to the end. Kind of awkward in demos.
MDR-jaz: Oh that one. That wasn't part of the original question. Thought it was
happening during D&D. Don't press scroll down button? Vote for Sun to fix
bug? Can't help you there, sorry.
dhewitt: Mea culpa.
MDR-jaz: Do press the ESC if your drag
"hangs," as that has helped me in the past.
hani: Hi, I have a component that directly
subclasses JComponent
, and does some work in its
paintComponent
method. What methods would I have to
override to ensure that all is well when the component is added
to a JScrollPane
?
MDR-jaz: Welcome hani. If you wish to place
a component in JScrollPane
and make it scrollable, implement
the Scrollable
interface in your component. The interface
defines the methods you need to work with.
hani: Would that take care of determing whether
scrollbars need to be shown?
MDR-jaz: That's the sizing methods min/max/preferred
Size.
hani: Aha, is there a defined way of how the
JScrollPane
will look at those methods, ie, what will it compare/use
to determine when to display scrollbars?
MDR-jaz: Just look in the source to see which ones are used.
hani: Ok, thanks.
MDR-jaz: Checking to see which are called. Looks like min just calls preferred.
max is max Short.
worsch: Is there a way to rotate contents of
a panel?
MDR-jaz: No. You can rotate an Image
, but
not a panel.
[Question queue is empty right now, so feel free to ask away.]
hani - Look at ScrollPaneLayout
to see how the sizing happens, to
see if scrollbars are necessary.
worsch: How would I rotate an image?
MDR-jaz: Welcome worsch. Depends on if you
are using JRE 1.1 or Java 2 platform. Or, if you want your program to
be limited to new runtime, or all...
worsch: Java 2.
MDR-jaz: Look at Java 2D capabilities with Graphics2D
which has rotate method.
worsch: Thank you.
MDR-jaz: You're welcome.
hani: How would I provide a custom image to
be shown when dragging, as opposed to the 'system' drag cursor. And if
I need to provide a Cursor
object instead of an
Image
, is there a way of going from one to the other?
MDR-jaz: Welcome back hani. You can provide a custom Cursor
by using the
Toolkit.createCustomCursor
method. The
DragSource.startDrag
method takes a Cursor
as an
argument
hani: Ok, thanks.
MDR-jaz: You're welcome.
worsch: How would you make an area of your graphic blink in an
application?
MDR-jaz: Welecome back worsch. Why do you want something to blink?
worsch: I want to show a position on a board.
MDR-jaz: For AWT-related questions, feel free to try out Sun's online tutorial
at http://java.sun.com/tutorial.
To make something blink, draw it then erase it repeatedly.
worsch: Thanks.
insul: I have used Linux. I would like to
swing in Linux server. Does Swing support Linux?
MDR-jaz: Swing requires Java 1.1.2+, although 1.1.5+ is best. I'm not personally
aware of any issues with Swing and Linux. However, I don't run it so don't
know. Stop by blackdown.org and ask
the folks over there.
insul: jdk1.2 of Linux has many bugs. So I couldn't use it.
MDR-jaz: So don't, you don't need it. Just 1.1.x is fine. Setup in 1.1.x is
difficult in Linux. I can't help you there.
insul: OK, Thanks for your support!
hani: I have had no problems running Swing
under Linux, btw. Installation is as simple as adding the
swingall.jar
to your classpath.
MDR-jaz: [thanks hani]
ISAAC: What about all the memory leaks in
Java 2 e.g. the memory leaks in JFrame
are pretty huge...I
have a big problem defending Java 2 to my fellow programers when frames
don't get gc'ed if a couple of components have been added to the frame
(e.g a JMenuBar
)
MDR-jaz: Welcome ISAAC .
Complain to Sun, submit bug reports with reproducable results. Nothing I
can do for you here.
worsch: Am I correct in considering the
content pane as being the same as a layer?
MDR-jaz: Hi again. I believe yes, let me double check.
It depends on implementation.
hani: I have a JTree
that gets
changed a lot asynchronously from a separate thread to the main event
dispatch thread. I have found that without using
SwingUtilities.invokeLater(Runnable)
, the app hangs and
the tree behaves in an erratic manner. My question is, is there any
way to deal with this without having to incur the huge overhead of
creating so many new objects/threads?
MDR-jaz: Swing components are not thread safe. The behavior you are
getting is expected.
To reduce the number of threads created, pool them. By pooling, I
mean don't let the original thread just die off, pass it another
Runnable
to do, basically. This has been written up in
several Java trade press articles, though I don't have a URL handy.
Shemp: I have a JTextPane
and
have added text of various Styles
to it, one of which is a
JButton
. I can remove everything fine except for the button.
My question is: how do you remove a Component from a
JTextPane
?
MDR-jaz: Welcome Shemp .
Shemp: I have a Document within a
JTextPane
that I have added text of various
Styles
to it, one of these Styles
is a
JButton
. When I use Document.remove()
the
text all goes away but the button stays. Then I get
NullPointerExceptions
when I use that Style
again. My question is: how do I remove a Component from a Document
within a JTextPane
?
Hello?
MDR-jaz: Understood question, checking for an answer.
A comment from dhewitt re: earlier threading issue.
dhewitt: Just a quick comment about the JTree
question. The
SwingUtilities.invokeLater(Runnable)
does not create a
thread. It is just an object with a defined run()
method.
Threads USE this, but using Runnable
does not imply a thread.
MDR-jaz: I can't seem to find an immediate answer.
If you have an isolated example you can send me, I can take a look at
it afterwards and try things out.
Shemp: There is an example
"TextDemoSampler.java
" in the Swing tutorial the
demonstrates various text components. When I run it and try to delete
the "beep
" button, there is an exception.
MDR-jaz: Then send the bug to Sun.
Shemp: Alright...
MDR-jaz: Which Swing tutorial? The demos? Or the JDC tutorial? Or the Java
tutorial at
java.sun.com/tutorial?
Shemp: Let me look for the URL.
It's at
"http://java.sun.com/docs/books/tutorial/
uiswing/components/simpletext.html"
MDR-jaz: That's the Sun Swing Tutorial book.
Shemp: I can't seem to scroll on this applet, so I can no longer see what
you are typing. I have to signoff, and sign back on.
MDR-jaz: Sorry about that. That's the Sun Swing Tutorial book.
Versus the JDC Swing tutorial or online demos
Shemp: Yeah...they don't explicitly address
deleting the Button
in their code, so I thought maybe there
is a special thing that needs to be done, they just aren't addressing it.
Where is the JDC Swing tutorial?
MDR-jaz: Checking example now.
JDC Swing tutorial at http://developer.java.sun.com/developer/
onlineTraining/GUI/Swing2/index.html
Part II went up last week, Part I has been up for a couple months
Shemp: I'll check there for more info...thanks.
MDR-jaz: No problem. Good luck.
Well, it looks like the offiice hour is up. Do come back in two weeks for
the next session.
Shemp: Thanks...I've been trying to get this
for about a week now. :)
MDR-jaz: Have you posted anything to the Swing mailing
lists?
Shemp: No...I just discovered those in your last transcript...I'll
try that too.
MDR-jaz: The people there can be helpful.
Shemp: Hopefully :)
MDR-jaz: Thanks for all for coming. The next office hours will be in two weeks.
Same time/channel. Expect a transcript of today's session online soon in case
you missed something.
Last moderator (me) signing off.
Reader Feedback
Tell us what you think of this transcript.