JFC/Swing musings (repost)
I am not a JFC/Swing expert. But here are some musings about JFC/Swing development. Resposting from my personal page.
JFC/Swing musings
- Learn how to use javax.swing.AbstractAction.
- Never extend JFrame. Use JPanel or JComponent instead. Think components for IDE's etc.
- If you create throwaway layout code.. you will throw it away.
- new ActionListener() { actionPerformed() { someMethod(); }} is not a good idea. I don't like Microsoft's delegates either. Use AbstractAction if it makes sense. If you have a lot of inner classes, it will probably increase your blood pressure and reduce code reuse.
- Don't extend JButton because "it's not good enough". The programmer, and not the API, is usually the problem.
- Don't add "pretty colors and stuff" to Swing components without testing them on Macs or Mac users will hate you for it.
- Support the keyboard. Add mnemonics and shortcuts. Solve component focus problems.
- Understand that the "everything off by default" mentality of Swing is 50% design and 50% "boring technical reasons". The Flash 9 plugin just came out on Linux. Cross platform code cause all kinds of boring problems. Try image tiles on the Direct X and OS X OpenGL pipeline as an example.
- To hide a window on a Mac you hit Command-W. Quit is Command-Q. The about dialog must be implemented through a custom Apple API. Learn the .app format. On Windows you should use a .exe wrapper. If you have seven javaw processes running, which one is the Swing application that just crashed ?
- Make sure that "lazy loading" is not a case of lazy developer. Users loathe software that gets a heart attack if you press the wrong key. Listen for the users frantic ESC ESC ESC commands.
- My mom presses the shutdown button. I rip out the power cable. Lost or corrupt data should be thing of the past. Most of my data is in a PostgreSQL database. Good IO code is a skill set on its own.
- A user should always be able to turn off your custom Look and Feel code. Otherwise your application will not be future-compatible. Think Vista, Leopard, high DPI LCD/Plasma.
- Use Eclipse and learn how to refactor code. It is like the old search/replace but much better, global and undoable. Netbeans is fun for creating component prototypes. The latest GUI builder is pretty nice. It does suffer from the old Layout-Used-Not-In-JDK syndrome (Sun, IBM, Borland - I am looking in your general direction)
- Thinking in bitmaps and pixels might bite you in the ass later. Test on multiple DPI settings and check any custom fonts. Some Linux distributions have really funky fonts. Resolution independent vector images is probably the future, but support is scarce. Also note that Photoshop (bitmap/vector) is more popular than Illustrator (vector) for creative work. There are fewer vector monkeys than bitmap monkeys.
- Try to understand property listeners. They are so boring that you forget how to use them properly.
- Standardize on one text encoding. I try to use UTF8. Line breaks is still an issue. Try CR in Unix perl. Understand this if your application does IO pipes and runtime commands.
- Check your Swing data models. Incorrect sorting, bad threading and generally being a dumbass can cause data corruption. This is not a Swing problem, but rather that an inexperienced developer (like myself) is given plenty of ways to shoot himself in the foot. Often system A works fine.. but when connected to systems B and C (developed six months later) you end up in trouble.
- Badly written IO and threading code might not manifest itself in the same way on different platforms. A bad UI routine might look good on your platform.
- Some people believe that "intelligent design" should be used in software development. I am more of a.. let's see what just crawled out of the primordial ooze kind of a guy. Evolution followed up by learning (refactoring). Instead of writing pretty code I write horrible code. If you have never written horrible code, how do you know what "good" code looks like ? Take pride in creating the most horrible piece of garbage the world have ever seen and then making it much worse. I know this is not very professional, but there are so many wrappers around wrappers around wrappers out there that it drives me to do this. How many layers of indirection do we need exactly ?
- Try not to copy/paste code. I know about the management problem relating to deadlines and such, but copy/pasted code often results in a "rewrite from scratch".
- "XML will solve everything" ? Most web developers use CSS for layout. The only layout code done in XML is the basic layout. Left, right, middle, top, bottom,box1-2-3, add script, bold, address, abbr etc. Global layout stuff usually ends up in a CSS file. Please understand that Microsoft XAML, Mozilla XUL and Adobe Apollo are huge frameworks. It ain't "simple". Lock-in and version dependencies are going to to be a bitch.
- Don't listen to "experts". Listen to people who have created something and that can give you a zip file or a URL. There are so many people advocating that Hello World-type applications should be written using the Netbeans or Eclipse framework. Come on already...
- Nuclear powered aircraft carriers SUCK at driving on land. Therefore.. BikeFramework will rule the seas !! Understand what you are creating. Is it a mountain bike or a Space Shuttle ?
-
UI threading is hard. I have my own way of doing it.. but I don't want to make it public since I am very talented in the arts of IllegalThreadStateException's and JVM crashing.
More information on Swing threading.
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
http://foxtrot.sourceforge.net/docs/index.php
http://spin.sourceforge.net/
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html