SD Module-1 Advanced JAVA Assignment No. 4 Title :- Transform the above system from command line system to GUI based application Problem Definition: Write a Java program with the help of GUI based Application Perform Various Operation on Student database like Add Student, Search Student, Deleted Student and Failed Student List etc. 1.1 Prerequisite: Basic concepts of AWT and Event in Java. Concepts of Swing in java. 1.2 Software Requirements: Eclipse SDK 1.3 Tools/Framework/Language Used: Concepts of Swing and AWT. 1.4 Hardware Requirement: PIV, 2GB RAM, 500 GB HDD, Lenovo A13-4089 Model 1.5 Learning Objectives: Understand the implementation of the GUI based Application using Swing, AWT Concept. 1.6 Outcomes: After completion of this assignment student are able to implement the concept of Swing and AWT in GUI based Application in easy way. 1.7 Theory Concepts: Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java. 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 are using the resources of OS. The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc. Java AWT Hierarchy The hierarchy of Java AWT classes are given below. Container Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK 4. 1
The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. Window The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. Panel The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. Frame The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc. Useful Methods of Component class Method Description public void add(component c) inserts a component on this component. public void setsize(int width,int height) sets the size (width and height) of the component. public void setlayout(layoutmanager m) defines the layout manager for the component. public void setvisible(boolean status) changes the visibility of the component, by default false. To create simple awt example, you need a frame. There are two ways to create a frame in AWT. By extending Frame class (inheritance) By creating the object of Frame class (association) Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK 4. 2
Swing Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java. Unlike AWT, Java Swing provides platform-independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc. Difference between AWT and Swing There are many differences between java awt and swing that are given below. No. Java AWT 1) AWT components are platform-dependent. 2) AWT components are heavyweight. 3) AWT doesn't support pluggable look and feel. 4) AWT provides less components than Swing. AWT doesn't follows MVC(Model View Controller) where 5) model represents data, view represents presentation and controller acts as an interface between model and view. Java Swing Java swing components are platformindependent. Swing components are lightweight. Swing supports pluggable look and feel. Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc. Swing follows MVC. The hierarchy of java swing API is given below. Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK 4. 3
Commonly used Methods of Component class The methods of Component class are widely used in java swing that are given below. Method public void add(component c) Description add a component on another component. sets size of the component. public void setsize(int width,int height) public void setlayout(layoutmanager m) sets the layout manager for the component. public void setvisible(boolean b) sets the visibility of the component. It is by default false. Java Swing Examples There are two ways to create a frame: By creating the object of Frame class (association) By extending Frame class (inheritance) We can write the code of swing inside the main(), constructor or any other method. Java ActionListener Interface The Java ActionListener is notified whenever you click on the button or menu item. It is notified against ActionEvent. The ActionListener interface is found in java.awt.event package. It has only one method: actionperformed(). The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods. The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one method: itemstatechanged(). The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java.awt.event package. It has three methods. The Java WindowListener is notified whenever you change the state of window. It is notified against WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods. Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code. The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. The Adapter classes with their corresponding listener interfaces are given below. java.awt.event Adapter classes Adapter class Listener interface WindowAdapter WindowListener KeyAdapter KeyListener MouseAdapter MouseListener MouseMotionAdapter MouseMotionListener Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK 4. 4
FocusAdapter FocusListener ComponentAdapter ComponentListener ContainerAdapter ContainerListener HierarchyBoundsAdapter HierarchyBoundsListener 1.8.1 Design and Program Flow: 1. Write Student.java program for Display the Student details like Roll no, name and marks. 2. Write Client.java program Perform Various Operation on Student database like Add Student, Search Student, Deleted Student and Failed Student List etc. 3. Write Operation.java program perform various Operations using ArrayList. 4. Write ServerDemo.java program for Connection establishment with Client. 5. Write Db.java program for Open and Close Connection using MySql. 6. Write ClientUI.java program for design GUI based application to perform all operation. 1.8.2 Conclusion: In this way now students are able to implement the GUI based application using Swing concept successfully. 1.9 Assignment Questions: 1. What is Event-Dispatcher-Thread (EDT) in Swing? 2. What are differences between Swing and AWT? 3. Why Swing components are called lightweight components? 4. What is Action Listener? 5. What is Event? 1. 9.1 Oral Questions: 1. What is JFC? 2. What is Layout Manager? 3. What is Spring Layout? 4. What is Box Layout? 5. What is Gridbag Layout? 1.9.2 References : 1. https://www.javatpoint.com/java-springlayout Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK 4. 5