Speakers Hans Muller (Hans Muller) and Scott Violet (Scott)
Moderator Edward Ort (MDR-edO)
This is a moderated forum.
MDR-edO Welcome to today's Java Live! chat on JFC Swing. Our guests are
engineers Hans Muller and Scott Violet, key members of Sun's JFC Swing component
team. They'll answer your Swing-related questions. So let's begin.
pforhan: What is a realistic memory target for Swing applications?
64MB? 128MB?
Hans Muller: Small to medium sized Swing applications will run
acceptably on a 32 MByte machine. However, two or three such applications
running at the same time will probably cause more paging than is acceptable. A
64M target is a safe bet for running up to a handful of small to medium sized
applications; for running large applications like application builders, 128MB is
better.
dberansky: I'm using HTMLEditorKit to create a basic
text editor. The problem I'm having is this: when I create an empty
JEditorPane and set it to text/html, there is a 20-25 pixel margin
between the first line and the top edge of the field. Is there any way to get
rid of it? I'd like the text to be as close to the top edge as possible.
Thanks
Scott: dberansk: Our HTML support uses CSS to attach styles to the HTML
elements. The default styles can be found in
javax.Swing.text.html.default.css. You can either override these
styles in your HTML document, or try and replace them by creating your own
StyleSheet with your own modified default CSS rules. Take a look at the
javadoc for HTMLDocument and StyleSheet
for more information on doing this.
jpaiz: hans: I read "cardpanel - an alternative to
cardlayout" and tried to compile the code. I got an error:
Exception java.lang.ClassNotFoundException is never thrown in the body of
the corresponding try statement (CardPanelDemo.java: "public class
CardPanelDemo"). How can I remedy this error? Also, is this the
best way to implement an applet in the GUI style of a "wizard"
(similar to install programs on Windows)?
Hans Muller: jpaiz: If I remember correctly, the CardPanel
demo uses reflection to load classes for the panels in the demo. I'm
afraid I'm not looking at the source code now. However, if there's a line that
contains Class.forName() in the code than a ClassNotFound
exception will be thrown. Perhaps you've commented that out? As for
building wizards; you might check the wizard infrastructure that's part of the
"BeanBuilder" demo that's on the Swing Connection web site. It's part
of the article about "XML Persistence."
Scott: Question: How to enable right to left text on Swing text
components? Answer: Right to left editing can enabled by setting the component
orientation. ComponentOrientation is a property on
Component. In locales that are right to left this should
automatically be set for you.
Sandip Chitale: The current implementation allows only one
JToolBar in a panel with borderlayout. I think modern
apps require multiple independant tool bars. Is Swing going to support multiple
dockable/floatable toolbars like framework component?
Hans Muller: We don't plan to create a more elaborate toolbar framework
for the next release of JavaTM 2 (1.4).
However, if there was a great deal of interest in that we could look into it.
jtr: With the current layout managers, the layout of HTML is
problematic. For instance, JLabel can't really tell when to wrap
lines (and assumes everything is on one line). Once either the width or height
is known, the label wraps correctly. So, the problem is the label must know
either it's height or width to do the HTML layout, but the layout manager don't
provide this. Is anyone looking into this?
Scott: jtr: We are currently not investigating any special layout
managers to do this. What you may try is multiple layout passes. After the size
has been set, the component may make itself invalid again, so that you should do
another layout pass. Please fill out an RFE in the bug paragrade if you have
other specificsyou would like addressed.
Jonah: I'm just getting used to Swing/MVC. I see the value of MVC
in programming, but I don't see how Swing components help implement it. For
example: Suppose I want to "view" a model that consists of a structure
that represents the weather outside. A simple "model" would be
something like a struct or class with a few data members: double
temperature; double humidity; double visibility; Now suppose I want to
create a view into this "model" whereby I simply display (in a
TextField) these numbers. Well, if I use Swing, the only recourse
is to store a TextField's data in a *Document* model! How
unnatural! The model should stay as close to the nature of the data as
possible, not turn everything into Documents. My only alternative is to make my
"model" a subclass of Observable, and make a
TextField that is an Observer, and add it as a
listener. Then it seems that I'm not making use of any built-in
Swing MVC, I'm basically implementing my own MVC. I could resort to making my
own custom model and view, but why do I need to make my own customs models and
views for data as simple as a double or int?
Shoudn't this be part of Swing? Why aren't there basic models and views to
display simple numbers so I can store my "model" data in a form that
is more natural to the data it represents?
john: I've noticed that Swing uses ints for constants
in the API. I've occasionally used an incorrect constant because of this.
Wouldn't it be much better to use constants of a particular class type in the
API?
Scott: There is a currently mix of using ints and specifics
instances for enumerations, text uses instances in a number of places, while
other classes uses ints. We would recommend you use actual
instances, as this avoid problems you are encountering. Unfortunately it would
require incompatable changes in Swing to move to using Objects
everywhere, which is why we have no plans to go back through the API and change
everything.
Hans Muller: It's true that the Swing models are tailored to suit the
GUI components that use them. So it's often the case that a Swing model is
really just an "adapter" or a view of an application specific object.
In your case buiding a Document that mapped from three doubles to a
some text is surely more trouble than it's worth. In a case like that the
application is better off modifying the text model indirectly by setting the
textfields "text" property. As for views of primitive types like
integers and doubles; presently the only relevant model is
BoundedRangeModel (JScrollBar, JSlider), which is for integers.
Developers have often asked for a "spinner" component for displaying
integers, which we plan to add in 1.4. Presently there aren't any plans to
provide a model for floating point numbers.
paforhan: We have a large application. Swing consumes a big chunk
of memory. Any tips on reducing GUI memory footprints?
Scott: The first thing to look at is if you are loading your entire app
before the user can do anything. Often times the whole application is not used
in a typical setting, and therefore dynamically loading pieces of the
application dramaticaly changes the footprint required. Beyond that I would
recommend using a heap analysis tool, such as hat, OptimizeIt or
the like to see where your largest memory consumptions are. Often times it is
not where you think it is. We have also spent time making sure that Swing
doesn't load more than it is needed, which should also help for small apps that
aren't using large portions of Swing.
BNNRC: I have had a large problem with memory leaks in Swing. I
find that JPanels and JFrames and
JInternalFrames are causing Memory Leaks. My application uses large
quanties of such objects and I allocate large chunks of memory in subclasses of
each of these objects. I find that my only recourse is to tell my users to buy
512 MB of RAM or even 1 GB of RAM and to restart often. These bugs are all
reported on the bug parade, but have not yet been addressed in even JDKTM 1.3 RC1 is there any hope for reasonable
performance from the garbage collector and for Swing to correctly dispose of the
memory for its own objects? I writing a large 3D medical image processing
application, which will be distributed under the GPL.
Hans Muller: BNNRC: We are very concerned about memory leaks in
Swing/AWT and I apologize if the bug you submitted hasn't been looked at yet.
Please send the bugid over. I'd recommend using Optimizeit (or
JProbe) to isloate the source of leaks. One problem that people
occassionally run into with images is that an image hangs on the component
argument you provide when the image is created. People often use a
Frame or some other long lived component for this value which
causes the image to stick around.
panlasigui: Hello, I have a question concerning the proper
(optimized?) use of the JTextPane class on large documents. I use
the doc.insertstring method to insert lines into the document and
am experiencing LONG delays before anything is viewable in the
scrollpane. By large document, I mean something larger than 1000 lines of
text. The document content is just simple text and attribute assignments are
only for color. -Thanks.
Scott: panlasigui: In 1.3 we will be introducing a new View
implementation in the text package, called ZoneView. The currente
View implementations require all the children Views to be created,
as you can imagine for large documents this seems unnecessary as more than
likely the user will never view the complete document. ZoneView allows you to
defined zones of the View that will only be created when accessed for (they
become visible) . I would recommend taking a look at this class in the 1.3 RC
releases that are currently out there. For future releases (perhaps 1.4) we are
exploring turning this on by default, which will mean the text components will
comsume far less memory for large documents, and insertions should be slower.
blair: Whats the future of the layouts? I read somewhere that
GridbagLayout would probably be replaced because it is too
unwieldy?
Hans Muller: blair: The Java Look and Feel group has been building what
they hope will be a replacement for GridBagLayout. The new layout
manager uses a simpler set of constraints and it comes with a JavaBeansTM "customizer" that can be used to
configure the layout in a GUI builder that supports customizers. Or
standalone. The future of this layout manager isn't certain, it may become part
of 1.4 or it may be released as a freebie demo.
David P...: I have a Swing Application that has multiple plug in
tools. I also have cut/copy/paste menu items. Is there a way that I can tell
what component has focus? So that I can determine which tool (plug in piece)has
focus? I looked at FocusManager, but that is only good for keyboard
focus stuff. It will not handle the case that a component receives focus from a
mouse click. Thanks... David
Scott: David P: Frame defines the method
getFocusOwner, which you can use to determine what
Component has focus in a Frame.
javax.Swing.FocusManager can be better thought of as a manager to
determine what component should next get focus. It does not manager who has
current focus. For 1.4 AWT and Swing are drasticaly changing how focus will be
handled. This is in response to the numerous bugs that have been filed against
focus. There will be better API for determining who has focus, listening for
focus changes, and all around clean-up in 1.4.
john: What is the focus of Swing in JDKTM 1.4 going to be? (I'd like more support for
Toolbars too, but more support for other things is more important
to me)
Hans Muller: Our primary goals for 1.4 are smaller, faster, better (fewer
bugs). We're also working on better integration with the Windows and Macintosh
desktops, an update to the Java Look and Feel, support for Windows2000, and a
few new components; notably formatted text fields and spinners.
calcaino: I'm looking for info on dynamically changing color on a
JTable after a user has edited a cell. Do you have any suggestions?
Scott: calcaio: You can create your own TableCellRenderer
that basis its background color off of anything you like, such as when a cell
has been edited.
Eric De Broeck: I have a glassPane that intercepts
all mouseEvents. Beneath the glassPane there are
nested Containers( derived classes from JPanel). In
the case of normal mouseEventHandling, events propagates starting
from the deepest component down to the first JPanel with registered
mouselisteners where it is consumed. When I intercept the
mouseEvent and send it via dispatchEvent to the
deepest JPanel (got from getDeepestComponentAt()), it
does NOT propagate to the first ancestor with active
mouselisteners. What am I doing wrong? Thanks in advance, Eric De
Broeck Belgium
Hans Muller: Eric De Broeck: You're not doing anything wrong; the glass
pane idea doesn't really support "observing" mouse events because
there's no support for really redispatching events. This is a common request
and I've been looking into a way to support it directly for 1.4. To redispatch
events, as you noted, you need to be able to find the top most component under
the mouse that has a matching listener. Presently the bits that indicate this
are private to the AWT.
Phil Bradford: Q: To run HTML (including applets, etc.) I'm using
HTMLEditorKit and parsing through to catch these tags, trying to
process them, etc. Is there a better (easier) way I am not aware of?
Scott: Phil B: It is not clear to me if you are using the
HTMLEditorKit to create an HTMLDocument and parsing
that. If you are doing this, I would recommend you use the
ParserDelegator
(javax.Swing.text.html.parser.ParserDelegator) yourself. You simply
pass in an instance of HTMLEditorKit.ParserCallback (I believe that
is the correct name) that is notified as the HTML is parsed. This way you don't
have the overhead of creating an HTMLDocument. If you are already
doing this, then I would say yes, this is a good way to parse HTML using already
existing tools. There will be an upcoming article (on the Swing connection)
showing how you can use this parser to parse the Netscape bookmarks file and
show it in a JTreeTable.
john: Have (Metal L&F) Swing components been evaluated (perferably
by professional usability folks) to determine how usable their UI is? Because
they are visually so different from win95 and Mac, I find most users get mildly
confused when they first see them. There are bigger issues, like the details
button in the JFileChooser doesn't work, but it almost looks
enabled in the UI (why is it there at all?).
Scott: john: Yes, the metal look and feel has been usability tested in
the past. I believe with the file chooser we have just not had the time to add
the details support. We are working on it for a future release.
Rama: I have an signed applet which brings up a
JFilechooser on a button action. Sometimes the dialog takes more
than a minute to show up, especially when there are disconnected network drives.
Any tips on how to make it faster?
BNNRC: Is there going to be a better HTML parser integrated into
Swing soon. One that would have support for cascading style sheets for example.
Scott: BNNRC: Swing has had support for CSS (level 1) for
some time now. The class StyleSheet supports parsing of a CSS document. The HTML
support allows you to attach CSS in the usual ways, either in the head of a
document, as a link, or as a style attribute. If you are using the html support
in Swing, I would strongly urge you to try out the 1.3 RCs, as the support has
improved tremedounsly since early releases. That isn't to say it is complete in
1.3, just much better than previous versions.
Hans Muller: Rama--although you may not be able to speed up the file
chooser, you can prevent the application from hanging while the file chooser is
busily blocking on calls (!) by constructing the file chooser in a
low priority worker thread. Check out the second threads article on the Swing
Connection web site, Swing
Connection.
Sandip Chitale: The inactive internal frame makes the
glasspane visible to capture mouse clicks so that if the user
clicks then the internal frame can be made active. The glasspane is
then hidden. This prevents a clean use of glasspane by the user.
The AWT frames do not use similar strategy (such as use of glass pane). Why
can't the same technique be used for internal frames? The inactive internal
frame could monitor the FOCUS_GAINED events using the
Toolkit.addAWTEVentListener(FOCUS_EVENT_MASK); Check if the source
is a descendent of it and make itself active? This will not require the
glasspane based implementation. Apperantly the
glasspane interferes with Drag and Drop functionality also.
Hans Muller: Sandip Chitale: tracking focus-in events isn't a reliable
way to activate internal frames--they don't all have descendants that request
focus on mouse pressed. The AWT doesn't implement activation at all. This work
is done by the underlying window system which can use tricks like passive mouse
grabs, that we don't have access to in Swing. We have changed the way this
works a little for 1.3, check out the "changes in 1.3" article on Swing Connection.
Scott Mccullough: I am creating an application that presents
similar to the Windows NT explorer and similar displays. A split pane with a
tree on the left and when a node is selected, its children on the right.
Usually there are four views to choose from. Small icons, large icons, list and
detail. I have detail working as a JTable. I cannot figure out
how to show the other three formats. I believe they are all similar and
figuring out one will guide me through the rest. I've tried a
JPanel with JTextFields, JLabels and JButtons. Each
have problems. Any ideas? Any samples?
Scott: Scott Mc: You will probably want to use a JList with
a custom ListCellRenderer. JList currently does not
support more than one column though (newspaper type layout) as you will often
see in the Windows Explorer. You could either add this support yourself, or wait
until 1.4, which is when we plan on adding support for multi-columns in
JList. Another problem will be editing, JList
currently does not support editing, although again this should not be that hard
to add yourself.
Dan Pritchard: How can I programmatically select a row in a
JTable? JTable.setSelectedRow(int) does not exist?
Scott: Dan P: JTable uses a ListSelectionModel
to maintain which rows are selected. You can get a handle to the
ListSelectionModel it maintains and then invoke
setSelectedRow on it. I believe JTable also has a
method called changeSelection, which is a cover for this, you may
want to investigate this as well.
MDR-edO We're down to the last ten minutes of the chat. However, because
of the volume of questions in the queue, we'll go overtime--at least until 5
after the hour.
Richard Stack: I find I am often using JTable to
display data that should be in objects, but I have to return it from the DB as a
Vector of Vectors because this is the way it's
optimized in AbstractTableModel. Is there any way to use a
Vector of MyObject (Customer for example) with
getters and setters and have it work with
AbstractTableModel. Regards richard
Hans Muller: Richard Stack: you definitely don't want to copy the DB data
into an TableModel unless there's no way to index it by
row,col. What you really want to do is build a subclass of
AbstractTableModel that has a reference to your vector
of MyObject objects and maps column indices to
MyObject properties.
Sandip Chitale: Can Swing support a JMenu factory,
which has all the menu items to switch to all possible Look and Feels? The
action handler for the menu items could travers all the JFrames in
Frame.getFrames() array and switch their look and feels to
selected look and feel. Alternatively there may be an explicit registeration
step to register the JFrames whose l and f should be changed
(similar to ToolTipManager.registerComponent strategy).
Hans Muller: Sandip Chitale: The truth is that we talked
about providing a utility class that did this for quite some time however we've
never quite found the time to do a good one. The code in the
SwingSet2 demo is a pretty good example of this.
Kin Wong: Are there any techniques to improve scrolling in a
JTable with a large number of rows?
Scott: Kin W: With 1.2.2 we added a client property on
JViewport that when set enabled a different strategy for scrolling
that is often times faster, and consumes less memory. This strategy is the
default in 1.3. If you are using 1.2.2, take a look at the javadoc
for JViewport to determine how to enable it.
john: I'm thinking of entering a RFE to place an event /first/ on
the AWT event queue. This would help resizing of components from a
ComponentListener among other things (if the
ComponentEvent isn't first on the event queue you get the UI
components doing a dance as one updates, then the next). Any plans to add this?
(BTW: I think Swing is a great architecture, it is usually very easy/fun to
use!)
Simon: Hi, my question relates to cursor problems I have with
JDKTM 118 and Swing111. When I am reacting to
an event that will take time to finish, I cannot set the cursor inside this
event handler, because the GUI is frozen (the AWT thread is executing my code so
how can it change/repaint a different cursor on a Swing component), isn't it?
What can be a workaround for this behavior if it not a bug nor my mistake?
Almost all complex Java applications I've seen does not show the hourglass when
doing long works (for example, Forté for Java, even if I appreciate it very much
:-)
Hans Muller: Simon: this is one of the most often reported AWT bugs, and
I apologize for the pain and suffering. It is fixed in 1.3 release (and in 1.2.2
I believe). BTW--If you're doing something time consuming on the event
dispatching thread, you should really be using a worker thread, you don't want
to hang the application. Take a look at the 2nd threads article on Swing Connection.
Scott: john: What you are hinting at is adding ordering to the beans
event notification. This has been discussed numerous times though, and there are
currently no plans to do it though. If you have a proposal for this, we would
love to read about it.
Larry Lace: What versions of Netscape and IE support Swing for
JDK 1.2 and upcoming 1.3? Area there and special requirements.?
glen_s: When will long term persistence of serialized objects be
implemented?
Scott: Larry L: I do not have a definitive answer as to which versions, I
believe we support NS 4.0 and newer, and IE 4.0 and newer. These are supported
through the use of the plugin, which you can get more information about from
java.sun.com/products/plugin. Each release of the JRE includes a new version of
the plugin.
MDR-edO Well our time (and overtime) has ended. I want to thank all of
today's partricipants for some excellent questions, and especially our guests
Hans and Scott for their fine answers.
Hans Muller: glen_s: We've been working on this problem for some time;
last year at JavaOneSM we presented the status
of a project to provide long term persistence for JavaBeans in general -
including Swing components. This project has been going very well, it's now
part of the "Java Community Process." You can find out more on the Swing Connection web
site--there's an article about the project there and a prototype implementation.
You'll find our JSR (Java Specification request) on Java.sun.com/JCP under the
link for "JSRs accepted for development".
Scott: Thank you for your questions. For those of you that have unaswered
questions, I would recommend you send them to Swing-feedback@eng.sun.com. Other
resources you can use include the Swing Connection. Sorry for any
other resources I forgot to mention.
MDR-edO Last moderator (me) signing off. The forum is now unmoderated
_______
1 As used on this web site, the terms "Java
Virtual Machine" or "JVM" mean a virtual machine for the Java
platform.
Reader Feedback
Tell us what you think of this transcript.