Applications

Jump to application stuff

what's next - the following pieces left out of earlier material,
then applications and menus

Some things you might find useful:

Boolean Values have a class: Boolean
API

these are Boolean values, but they are objects. (public final static)

if B==Boolean.TRUE what? (hint, both are objects)

if (B.equals(Boolean.TRUE))

other Boolean methods:

Look at the String and StringBuffer classes

Why Two String Classes? and Creating Strings and StringBuffers , and in JDK 5 we now have StringBuilder.

Strings are much more efficient for fixed length (normal use)

One can set aside an empty StringBuffer of a specified length. One can use a String as the starting point for a StringBuffer

To change the contents of a StringBuffer, you have the opportunity to use a bunch of StringBuffer append and insert methods

Concatenation of Strings is really using StringBuffer

e.g. is interpreted as Eg.,

Applications

Remember the HelloWorldApp.java

You might have noticed the main().

In the above, the main() method expects an array of Strings to be passed to it. These strings are the command-line arguments (as in C). You have access to any arguments that follow the application call.

E.g.

(indexes in arrays start at 0)

java myprog arg1 arg2 arg3
(note: no extension of .class or .java)

note: in applets, you use the HTML attributes to pass arguments ... remember

<param name=image value="duke">
then in code use: getParameter("image")

main

A program needs to be started. In Java, the starting "class" needs a main() method.

When you run your Java application (javac to compile, java to run) the first thing that gets called is main().

The signature for main() must be:

public static void main(String arg[]) { }

The body of the main() method contains any code you need to get your application started - creating instances, etc.

When Java executes the main() method, keep in mind that main() is a class method - the class that holds it is not automatically instantiated when your program runs. If you want to treat that class as an object, you have to instantiate it in the main() method yourself.

also
An important thing to note about the arguments you pass to a java program is that those arguments will be stored in an array of strings. This means that any arguments you pass to your program will be converted to strings so they can be stored in the argument array. To treat them as non-strings, you'll need to convert them to whatever type you want them to be.

There must be exactly one main() in an application. Where is is? Could be in any class you put it.

A suggestion: (to be ontologically honest in OO)

Main() should really only be something to get the program going. For instance, maybe it opens a window or frame to instantiate a user-interface. In OO, one should always think of where methods should really reside...who is in charge of what. Since starting a system going is not really any certain classes responsibility, perhaps (for OO purposes and for your own book-keeping (where IS that stupid main() ) you might consider making a class called Main with one method main()

Then whenever you want to start a program, you call: java Main

Frames

See
java.awt.Frame or JFrame

In Java, a Frame is the base window used to create GUIs. In a typical browser, the Frame class is a parent class for the displayed GUI. (Frame is the parent of AppletViewer)

Frame extends Window (a blank window with no borders and no menubar)

Window is typically for pop-up kind of idea

Frame gives window a border, a layout, and a title.

The AppletViewer extends Frame to include a menubar and a drawing panel.

An example of the use of Frames: Memo Application

Java is a full object-oriented language (not just for applets) We will now look at how to do the same things we did with graphics for applets on Frames for applications.

A small application can be seen in the Memo Frame. The application will pop up a frame and display a message on the frame (the message can come from the command line, or it can be a default message (see Constructor)). If the user clicks in the window, the application will exit.

This class has two constructors Memo() and Memo(String s) - depending on if a variable is passed

Memo.java
New stuff: notice
show() and hide()deprecated now setVisible(false)

pack()

You will also find pack() useful when you have multiple components:

pack(). Method in class java.awt.Window 
    Causes subcomponents of this window to be laid out 
    at their preferred size.

setVisible(boolean) - was hide() and show() makes the frame (and any attached components) visible

dispose() - frees up system resources occupied by frame only use when completely done with frame

Menus

Add items

Menu Events

When you select a menu item, the event generated is an ActionEvent. You need an ActionListener interface. Note the use of inner classes to implement the interface:

You can also add Checkbox menu items, division lines and submenus. Now, place it in the application's menu bar: Set menu bar for the frame All together now (1.1) and as seen in real life ( Here is another example.)

***Note PopupMenu and MenuShotcut in ScribbleFrame (Nutshell2) http://www.ecst.csuchico.edu/~amk/foo/javanut2/index.html (example 8-1)

To create a popup menu, create a PopUpMenu, and add MenuItem objects to it. Do as you would with a regular menu pane. However, instead of adding a popupmenu to the menubar, you add it to the component over which it pops up. This is done with the add() method of the target component.

See also MenuShortcut

is a shortcut invoked as a Ctrl-C (platform independent ...cool). VK_ are Virtual Key constants defined in the KeyEvent class

Design issue: Should we have a MenuedFrame class?

Are frames with menus common? The above is simply an example, the MenuedFrame which you create as a subclass for multiple uses might have null or comments for where actions should be...or might have default buttons that are common that a user can change upon specialization. Point of subclassing - what is particular to this specific type of object. Will it be reused?

Domain objects should have nothing to do with the interface, but an interface must connect to the domain or there is no way to tell anything to do anything!

With Java's 1.1 Event Handling, the above is even easier... just don't implement the Interface until there is an application.

A specific application's interface is usually not inherited, however, many of the items we might use in an interface might not be objects that are supplied by java. The developer may want to create subclasses for future use...objects that they can pull off the shelf and use again, with minimum changes.

Similarly, subclassing buttons which are passed the panels on which they belong, or default actions to use could be interpreted as providing a general type of capability (say buttons that communicate with textwindows).

This has absolutely nothing to do with an application. When a user sees this class, they may find it as useful and specialize it for their specific need.

The above (menu and submenus) for frames are a reasonable thing to subclass for future use.

Here is another example of an application (a decimal converter): download and run

Notice the main() - instantiates and starts