






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Advanced Java is everything that goes beyond Core Java – most importantly the APIs defined in Java Enterprise Edition, includes Servlet programming, Web Services, the Persistence API, etc. It is a Web & Enterprise application development platform which basically follows client & server architecture
Typology: Lecture notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Chapter 01 Java AWT Applet: All applets are subclasses of Applet. Thus, all applets must import java.applet. Applets are not executed by the console-based Java run-time interpreter. Rather, they are executed by either a Web browser Execution of an applet does not begin at main( ). Actually, few applets even have main( ) methods. Instead, execution of an applet is started and controlled with an entirely different mechanism or an applet viewer. All but the most trivial applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. Four of these methods init( ), start( ), stop( ), and destroy( ) are defined by Applet. Another, paint( ), is defined by the AWT Component class. Default implementations for all of these methods are provided. Applets do not need to override those methods they do not use. However, only very simple applets will not need to define all of them. These five methods can be assembled into the skeleton shown here: // An Applet skeleton. import java.awt.; import java.applet.; /* / public class AppletSkel extends Applet { // Called first. public void init() { // initialization } / Called second, after init(). Also called whenever the applet is restarted. / public void start() { // start or resume execution } // Called when the applet is stopped. public void stop() { // suspends execution } / Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities } // Called when an applet's window must be restored. public void paint(Graphics g) { // redisplay contents of window }} Although this skeleton does not do anything, it can be compiled and run. When run, it generates the following window when viewed with an applet viewer:
Applet Initialization and Termination It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins, the AWT calls the following methods, in this sequence:
These controls are subclasses of Component. Adding and Removing Controls To include a control in a window, you must add it to the window. To do this, you must first create an instance of the desired control and then add it to a window by calling add( ), which is defined by Container. The add( )method has several forms. The following form is the one that is used for the first part of this chapter: Component add(Component compObj) Here, compObj is an instance of the control that you want to add. A reference to compObj is returned. Once a control has been added, it will automatically be visible whenever its parent window is displayed. Sometimes you will want to remove a control from a window when the control is no longer needed. To do this, call remove( ). This method is also defined by Container. It has this general form: void remove(Component obj) Here, obj is a reference to the control you want to remove. You can remove all controls by calling removeAll( ). Responding to Controls Except for labels, which are passive controls, all controls generate events when they are accessed by the user. For example, when the user clicks on a push button, an event is sent that identifies the push button. In general, your program simply implements the appropriate interface and then registers an event listener for each control that you need to monitor. Labels The easiest control to use is a label. A label is an object of type Label, and it contains a string, which it displays. Labels are passive controls that do not support any interaction with the user. Label defines the following constructors: Label( ) Label(String str) Label(String str, int how) The first version creates a blank label. The second version creates a label that contains the string specified by str. This string is left-justified. The third version creates a label that contains the string specified by str using the alignment specified by how. The value of how must be one of these three constants: Label.LEFT, Label.RIGHT,or Label.CENTER. You can set or change the text in a label by using the setText( ) method. You can obtain the current label by calling getText( ). These methods are shown here: void setText(String str) String getText( ) You can set the alignment of the string within the label by calling setAlignment( ). To obtain the current alignment, call getAlignment( ). The methods are as follows: void setAlignment(int how) int getAlignment( ) The following example creates three labels and adds them to an applet. import java.awt.; import java.applet.; /* */
public class LabelDemo extends Applet { public void init() { Label one = new Label("One"); Label two = new Label("Two"); Label three = new Label("Three"); // add labels to applet window add(one); add(two); add(three); }} The above window created by the LabelDemo applet. Notice that the labels are organized in the window by the default layout manager. Buttons The most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button. Button defines these two constructors: Button( ) Button(String str) The first version creates an empty button. The second creates a button that contains str as a label. After a button has been created, you can set its label by calling setLabel( ). You can retrieve its label by calling getLabel( ). These methods are as follows: void setLabel(String str) String getLabel( ) Here, str becomes the new label for the button. Handling Buttons Each time a button is pressed, an action event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ActionListener interface. That interface defines the actionPerformed( )method, which is called when an event occurs. An ActionEvent object is supplied as the argument to this method. Steps: Create the Button object Register the listener to receive the event generated by button Add the control to the applet window Implement the method actionPerformed() Here is an example that creates three buttons labeled “Yes,” “No,” and “Undecided.” Each time one is pressed, a message is displayed that reports which button has been pressed. In this version, the label of the button is used to determine which button has been pressed. The label is obtained by calling the
Check Boxes A check box is a control that is used to turn an option on or off. It consists of a small box that can either contain a check mark or not. There is a label associated with each check box that describes what option the box represents. You change the state of a check box by clicking on it. Check boxes can be used individually or as part of a group. Check boxes are objects of the Checkbox class. Checkbox supports these constructors: Checkbox( ) Checkbox(String str) Checkbox(String str, boolean on) Checkbox(String str, boolean on, CheckboxGroup cbGroup) Checkbox(String str, CheckboxGroup cbGroup, boolean on) The first form creates a check box whose label is initially blank. The state of the check box is unchecked. The second form creates a check box whose label is specified by str. The state of the check box is unchecked. The third form allows you to set the initial state of the check box. If on is true, the check box is initially checked; otherwise: it is cleared. The fourth and fifth forms create a check box whose label is specified by str and whose group is specified by cbGroup. If this check box is not part of a group, then cbGroup must be null. To retrieve the current state of a check box, call getState( ). To set its state, call setState( ). You can obtain the current label associated with a check box by calling getLabel( ). To set the label, call setLabel( ). These methods are as follows: ItemSelectable getItemSelectable() it will return ItemSlectable object boolean getState( ) it will return true or false void setState(boolean on) to ON use true, to off false String getLabel( ) void setLabel(String str) Here, if on is true, the box is checked. If it is false, the box is cleared. Handling Check Boxes Each time a check box is selected or deselected, an item event is generated. This is sent to any listeners that previously registered Each listener implements the ItemListener interface. That interface defines the itemStateChanged( )method. An ItemEvent object is supplied as the argument to this method. It contains information about the event (for example, whether it was a selection or deselection). Steps: Create object of Checkbox Register listener to receive event generated by checkbox
Add to applet window Implement the method itemStateChanged() The following program creates four check boxes. The initial state of the first box is checked. The status of each check box is displayed. Each time you change the state of a check box, the status display is updated. import java.awt.; import java.awt.event.; import java.applet.; / */ public class CheckboxDemo1 extends Applet implements ItemListener { Label l1; Checkbox win98, winNT, solaris, mac; public void init() { Win98 = new Checkbox("Windows 98/XP", null, true); win98.addItemListener(this); add(win98); winNT = new Checkbox("Windows NT/2000"); winNT.addItemListener(this); add(winNT); solaris = new Checkbox("Solaris"); solaris.addItemListener(this); add(solaris); mac = new Checkbox("MacOS"); mac.addItemListener(this); add(mac); l1=new Label(" "); add(l1); } public void itemStateChanged(ItemEvent ie) { Checkbox cb=(Checkbox)ie.getItemSelectable(); l1.setText(cb.getLabel()+" "+cb.getState()); }} TextField
import java.awt.; import java.awt.event.; import java.applet.; / */ public class TextFieldDemo extends Applet implements ActionListener { TextField name, pass; public void init() { Label namep = new Label("Name: ", Label.RIGHT); Label passp = new Label("Password: ", Label.RIGHT); name = new TextField(12); pass = new TextField(8); pass.setEchoChar('?'); add(namep); add(name); add(passp); add(pass); // register to receive action events name.addActionListener(this); pass.addActionListener(this); } // User pressed Enter. public void actionPerformed(ActionEvent ae) { repaint(); } public void paint(Graphics g) { g.drawString("Name: " + name.getText(), 6, 60); g.drawString("Selected text in name: "