









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Unit Description This unit deals with the general descriptions of java AWT and Swing components, the general difference between AWT and Swing components and how to create applications in java using GUI (graphical user interface). These contents will be delivered to the students through brain storming, group discussion, observation/demonstration and mini lecture, and students will have assessed with lab exercise, group work and individual lab assignment. Objectives: At the end of this unit, the students will be able to: Define AWT and Swing K L, Identify components of AWT and SWING A L, Distinguish the difference between AWT and Swing Components K L, Define event listeners and handlers’ K L, Develop a certain java application using AWT and SWING components A L, Contents: Definition and concept of AWT and Swing Difference between AWT and Swing Components of AWT and Swing Event handlers and listeners Method of teaching: Brain storming, gap lecture, group discussion Brainstorming: What are the Methods or Classes you know so far in java? 1.1. AWT 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.
The hierarchy of Java AWT classes are given below
The Container is a component in AWT that can contain another component like the one Buttons, TextFields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.
The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window Figure 5.1. Hierarchy of java AWT classes
The Panel is the container that does not contain title bar and menu bars. It can have other components like button, textfield etc.
The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
Object is the super class of all other classes you use, including the ones you implemented. In addition, as you see component and its elements are inherited from Object in every class.
The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example that sets the position of the AWT button. The output of previous java source code is displayed below on figure 1.2. AWT Example by Association Let us see a simple example of AWT where we are creating instance of Frame class. Here, we are showing Button component on the Frame. Figure 5. 1 AWT example by inheritance. Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling. Event and Listener (Java Event Handling)
Java Event classes and Listener interfaces Table 2 Java Event classes and Listener interfaces Perform Event Handling In order to perform event handling in java AWT we have to register the component with the Listener Registration Methods For registering the component with the Listener, many classes provide the registration methods. For example: Button public void addActionListener(ActionListener a){} MenuItem public void addActionListener(ActionListener a){} TextField public void addActionListener(ActionListener a){} public void addTextListener(TextListener a){} TextArea public void addTextListener(TextListener a){} Checkbox public void addItemListener(ItemListener a){} Choice
Java AWT Button The button class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. AWT Button Class declaration public class Button extends Component implements Accessible Java AWT Button Example
AWT Choice AWT List AWT Scrollbar AWT Menu and MenuItem AWT PopupMenu AWT Panel AWT dialog 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(). actionPerformed() method The actionPerformed() method is invoked automatically whenever you click on the registered component. public abstract void actionPerformed(ActionEvent e); How to write ActionListener The common approach is to implement the ActionListener. If you implement the ActionListener class, you need to follow 3 steps:
Java ActionListener Example: On Button click Java MouseListener Interface 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. Methods of MouseListener interface The signature of 5 methods found in MouseListener interface are given below:
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.
The methods of Component class are widely used in java swing that are given below. Table 3 Commonly used methods for component class 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. Simple Java Swing Example Let's see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() method. File: FirstSwingExample.java Example of Swing by Association inside constructor We can also write all the codes of creating JFrame, JButton and method call inside the java constructor. File: Simple.java
JButton class declaration Let us see the declaration for javax.swing.JButton class. public class JButton extends AbstractButton implements Accessible
Commonly used Methods of AbstractButton class:
Example of displaying image on the button: Question 1 : - what are the commonly used methods, and constructors and write sample code of the following Swing components? JLabel JTextField JTextArea JPasswordField JCheckBox JRadioButton JComboBox JTable JList JOptionPane JColorChooser