Page 1 of 2
In the second part of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.
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 is 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.
A JFrame is just a Java class and most Java classes are composed of nothing but Java code. This is true of JFrame but NetBeans "knows" that the JFrame is a part of Swing and it "knows" how to display it as a graphic. What is more NetBeans loads the MyFrame class into a text editor windows and a design window that you can use to add other Swing classes to it. That is NetBeans provides two ways for you to edit a JFrame - as source code or as a GUI in a designer. Notice that the view that you get of JFrame in the designer is all the work of NetBeans - JFrame really exists only as Java source code.
The designer is an easy way to generate and modify that Java source code.
If you look at the Palette to the right of the Designer window you will see a set of different Swing components that can be placed on the JFrame surface. Each one of these corresponds to a Java class that belongs to the Swing library.
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 objects 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.
Methods make objects do something.
Swing objects have one final aspect and we have already met it in the first chapter - 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.
In fact events are just a special case of object methods. An event is a method that is used when something happens. So for example a move method might move a UI component when you use it in your Java program. A click event handler is just a method that is used automatically when a button or some other component is clicked.
It may be that an event is just a special sort of method but it is helpful to treat them as something slightly more than a method - especially when it comes to understanding Swing and NetBeans.
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. If you find this confusing don't worry about it you will discover that its a natural way to think about things and the more you work with Java the more natural it will seem.