Window Pane Management

I have been creating buttons that have no text on them. They only have a graphic icon loaded from an image file. I noticed that the button size grows to accommodate larger pictures. This leads to different sized buttons and a messy user interface. There must be a way to keep a uniform button size, shrinking icon images as needed.

Another problem I have is when a control such as a button is invisible. The other controls rearrange during the paint() so as to obscure the position where the invisible control would be. I don't like this reshuffling of the screen when a control goes invisible. There should be a way to reserve space on the screen for a control that may become visible in the future. However this is not the default behavior.

I normally use appletViewer to test out my applets in development. But I found out that this tool only displays the embedded applet in an HTML page. It does not render any of the other HTML elements. Weird.

Button Action Command

Normally you have your applet listen for actions from your controls like the buttons in your applet. The event handler gets the action command. That command is normally the text on the button itself.

Recently I have been creating buttons that have no text. They just display an image that I loaded as the icon of the button. How does the applet know which control generated the event?

It turns out that you can call setActionCommand and pass it a String. This String is when the applet will get when it tries to get the command. It puts you back in control for the event processing.

Here is a tip I discovered the hard way. Make sure you addActionListener(this) on the button you want to handle events for. Otherwise the applet will never get a notification that your button was clicked. This might be obvious. But it is painful to debug when you forget to make the call.

Applets and Web Pages

So far I have been concentrating on applets. The applet is normally the entire web page. The HTML for the web page is merely to name the applet, and give it a size. However this is not normal. You usually have some other presentation in the HTML page. Therefore I did a more realistic project that integrated applet output with the markup from the HTML source.

I had an exercise from my textbook dealing with times. In Java, you can get the number of milliseconds since 1970. This feature has been encapsulated with the Date class. The default constructor of Date initializes the object to the current date and time. Use DateFormat to choose how you want to show the date. Even though the class is abstract, you can still call the static members.

What Time is it?

Here are some leftover tips from a textbook project I did recently. The format() function is a static one from the String class. It helps your output look good. Call it like this: String.format(...).

You can hide controls in an applet by using the setVisible function, passing in the parameter false. Here is something weird though. Hidden controls are removed from the user interface layout. There is no reserved space on the screen where the hidden control is supposed to be. The output looks as if the control was never there. Other controls will be placed over top of the region where the hidden control belongs.

Applets and Browsers

Sometimes I need to see how my applet looks in a browser. My browser of choice is Internet Explorer 6. Yes I know this is an ancient version. I tried installing version 8 but had some problems.

My IE6 blocks the applet from running by default on a web page. This is probably related to my security settings, and not my specific browser. I still need to click to "allow blocked content" before the browser will run my applet.

I am starting to see a pattern with applet GUI programming. Create a label, text field, and button. Add them all to the content pane. Then you make sure the applet can get the events from the button. This is all happening in my init() function. It is almost boilerplate code for me now.
Like most things, I find that writing the code for an applet is easiest if you start out with a working applet to modify. Now let's get back to some Java basics. Previously I had recalled how to convert a String to an int. Now I needed to do the same for a double. The tried and true design worked well.

You create a Double object. Pass the String into the constructor of the Double. Then you can assign the value of the Double to a double variable. The conversion works well. Actually I think this is implicitly calling a member of Double that returns a double. Is this what they call boxing? Or maybe it is unboxing.

Button Behavior

You size an applet in the HTML page that hosts it. I find that you need to size the applet area carefully to allows your controls to be layed out as expected on the screen.

The general pattern I use when user input drives the display is to have a member variable of my JApplet derived class. There is conditional logic in the paint() method that depends on this variable. When a user action changes the variable state, repaint() is called to redraw the screen.

I've been doing a lot with buttons that have no text on them. They only have an icon image on them. I have encountered some problems with transparency. I would like the button background color to bleed through any white portion of my image. However does not seem to be the default behavior of the button class. Maybe I need to set some unknown options. I hope I do not need to code or draw my own button face.

The JTextArea

I started using the JTextArea control in my applets. Seems all the good controls are in the "javax.swing" package. You pass in the height to the JTextArea constructor. However it seems that the thing will allow more than that many rows in the control. The control seems only bounded by the applet window size. What the heck?

You use newlines in the JTextArea text to make the thing jump to the next row in the control. One thing is strange though. The default behavior does not seem to provide anything other than the text in the control. There is no border or scrollbar or anything. Maybe there is some other control that is more like the Windows control I am used to.

Eclipse User Trends

Let's briefly look at the trends of Eclipse users. You could say they are comparable to the general Java development community. For starters they are using Subversion as the configuration management tool of choice. They also are using the Linux operating system for desktop development. These developers continually download and use the latest version of Eclipse.

CVS use is on the decline for configuration management. ANT is the build tools of choice. Maven is trying to catch up. Some are just using Eclipse itself to control builds. Web development is done using jQuery (I just got a book on jQuery, so I will be sharing what I learn). The majority of Eclipse users do not contribute to open source. That last statistic surprises me.

I hope I can get my hands on some survey data for Java developers as a whole.

Wish Upon a Star

I am moving on to writing more applets. These are simple programs to get my bearings. The most important methods in JApplet are init() and paint(). You add items or objects to the screen in the init() function. And you draw things such as labels and buttons in the paint() function.

If you handle some GUI events like button clicks, the pattern is to call repaint() to get the screen updated based on the user input. It is funny how quickly I forget my Java basics when I do not code for a long time. These rules have not been committed to heart.

For example, I had to rediscover how to convert a String to an int. You create an Integer object, passing the String into the constructor. Then you can assign the resulting Integer to a variable of type int. Also you need to qualify Math package functions with "Math." to get your Java to compile.

Some topics are universal. Break down complex routines into multiple subroutines (functions). Make use of constants instead of magic numbers. In Java this is done with public static final variables. I will say I am starting to get some bearings on how to do drawing on the Canvas. The upper left hand corner of the screen has coordinates (0,0). Increasing x goes to the right. Increasing y goes down.

My First Applet

Here I am in my full week of training. This is an independent study for me. My goal is to learn applet programing. Read a couple chapters in my school textbook. We always skipped the applet and other GUI sections. Unfortunately the applet chapter requires you to read the big Java Swing chapter. I have skipped it. So I am going to be having some troubles.

Applets do not have a main function. Instead they have an init. They close when the web page that contains them closes. The containing web page is written in HTML. The applet itself runs on the client machine. There are some security restrictions like an applet cannot run a program, read a file, or write a file. You should test your applets on many browsers including multiple versions of the same browser.

The older class for applets is Applet. Go figure. You cannot use Swing with this old style class. You also cannot do icons or menus. Use buttons instead. I found that I could load an HTML page which referenced an applet. However when I recoded and recompiled the applet, an Internet Explorer 6 refresh would not run the new version of the applet. That was annoying. Instead I decided to use appletviewer. Here is another strange thing. The reload function in appletviewer causes the window to disappear and the viewer to freeze. Another funny thing was that I tried to use Graphics2D in my paint method. It compiled but just output a light blue screen. All my graphics were gone. I guess I can make do with the normal Graphics class.

Applet Time

In my college Java class, we skipped over most of the information on applets. I wanted to learn more about them though. There was a whole chapter in the text that covered applets. I also have a lot of information on my latest book about applets for game programming in Java.

It is time for me to learn applets. I convinced my employer to let me spend a week in independent study on Java. I need some program to keep me busy to really learn practical applet programming. Initially I thought I would write a copy of an arcade classic such as Pacman. However I think I will need more than a week to get deep into applet programming.

My new plan is to actually try to duplicate the main program we have on our project at work. It is a client/server C++ Windows application that connects to an Oracle database. This might be a good sized program to implement as an applet. The thing is over 2o0k lines of code. This will keep me busy for quite some time. I figure I can work on it in the office, and not get too much attention on my sideline activities.

Access Modifiers

Just read a rant about a developer that likes to use parentheses to make the order of operations explicit. He does not want a reader of his code to need to know the operator precedence rules. As an extension, he also explicitly declares the access modifier for any java function that he writes.

This started a big discussion on what it means to not specify an access modifier for a Java function. In C++ it is easy. C++ class members without an access modifier are private. C++ structure members without an access modifier are public. However in Java, not specifying an access modifier means the function is package private.

Package private has no keyword. It is the access allowed when you do not specify a modifier. This causes the function to only be accessible by other members in the package. This is different than public, private, or protected. I am sure I learned this some time ago in my Java college class. However today it was solidified.