Stop, or I'll shoot!
So far, we've seen a few of different things that we might be able to use in a Jython Swing application, but lots more remain. Today, let's take a different approach.
Scripting from Scratch
In April, I had an article published on developerWorks entitled Scripting from scratch: Creating a Jython administrative script for IBM WebSphere Application Server. In the first 4 months, about 8,000 people have review it, so I thought that something similar here may help people with their Jython Swing application development.
What are we going to do?
Let's see what it takes to develop a simple Swing application from scratch (meaning "from nothing"). Because this is a blog, we're not going to be too ambitious, at least not yet.
How are we going to do it?
We're going to start with an absolute minimum script, and iterate over it as we better understand what needs to be done. All this means is that we're going to keep adding pieces in order to approach our idea of what we want our application to do.
Aren't we going to make mistakes?
Certainly. That's how we learn. It's when we make mistakes that we better understand what we're trying to do. As we make mistakes, and figure out why they are mistakes, and figure out to to correct them that our knowledge improves.
Where do we begin?
Let's start, as we said earlier, with an absolute minimum Swing script. How does that look? Personally, I like to start with something like this:
It can be executed, and even though the default application window size doesn't allow us to read the complete title, we can resize it, and test it to verify that:
- The application starts, and stops cleanly.
- No error/exceptions are generated.
- We can use the mouse, or keyboard (if we know how), to move, resize, and even close the application.
- The application can be terminated simply by pressing <Enter> at the command prompt used to start the script.
Next, we have to come up with some idea for an application that is simple enough to do, yet interesting enough for us to write. What if we write a simple Stop Watch application? How simple? Well, just enough to measure some elapsed time.
What do we add first?
Let's start by:
- Changing "ToBeDeterminted" to "StopWatch".
- Adding a "Start" button.
This makes our application look like:
What does the application window look like now?
That's a start, isn't it? What else do we need? Oh yeah, we're going to want to have someplace to display a message, aren't we. Alright, let's add a label to the application. What does that do for us?
What does that look like?
What's the difference?
I don't know about you, but I don't see anything different. Why is that? Well, the reason is that when we created the label, we didn't initialize it with any text, so it requires no space on the application window. That's why.
Where's the "Stop" button?
Oh yeah, we have a "Start" button, but we don't have a "Stop" button. We also need to either addActionListeners to each button, or define the actionPerformed routine as part of the button creation step. Let's do those, and see what we get:
OK, let's add a stop button, and see what the code looks like now:
Now, what does that look like? Especially:
- Before pressing either button.
- After pressing the "Start" button.
- After pressing the "Stop" button.
Now, all we have to do is add some code to the "Start" and "Stop" button event handling routines to perform the actual timing related role. What does it look like after we do that?
What does it look like now?
Alright, after making these changes, what does it look like?
Did you notice anything different? For example, why is there a gap (empty space) between the two buttons? See how the creation of the JLabel was done (line 20). The initial value is now a string of 45 blanks. When the frame.pack() routine is called, this is taken into account, and the relative positions of the other components (e.g., the buttons) are adjusted accordingly.
Did you also notice how nicely the "elapsed" message fits in "available" space? What would happen if we left the timer run for a "long" time? What happens to the application? Does the window get "resized"? (No, the message will be "truncated", and the "seconds" will be shown as something like "seco...".)
Are we done yet?
Well, the answer to that is "it depends." On what does it depend? Well, remember that applications that we develop are only as good as we make them. Does this program have room for improvement? Certainly. Here are some things that we might want to consider:
- Using Actions for the buttons so we can "disable" them.
- We might want to either add some buttons, or dynamically change the buttons.
- Can we have the elapsed time be displayed as the timer is running, like a "real" StopWatch?
- Might we want to format the "elapsed time" differently?
Before we press "Start" it would really be best if we disabled "Stop", and while the timer is running (i.e., after pressing "Start") it would be best to disable the "Start" button. Don't you think?
Might we was a "Restart", or "Resume" button? How about a "Reset" button?
This might make the application more "user friendly."
This too might make the application more "user friendly."
The purpose of this blog entry was to show how one might "iterate", or progressively improve an application, and test it as we go along. This allows us to see what it actually looks like as we make changes. This can drive us to make changes in a different way, based upon what we have learned. That's one of the really good parts of using a scripting language like Jython to develop a Graphical User Interface (GUI) application. If we see something that we don't like, we can quickly, and easily, make some changes to either fix something that isn't working right, or change our design direction based upon a better understanding of what the program is actually doing, and exactly how it looks.
Did you find this post helpful? I hope so. Feel free to take a few moments and let me know what you think.
Thanks for taking the time to read. I hope that you liked it.