Swing JTable
I've been working hard these days, and I almost have an usable version of PennyBank. Right now, users are able to create and delete users and accounts, operate with those accounts (deposit, withdraw and transfer), displaying account operations, and importing movements from Cashbox PList files.
I will provide with some screen-shots as soon as possible, but today I wanted to talk about Swing JTables and their TableModel property. Tables are great tools when you need to display a collection of data, so you can arrange it in rows (one row per element in the collection) and columns (one column per attribute in the element).
JTables are quite easy to use. You need to declare the table itself, give it a model, and display it in your panel. Depending on the model you choose, you can edit the table's cells, you can display the elements according to their type...
Even when Sun's tutorial is nice enough to have the basic necessities solved, these three articles I found on InformIT are very good pointing at the changes you may probably want to do with your tables:
Now, I still have to give my table the OS X standard Look & Feel, painting even and odd rows with different colors each one. This link provides some good steps to accomplish that task.
Some (unasked) explanations
When I started developing PennyBank, the first decision I faced was the programming language I should use. I wanted something that looked integrated in the system, like iTunes, Mail, iCal... The first choice was obviously Objective-C, with the interface done in Interface Builder. I had followed a small tutorial, and doing a trivial application (a calculator, I think) had showed me how good its visual programming was.
In my job I work using C# with Visual Studio 2008. I can develop GUIs real quickly, without having to withdraw time from what it is usually more important: the business logic. Here, I wanted more or less the same. I wanted a language which could let me focus on the logic without too many headaches when doing the GUI. So Objective-C seemed the right option, as there was that Interface Builder tool. But after just some searches in Google, I realized that I wouldn't be able to get the application done if I chose a language I was not familiar with.
And so I chose Java. I've done many small apps in Java when I was studying, so I feel quite familiar with it. Besides, I wanted to try Hibernate, so this seemed a very good excuse. The other reason to choose Java was SWT. Some friends and I developed an application last year using SWT. We were targeting Windows, but I recall reading that SWT was "the ultimate Java interface" in terms of native Look & Feel integration. As our application got to look great in Windows, I started coding in Java with the idea that SWT would save all my problems.
I was wrong. After two weeks developing the model layer with Hibernate, I wrote the first lines of code for the GUI. It was quite strange: I needed several days to notice that something was going wrong. The first days it was due to the following:
The special VM option -XstartOnFirstThread is also required for SWT applications to run properly on the Mac.
It is stated clearly in the Mac OS X section of their page, but somehow I didn't read it, experiencing some real awful exceptions until I found it. After that, I had several issues that almost have PennyBank development stopped. First of all was the lack of a good Visual Editor in Eclipse. OK, there was the Visual Editor plug-in , but I wasn't able to get it working: its support seemed to have been dropped off, and there was no sign of activity in its section for the last two years (Jump here if you don't want to read the rest and want to find which solution I found). After that, I wrote the first example of a toolbar.
It was... weird. I was looking at the main window, knowing that something wasn't right, but unable to tell what it was. The toolbar! This was NOT an OS X toolbar!After that, I knew that I had no reason to keep using SWT, as it wasn't giving me the integration I was expecting, nor it was giving me any ease of visual programming. I found a blog where someone built an unified tool bar using SWT, but it looked too much of a hacking for me. So I moved to Netbeans: I knew it had a tool for writing GUIs so even if I would be coding a not-so-nice application, at least it would be faster.
Using Maven proved here to be a very wise decision: migrating from an IDE to the other was painless. But I didn't stay with Netbeans too much: after an strange problem with Subversion, I moved back to Eclipse. At the beginning, I even thought about developing the interface by hand. Fortunately, I found a better solution.
Even if the plug-in was officially unsupported, in this page a link is provided to have it working under the last Eclipse build. Now that I was able to do visual programming, the only point missing was the so much wanted operative system integration.
So I started searching for "Java OS X integration", finding lots of resources... until I found The One. Exploding Pixels writer has developed Mac Widgets for Java: some great widgets which gives the application a native look and feel. Another must have link is the Java Development Guide for Mac OS X. With this one and the Apple Human Interface Guidelines I think I have all the technical information I need.
Introducing PennyBank
Pennybank is an Open Source personal accounting application, aimed to allow users to keep track of the most common financial operations: incomes, expenses and transfers, so it makes easier to keep your economy controlled.
It is written in Java, and developed with a good Mac OS X integration in mind.
An early screenshot can be seen at the PennyBank picasa gallery.
