Scott McLaughlin (color@mit.edu) writes:
Could you explain to me what is meant by the term API, application programming interface? I've programmed a little in C++ and am trying to teach myself Java. I see API used all the time. What is it and how does it fit into the programming picture?
Traditionally, programming languages are just that, languages. They have a syntax and grammar that must be followed. Java is no different. However, the creators of Java added value to the base language itself. This value is in the form of extra features.
In order to utilize these features, you must create Java code that speaks to them properly. This communication pathway is called an Application Programming Interface, or API. The API is the interface with which your application communicates with the object in question.
In Java, the classes that make up the core packages (i.e., java.io, java.net, java.lang, etc.) expose their API, or interface, to you, the programmer. You can utilize these objects by interfacing with them, using their exposed methods and member variables.
Other languages, C++ for example, have no API to speak of. Sure there are APIs around that you can download or buy, but none are shipped with the standard language.
Jack Kapp (kapp@womex.com) writes:
Is it possible to change the background color of a button derived from java.awt.Button? Further, the code will be placed in the button's click event.
Sorry Jack, but unless you create your own button component, there is really no way to change the background color. This is because of Java's platform independence. When your Java program creates a button, it is really asking the host operating system to create a button for it. The Java runtime doesn't know how to create buttons, nor does it care. It really is only a manager of components.
Creating your own button, however, is quite simple. All you need to do is derive a class from Java's Canvas class. Set your color, then draw your text. Voilá! A button. Be sure to capture your mouse clicks and convert them into ACTION_EVENT events. You can get fancy with 3D frames and emulate an actual button. Beware, however, that while your button will appear similar across all operating systems, it may not be similar looking to the host operating system buttons.
One side note is of interest here. In researching your question, I found that in Windows 95 there is no easy way to change the background color of a single button. Usually one can respond to the Windows message called WM_CTL_COLOR and pass back a color but with buttons this doesn't work. You must create a custom control to change the background color.
MBGross@aol.com writes:
I'm having trouble creating a menu. Actually, the menu appears (in a Frame) but doesn't do anything. How can I correct this communication problem between the frame and the applet?
The problem is that you do not have an event handler in your Frame object to receive your menu clicks. To remedy this situation, simply subclass the Frame class. This new class needs to pass menu clicks to the applet so they can be handled there.
Listing 1 is one such subclassed Frame that can be used in your applets. It passes all action events on to the parent applet. It is called, aptly, AppletFrame.
Listing 2 is a class that exemplifies the use of AppletFrame. FrameMenu creates an applet with an AppletFrame that has a menu.
About the Author
Jerry Ablan and his brother Dan (dma@mcs.net) operate NetGeeks (http://www.netgeeks.com), an Internet consulting firm in Chicago, Illinois. Jerry is the author of "Developing Intranet Applications with Java" and the co-author of the "Web Site Administrator's Survival Guide," bothfrom Sams.net. Jerry can be reached at munster@mcs.net
Listing 1: AppletFrame.java
import java.awt.Frame;
import java.applet.Applet;
import java.awt.MenuItem;
import java.awt.Event;
public class
AppletFrame
extends Frame
{
Applet myApplet;
public
AppletFrame( Applet myApplet )
{
super();
setApplet( myApplet );
}
public
AppletFrame( Applet myApplet, String title )
{
super( title );
setApplet( myApplet );
}
public void
setApplet( Applet myApplet )
{
this.myApplet = myApplet;
}
public Applet
getApplet( Applet myApplet )
{
return( myApplet );
}
public boolean
action( Event e, Object arg )
{
if ( myApplet != null )
return( myApplet.handleEvent( e ) );
return( false );
}
}
Listing 2: FrameMenu.java
import java.applet.*;
import java.awt.*;
public class
FrameMenu
extends Applet
{
AppletFrame myFrame = new AppletFrame( this );
Menu fileMenu, helpMenu;
public
FrameMenu()
{
}
public void
init()
{
resize(320, 240);
MenuBar mb = new MenuBar();
fileMenu = new Menu( "File", true );
fileMenu.add( new MenuItem( "Open" ) );
fileMenu.addSeparator();
fileMenu.add( new MenuItem( "Exit" ) );
mb.add( fileMenu );
helpMenu = new Menu( "Help" );
helpMenu.add( new MenuItem( "About..." ) );
helpMenu.getItem( 0 ).disable();
mb.add( helpMenu );
mb.setHelpMenu( helpMenu );
myFrame.setMenuBar( mb );
myFrame.show();
}
public boolean
handleEvent( Event e )
{
System.out.println( e.toString() );
return( super.handleEvent( e ) );
}
}