Showing posts with label components. Show all posts
Showing posts with label components. Show all posts

Eating the Keys

I am learning about the finer points of GUI development with Java this week. It turns out there are some GUI basics in Java that I don't know about. So I am studying them first. One topic of interest is catching events. To practive, I started coding up an event viewer. It is supposed to get and print out events that fire based on a bunch of different GUI components.

For the most part I can knock out the even viewer code. I am setting up listeners that log events to the screen. Then I try to do a KeyListener. But my event handlers are not getting called. What the heck? Initially my text area control was displaying the keys. Okay. I disabled it. Still no keyboard events.

I am a good debugger. I traced the issue down to the presence of a JButton on my GUI. If I remove the JButton, the keyboard events fire and I get the calls. What could be going on here? Is the JButton receiving and eating up the keyboard events? It looks like I am going to have to consult an expert here.

Event Based

Java has always used event based programming to deal with user interfaces. Initially would generate events and send them to components. The component would information the system whether it processed the event. If so, that was the end of the processing. Otherwise the system would forward the event to another component, and so on until one did process the event.

By the time Java 1.1 was released, event processing had changed. A new delegation model was adopted which implements the observer pattern. Any components interested in an event implement a listener interface. They register with the system to handle the event. This makes the system's job easier. When the event fires, all listeners registered for that event get notified. More than one component can receive and process the event.

If your component's event processing is going to take any real time, you should do the work in a separate worker thread to keep the system responsive for the user.

Abstract Window Toolkit

I have read an intro chapter on the Abstract Window Toolkit (AWT). It is implemented via the "java.awt" package. The package provides a number of graphic user interface components. Examples are buttons, scrollbars, and checkboxes.

Two of the most useful components are the TextField and TextArea. The TextField is a single line of input from the user, while the TextArea is a multiple line edit control.

Checkboxes are components. They can be grouped using a CheckboxGroup class, which itself is not a component.

A LayoutManager interface has been defined to determine how components are placed on a screen. There are 5 different implementations of the LayoutManager provided to you by the AWT.

Let's talk about a few more AWT concepts. A container is an area on the screen which holds other components. A windows is a top level one on the screen that has no menu bar or border. It is used to create popup messages. If the user provides input in a such a window, it is then called a dialog. A frame, on the other hand, is a window that has a title, border, and buttons like maximize/minimize.

The last idea which I shall expound upon later is that of events. The entire AWT system is driven by events. AWT manages these events, and sends them to interested components. For example, an event is generated when the user clicks the mouse button. The AWT catches this, and sends the information to component(s) that had previously requested mouse press events.

Origins of Java

A Java game programming book uses the Abstract Window Toolkit. So I thought I would read up on it a bit. There are some free materials online that cover this older technology. I figure what I learn can also help in my learning of newer GUI frameworks.

The book I read online claims that the AWT is crucial for Java programmers to learn. Initially there were major changes in AWT before it stabilized. Here is a funny thing. Part of the AWT intro explained the origins of the word Java.

Java is actually an island in Indonesia. It is not just any island. A whopping 124 million people live on the island. That is more than half the population of Indonesia. The capital of Java is Jakarta. This explains the naming for the Apache Jakarta project.

Next time I will get into some of the components that the AWT has to offer.