tags:

views:

136

answers:

3

Some swing code I write in my computer behave different on my colleague's computer, and in my PC, and in my notebook.

I wonder, is there something I can do to my Swing applications behave the same in every computer?

I want to have sure a algorithm I've tested in my computer will work the same way in my clients computers.

E.g. Problem to focus JTextField works fine in my notebook with Windows XP, but not in my collague's computer with Windows XP, nither in my work computer with Ubuntu.

obs. the specific JTextField problem is not the subject of this question.

+3  A: 

Problems with Swing apps on different platforms are common and they are caused by the simple fact that no matter what level of abstraction Java offers it has to play ball at some point with the native components of the underlying operating system. Event though Swing only uses the windows(frame) and draw everything by itself - discrepancies are very very common.

I develop a mutliplatform Swing application - and users on Windows are reporting all sorts of issues that Linux users don't have and vice versa. Sadly there is no silver bullet for such problems - extensive testing and nasty fixes are the only game in town.

And everything come exceptionally buggy and dirty in the area of pluggable look and feels. For example - resizing a JSplitPane with metal or nimbus is super fast(as expected), but if you use GTK+ plaf, everything goes to hell. This is a more serious(performance) problem - visual problems(missing borders, components not fitting properly containers, etc) have no end... Despite all of this Swing continues to be one of the best bet for multiplatform desktop applications.

Bozhidar Batsov
@Bozhidar Batsov: +1... me too I'm shipping on Windows/OS X a Swing app and the issues are crazy. People who drank the Swing cool-aid and never actually worked on a complex mutli-platform Swing app shouldn't answer nor comment on this question :)
Webinator
Its worst than that. In the example I've posted, the swing is behaving diferent in 2 Windows systems.
Tom Brito
+2  A: 

I wonder, is there something I can do to my Swing applications behave the same in every computer?

I'm working on a complex Java Swing app that is shipped on OS X / Windows / Linux so just like Bozhidar answered, the issues are all far too real.

For some components, if you happen to have some UI design/programming skills, you can simply write your own component: I realize it's probably not a helpful answer, but it works.

For example, we wanted a drop-down "find-as-you-type" popup list (like the one that appears when you start a search on Google's main search page) that would look and work the same on Linux/Windows/OS X. After trying countless of "solutions" full of Swing idiosynchrasies that would not work everywhere (like, guess what, focus issues ; ) we decided to simply write our own component "from scractch".

We can intercept mouse and keyboard events on both OS X / Windows / Linux: we can write a component that not only looks but also behaves identically on all three platforms.

In addition to the "find-as-you-type", we also wrote our own tooltip-popup component, a dual progress bar (to progress bar in one to show producer/consumer style progress in a single bar) and a complex component involving several "text fields" which was absurdly complex and broken when we tried to do it using Swing (and broken in different ways on different platforms, like weird focus issues or caret not showing, etc.). So we "went dirty" and rewrote the entire component ourselves.

Result? Working identically on all platforms where Java can give you notifications about mouse and keyboard events...

I realize this may not be what you want to hear: I happen to have worked on both games UI and mobile apps UI back in the days and I have some graphic skills so it's not "hard" for me to write good looking UI components.

Sadly if you want some Java UI component to look and behave identically on all platforms, it's sometimes your only alternative...

Webinator
+1 from me as well. I feel your pain - I've written a lot of custom components myself. And the focus traversal policy of Swing can only be described as "black magic"... My other "favourite" multiplatform issues are clipboard integration and sys tray integration...
Bozhidar Batsov
the worst is that the example I've posted show that even in 2 Windows systems the swing app can behave different.
Tom Brito
A: 

I want to have sure a algorithm I've tested in my computer will work the same way in my clients computers

An "algorithm" should work fine.

You get problems when you rely on the ordering of events, which may be different on various platforms. One of the most common I know about is holding down a key so that is repeats:

a) on Windows you get keyPressed, keyPressed, keyPressed, .... keyReleased.

b) on Unix you get keypressed, keyReleased, keyPressed, keyReleased ...

By the way a comment would be nice as to whether my suggestion in your "textfield" posting works or not. As I mentioned, I don't have a Ubuntu platform to test it on, so I'm curious as to the result.

camickr