Advanced Java Programming (22517) Notes PDF
Document Details
Uploaded by InexpensiveOpal723
Tags
Summary
These notes provide an overview of Advanced Java Programming, focusing on fundamental aspects of AWT (Abstract Window Toolkit), GUI (Graphical User Interface) development and related concepts in Java programming. The notes cover various components, layouts, and methods for creating and managing graphical user interfaces. They are suitable for undergraduate students.
Full Transcript
Advanced Java Programming (22517) Chapter No Chapter Name Marks (As per Syllabus) 01 Introduction to 12 Abstract Windowing Toolkit(AWT) 02 Swing 10 03 Event Handlin...
Advanced Java Programming (22517) Chapter No Chapter Name Marks (As per Syllabus) 01 Introduction to 12 Abstract Windowing Toolkit(AWT) 02 Swing 10 03 Event Handling 12 04 Networking 10 05 Interacting with 12 Database 06 Servlet 14 70 Unit 1 Introduction to Abstract Windowing Toolkit (AWT) (12 Marks) Unit Outcomes Develop GUI using awt Create frame window using different awt component Arrange GUI components using Layout Manager Develop program using menu and Dialog Boxes AWT (Abstract Window Toolkit) It is a platform dependent API for creating Graphical User Interface (GUI) for java AWT contains numerous classes and methods that allow you to create and manage window import java.awt.*; Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components uses the resources of system. Abstract Window Toolkit GUI using Awt Applet Fram AWT Class Hierarchy Simple Applet program import java.awt.*; import java.applet.*; public class Simpleapplet extends Applet { public void init() { setBackground(Color.YELLOW); } public void paint(Graphics g) { g.setColor(Color.RED); g.drawString("Hello",100,150); } } Working with Frame Window Frame’s constructors: Frame( ) Frame(String title) Setting the Frame’s Size void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) Dimension getSize( ) Hiding and Showing a Window void setVisible(boolean visibleFlag) Setting a Window’s Title void setTitle(String newTitle) Closing a Frame Window setVisible(false) To perform window-close event we must implement windowsClosing() method of WindowListener //Creating Frame window by extending Frame class import java.awt.*; class Simpleframe extends Frame { Simpleframe() { setSize(300,300); setVisible(true); setTitle("This is Frame window"); setBackground(Color.BLUE); } public static void main(String args[]) { Simpleframe f=new Simpleframe(); } } Difference between java application(Frame) and Applet AWT Class Hierarchy Component Component is an object having a graphical representation that can be displayed on the screen and that can interact with the user for eg button,label At the top of the AWT hierarchy is the Component class. Which object is responsible for remembering the current foreground and background colors and the currently selected text font? (Answer: Component) It extends: Object Class Implements: ImageObserver, MenuContainer, Serializable Container Container class is a subclass of Component. Provided additional methods that allow other Component to place on it. Container is responsible for laying out (that is, positioning) any components that it contains. It does this through the use of various layout managers. Panel Panel is a concrete subclass of Container Panel is the immediate superclass of Applet. When screen output is directed to an applet, it is drawn on the surface of a Panel object. Window Creates top-level window means directly on the desktop. The window is the container that have no borders and menu bars. Uses Frame, Dialog class which is subclass of Window class for creating window. Frame It is subclass of Window and has a title bar, menu bar, borders, and resizing corners. AWT Controls AWT Controls: Component which allows you to interact with application. Labels Button Checkbox Checkbox group Scrollbars Text field Text Area AWT Control: Label Used to just display string on window. Passive Components Constructors: Label( ) Label(String str) //left - justified Label(String str, int how) // how value can be Label.LEFT, Label.RIGHT, Label.CENTER Methods to perform operation: Setter and Getter Method. About text: void setText(String str) String getText( ) About Alighment void setAlignment(int how) int getAlignment( ) Label(using frame window) import java.awt.*; class Labelframe extends Frame { Labelframe() { Label l1=new Label("Name",Label.LEFT); l1.setBounds(30,30,80,30); add(l1); setSize(300,300); //frame size 300 width and 300 height setLayout(null); setVisible(true); //now frame will be visible, by default not visible } public static void main(String args[]) { Labelframe f=new Labelframe(); } } Run this program in command prompt- javac Labelframe.java java Labelframe Label(using Applet) import java.awt.*; import java.applet.*; public class Labelapplet extends Applet { public void init( ) { setFont(new Font ( "Times new roman" , Font.BOLD, 14)); Label l1 = new Label("aligned left", Label.LEFT); add(l1); Label l2 = new Label("aligned center", Label.CENTER); add(l2); Label l3 = new Label("aligned right",Label.RIGHT); add(l3); //using setText and getText Label l4=new Label(); String str=l3.getText(); l4.setText("using setText = "+str); add(l4); } } AWT Control: Button It contains a label and that generates an event when it is pressed. Active Components Constructors: Button( ) Button(String str) Methods to perform operation: Setter and Getter Method. void setLabel(String str) String getLabel( ) import java.awt.*; import java.applet.*; public class Buttonapplet extends Applet { Button b1,b2; public void init() { b1 = new Button("Yes"); b2 = new Button("No"); add(b1); add(b2); //getLabel and setLabel Button b3=new Button(); String str=b2.getLabel(); b3.setLabel(str); add(b3); } } AWT Control: CheckBox Used to turn an option on or off. Small box: check mark or not. Each check box has label. Constructors are: Checkbox( ) Checkbox(String str) Checkbox(String str, Boolean on) Checkbox(String str, Boolean on, CheckboxGroupname) Checkbox(String str, CheckboxGroupname, Boolean on) Setter and Getter methods: Boolean getState( ) void setState(Boolean on) String getLabel( ) void setLabel(String str) Program // Demonstrate check boxes. import java.awt.*; import java.applet.*; public class Checkboxapp extends Applet { Checkbox c1,c2; public void init() { c1 = new Checkbox("Cricket", true); c2= new Checkbox("Football"); add(c1); add(c2); } } CheckboxGroup(Radio Button) It is possible to create a set of mutually exclusive check boxes in which one and only one check box in the group can be checked at any one time. These check boxes are often called radio buttons, Methods getSelectedCheckbox(), setSelectedCheckbox(). import java.awt.*; import java.applet.*; public class CBGroupapp extends Applet { Checkbox c1,c2 ; CheckboxGroup cbg; public void init() { cbg = new CheckboxGroup(); c1 = new Checkbox("Male", cbg, true); c2= new Checkbox("Female", cbg, false); add(c1); add(c2); } } AWT Control: Choice Used to create a pop-up list items. Default constructor Choice() create empty list. For add item in list and select active item: void add(String name) void select(int index) void select(String name) Each item in the list is a string that appears as a left-justified label in the order it is added to the Choice object. To determine selected item: String getSelectedItem( ) int getSelectedIndex( ) String getItem(int index) program import java.awt.*; import java.applet.*; public class Choiceapp extends Applet { Choice sub; public void init() { sub= new Choice(); // add items to os list sub.add("c"); sub.add("c++"); sub.add("java"); add(sub); } } AWT Control: List List class provides a compact, multiple-choice, scrolling selection list. List object can be constructed to show any number of choices in the visible window. In Choice only one item is shown. Constructors List( ) List(intnumRows) List(intnumRows, booleanmultipleSelect) for eg. List(4,true) Following methods are used to add items: void add(String name) void add(String name, int index) For single selection items: String getSelectedItem( ) Int getSelectedIndex( ) For Multi selection items: String[ ] getSelectedItems( ) int[ ] getSelectedIndexes( ) To retrieve item: String getItem(int index) To get Item Count Int getItemCount( ) Active Item void select(int index) Program- import java.awt.*; import java.applet.*; public class Listapp extends Applet { List sub; True means multiple selection is enabled public void init() { sub = new List(4, true); // add items to os list sub.add("c"); sub.add("c++"); sub.add("DSU"); sub.add("JAVA"); sub.add("CSS"); sub.select(1); // add lists to window add(sub); } } Choice List Choice Class is used List Class is used AWT Control: TextField TextField is subclass of TextComponent. TextComponent is subclass of Component. TextField class implements a single-line text-entry area, usually called an edit control. Constructors: TextField( ) TextField(int numChars) -> TextField(10) TextField(String str) -> TextField(“enter name”) TextField(String str, int numChars) -> TextField(“enter name”, 10) Setter and Getter Method of TextField and TextComponent: String getText( ) void setText(String str) Particular Text selection: String getSelectedText( ) void select(intstartIndex, intendIndex) About Modification of Text: boolean isEditable( ) void setEditable(Boolean canEdit) Setting echo character to text field and related methods: void setEchoChar(char ch) Boolean echoCharIsSet( ) char getEchoChar( ) Program- // Demonstrate text field. import java.awt.*; import java.applet.*; public class TextFieldapp extends Applet { TextField t1, t2; Label l1,l2; public void init() { l1 = new Label("Name: "); l2 = new Label("Password: "); t1 = new TextField(12); t2= new TextField(8); t2.setEchoChar('*'); add(l1); add(t1); add(l2); add(t2); } } AWT Control: TextArea Sometimes a single line of text input is not enough for a given task. Subclass of TextComponent. Constructors: TextArea( ) TextArea(intnumLines, intnumChars) -> TextArea(10, 5) TextArea(String str) -> TextArea(“welcome”) TextArea(String str, intnumLines, intnumChars) -> TextArea(“welcome”,10, 5) TextArea(String str, intnumLines, intnumChars, intsBars) -> TextArea(“welcome”,10, 5, SCROLLBARS_BOTH) The values of sbar: SCROLLBARS_BOTH SCROLLBARS_NONE SCROLLBARS_HORIZONTAL_ONLY SCROLLBARS_VERTICAL_ONLY It supports: getText( ), setText( ), getSelectedText( ), select( ), isEditable( ), and setEditable( ) Other some methods: void append() void insert() void replaceRange() Program:- // Demonstrate TextArea. import java.awt.*; import java.applet.*; public class TextAreaapp extends Applet { public void init() { String val = "There are two ways of constructing " + "a software design.\n" + "One way is to make it so simple\n" ; TextArea text = new TextArea(val, 10, 30); No. of characters add(text); } } No of Lines Scroll Bars The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns. Scrollbar defines the following constructors: Scrollbar( ) Scrollbar(int style) Scrollbar(int style, intinitialValue, intthumbSize, int min, int max) for eg. Scrollbar(Scrollbar.HORIZONTAL,0, 60, 0, 300); program- // Demonstrate scroll bars. import java.awt.*; import java.applet.*; public class Sbar_app extends Applet { Scrollbar v, h; public void init() { int width = 300; int height = 200; v = new Scrollbar(Scrollbar.VERTICAL,0, 100, 0, height); h= new Scrollbar(Scrollbar.HORIZONTAL,10, 1, 0, width); add(v); add(h); } }