Page 1 of 2
Modern Java Contents
- Getting started with Java
- Introducing Java - Swing Objects
- Writing Java Code - Methods
- Command Line Programs
- The Java User Interface - More Swing
- Working With Class
- Class Inheritance
- Building a Java GUI - Containers
In Getting started with Java we covered the basic mechanics of creating a Java program including installing NetBeans is and creating and running a simple Hello World program. Now we are ready to move on to more interesting programs.
The first thing to restate is that that we are using an approach to learning to program which is based on creating a UI (User Interface) using the Swing framework and the drag-and-drop editor provided by NetBeans. This allows us to build quite complicated programs while writing minimal Java code. More traditional introductions to Java start from the other end of the problem and show you how to write code and work up to using the UI editor.
The advantage of starting with the drag-and-drop editor is that it introduces the key concept of "object" and hence object-oriented programmng (OOP) in a perfectly natural manner and it makes it both simple and obvious.
Objects via Swing
So let's find out a little more about objects by exploring the Swing framework.
Start a new Netbeans Java Application project and uncheck the Create Main Class selection box. Call the project Swing1. When the project opens right click on the Source Packages folder in the Projects window and select New, JFrame Form.
Call the new JFrame MyJFrame.
The JFrame is a very commonly used basic display window that you can place other Swing components on to build a UI. Most Swing based projects start with a JFrame as a JFrame is a "top level window complete with title and boarder".
From our point of view however one of the most important things about JFrame is that it is a container. That is it can contain and hence display other components like buttons. Swing provides three top-level containers - JFrame which we have already met, JDialog a dialog window and JApplet a window that can appear within a browser. More of both JDialog and JApplet later.
The JFrame slightly more complicated than is apparent when you first meet it. In particular when you add a component such as a button to a JFrame the button isn't actually added to the JFrame but to a Panel container. That is every JFrame has a Panel that is the main display area of the window, i.e. everything below the title bar. The reason for this extra complication is that the JFrame also contains another container which can be used to add a menu to the window - more of this later.
When you drag and drop a component onto a JFrame it is added to its internal Panel container. You can move the components around and change there sizes using the mouse and so design a user interface. Let's take a moment an look at one of the components in more detail.
If you drag-and-drop a Progress Bar component onto the JFrame you will see that it doesn't look very exciting. As you might expect the Progress Bar component is intended to be used to show a percentage complete bar as you application does something time consuming. Its default state when you place it on the JFrame is to show 0% completed which is why it is just a white bar.
If you look at the Properties window while the bar is selected and scroll down you will find a property called value. If you now enter 50 in value then you will see the bar change to a 50% display.
This sort of behaviour is very general. All components, in fact just about everything you will meet in Java, is an object. Think of a button or a progress bar as objects. Objects have properties that customise their look and their behaviour. When you are designing a user interface the properties of the currently selected component are displayed in the Property window and you can change them by typing or selecting between alternatives.
So object have properties but they also have "methods". A method is some Java code that does something. For example, a Clear method might reset any custom graphics and a Move method say to change the position of a component. Of course you don't use the methods that objects provide while you are designing the UI only within the code that you write that it active when you run the program.
Objects have one final aspect and we have already met it - events. An object can respond to an event like a button click and you can write special methods - event handlers that are run when the event occurs. In this way objects can react to things that happen.
Thus to sum up:
- An object is something that has properties, methods and events.
Another way to look at this, and it's a more sophisticated way that pays off in the long run, is that an object just has properties - and some of these are values, some are methods and some are events.
Finally we have to make the distinction between the definition of an object - its class - and an instance of the object. For example, the button icon that you see in the tool Palette isn't actually a button. However you can use it to create as many instances of a button as you care to create by dragging and dropping onto the JFrame. Each instance of the button is independent and can be customised by setting values on its properties. Each instance also has a name that distinguishes it from every other instance. When you drag-and-drop a component the name is automatically generated as are many of its properties but you can change the name to something else if it helps - as long as the name is unique.
At the moment the idea that there are classes that define objects and instances that are the objects may seem like a subtle and almost unnecessary one but if you can keep it in mind it will make the transisition to creating your own classes and object much simpler.
So the basic idea is that you use the Tool Palette to place components on the JFrame. You then write Java code to make things happen - respond to events and change the objects properties and use object methods.
Let's see how this works in a simple example.