From the previous video of our course we understood everything about Object Oriented Programming in Java. In this video we are going to learn about Java Swing.
Java Foundation Classes are set of GUI components which simplify the development of desktop applications. Java Swing is a part of Java Foundation Classes. It is built on top of AWT (Abstract Windowing Toolkit) API and entirely written in Java.
1. Java Swing follows loosely based MVC architecture.
2. Model represents components data
3. View represents the UI
4. Controllers acts in between the Model and the view.
Java SE is freely available from the link download.java. So we can download a person based on our operating system. Follow the instruction to download Java and run the .exe to install Java on your machine.
Once you install Java on your machine, you will need to set environment variable to point to correct installation directories.
Every user interface considers the following three main aspects :
1. Ul elements : These are the core visual elements the user eventually sees and interacts with GWT provides a huge list of widely used and common elements varying from basic to complex.
2. Layouts : They define how UI elements should be organized on the screen and provide a final look and feel to the GUI (Graphical User Interface).
3. Behavior : These are events which occur when the user interacts with UI elements.
Every SWING controls inherits properties from following Component class hierarchy. Class and Description :
A Component is the abstract base class for the non menu user-interface controls of SWING. Component represents an object with graphical representation.
A Container is a component that can contain other SWING components.
Jcomponent : A JComponent is a base class for all swing UI components. In order to use a swing component that inherits from JComponent, component must be in a containment hierarchy whose root is a top-level Swing container.
Jlabel : Jlabel object is a component for placing text in a container.
Jbutton : Creates a labelled Button.
JColorChooser : Used to manipulate and select a color.
JCheckBox : use for selecting multiple choices.
JRadioButton : use for selecting one choice.
Jlist : used for creating list of text items.
Jcombobox : used for showing a menu of choices.
JTextField : used for single line of text.
Jpasswordfield : use for password fields.
Jtextarea : used for multiple lines of text.
ImageIcon : used for pointing icons from images.
Jscrollbar : used for representing scrollbar.
Joptionpane : provides a set of standard dialog boxes.
JFileChooser : Provides a dialog window from where the user can select a file.
JProgressBar : used for showing the percentage of completion.
JSlider : used for creating a slider.
Jspinner : used for selecting a number object value from an ordered sequence.
There are three top level containers in Swing. They are as follows :
1. JFrame : used for the application's main window (with an icon, a title, minimize/maximize/close buttons, an optional menu-bar, and a content-panel, as illustrated.
2. Jdialog : Used for secondary pop-up window (with a title, a close button, and a content-pane).
3. Japplet : Used for the applet's display-area (content-pane) inside a browser's window.
Change in the state of an object is known as Event in Java i.e. Event describes the change in state of source.
Events in Java are generated as result of user interaction with the graphical user interface components.
There are different types of Events in Java –
1. Foreground event in Java
2. Background event in Java
Foreground Events in Java :
Those events which require the direct interaction of user. They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse.
Background Events in Java :
Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure timer expires, an operation completion are the example of background events.
Event Handling in Java programming is the mechanism that controls the event and decides what should happen if an event occurs.
This mechanism have the code which is known as Event handler that is executed when an event occurs.
Java uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.
The Delegation Event Model has the following key participants namely :
The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler lava provide as with classes for source obiect.
It is also known as event handler. Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received the listener process the event an then returns.
Following are the steps involved in Event handling :
1. The User clicks the button and the event is generated.
2. Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object.
3. Event object is forwarded to the method of registered listener class.
4. The method is now got executed and returns.
So, this is how Events in Java works and we have also covered that what is Java Swing. Now in our next video we will learn about Comparator Interface and Comparable Interface in Java programming.
Have a friend to whom you would want to share this course?