UNIT 5 (OOPs)
UNIT 5 (OOPs)
GUI is the interface that uses graphical elements to let people interact as per requirement
with electronic devices including computers, laptops, tablets, and smartphones. In terms of
human-computer interaction systems or technology, it’s a very important component of
software application programming since it substitutes actions for the text-based commands
in the system. Whether it’s a text file, object, image, or video as per requirement, it displays
all types of required content a user could envision in the system. Additionally, it can be
featured in the gaming platform
History of GUI
In the year of 1981, Alan Kay, Douglas Engelbart, and other researchers at Xerox PARC (Palo
Alto Research Centre) developed the GUI technology. A GUI-equipped Lisa computer was
later released by Apple as per requirement on January 19, 1983. Before there was a
graphical user interface, communication was done via the command-line interface, or CLI to
manage the overall system. The end users did not find the CLI particularly enjoyable to use
and modify, so they were unfamiliar with all of the user-beneficial commands. One of the
most important features of GUI is “ease of use”. The individual system of Xerox 8010
Information System served as the first GUI-centric machine operating model in the
technology.
Components of GUI
Pointers: The pointer appears on the user’s screen as a marking symbol. The pointer
moves on to choose instructions and objects as per requirement.
Icons: Icons allude to tiny visual representations of windows, documents, actions,
and other things on the display screen to simplify. A pointer and pointing device can
be used by the user to carry out the initial tasks for the overall processes.
Pointing tool: At the initial stages, the pointing tool enables the user to select and
move the required pointer items on the screen, including a trackball or mouse. It is
the most beneficial tool in GUI.
Desktop: The desktop is the screen that is contained within the icons and user
beneficial.
Advantages of GUI
A graphical user interface (GUI) is basically seen as more intuitive for users than a
text-based command-line interface as per requirement, such MS-DOS or the Unix-like
operating system shell process.
It is incredibly user-friendly and readily available to all and for novices, the user
interface is rather easy to understand and uncomplicated as per requirement.
GUI represents the now-hidden lines of command with the required graphic
elements.
The end users must commit required instructions to memory for the software to
function properly.
Java Swing
Java Swing is a popular and powerful Graphical User Interface (GUI)
toolkit that is used for developing desktop applications. It is a part of
the Java Foundation Classes (JFC) and provides a rich set of
components and layout managers for creating a variety of GUIs. Java
Swing is platform-independent and can be used on any operating
system that supports Java.
Java Swing provides a pluggable look and feels that allows developers
to customize the GUI according to the user’s preferences. It also
provides a robust event-handling mechanism that allows developers to
handle events generated by the graphical components.
1. JFrame
2. JButton
3. JLabel
4. JTextField
5. CheckBox
6. JList
7. JTable
8. JTextArea
9. JPasswordField
10. JScrollPane
11. JRadioButton
12. JComboBox
1. JFrame: JFrame is a top-level container that represents the main
window of a GUI application. It provides a title bar, and minimizes,
maximizes, and closes buttons.
JFrame frame;
JPanel panel;
JButton b1,b2,b3,b4,b5;
StColor(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setSize(100, 50);
b1 = new JButton("BLUE"); //Creating a button named BLUE
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
frame.setSize(500,300);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
@Override
frame.getContentPane().setBackground(java.awt.Color.blue);
//changing the panel color to blue
frame.getContentPane().setBackground(java.awt.Color.red);
//changing the panel color to red
}
frame.getContentPane().setBackground(java.awt.Color.cyan);//changing
the panel color to cyan
frame.getContentPane().setBackground(java.awt.Color.pink);
//changing the panel color to pink
frame.getContentPane().setBackground(java.awt.Color.magenta);
//changing the panel color to magenta
class Test {
Output
2. JButton: JButton is a component that represents a clickable
buttonJButton class provides functionality of a button. It is used to
create button component. JButton class has three constuctors,
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
testswing()
setVisible(true);
new testswing();
Output
3. JLabel: JLabel is a component that displays text or an image. It is
commonly used to provide information or to label other
components.In Java, Swingtoolkit contains a JLabel Class. It is under
package javax.swing.JLabel class. It is used for placing text in a box.
Only Single line text is allowed and the text can not be changed
directly.
1. JLabel()
2. JLabel(String s)
3. JLabel(Icon i)
import javax.swing.*;
class SLabelDemo1
{
JLabel label_l1,label_l2;
label_l1.setBounds(50,50, 200,30);
label_l2.setBounds(50,100, 200,30);
label_f.add(label_l1);
label_f.add(label_l2);
label_f.setSize(300,300);
label_f.setLayout(null);
label_f.setVisible(true);
}
4. JTextField: JTextField is a component that allows the user to input
text. It is commonly used to get input from the user, such as a name
or an address. It is used for taking input of single line of text. It is most
widely used text component. It has three constructors,
JTextField(int cols)
JTextField(String str)
Copy
Program
In this example, we are creating text field using JtextField class and
adding into the jframe container.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
{
public MyTextField()
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new MyTextField();
Output
5. JCheckBox: JCheckBox is a component that represents a checkbox. It
is commonly used to get a binary input from the user, such as
whether or not to enable a feature.
JCheckBox(String str)
Program
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public Test()
{
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new Test();
Output
6. JList: JList is a component that represents a list of elements. It is
typically used to display a list of options from which the user can
select one or more items. In Java, Swing toolkit contains a JList Class.
It is under package javax.swing.JList class. It is used to represent a list
of items together. One or more than one item can be selected from
the list.
1. JList()
2. JList(ary[] listData)
3. JList(ListModel<ary> dataModel)
Program
import javax.swing.*;
SListDemo()
list_l1.addElement("Red");
list_l1.addElement("Pink");
list_l1.addElement("Blue");
list_l1.addElement("Black");
list1.setBounds(100,100, 75,75);
list_f.add(list1);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
new SListDemo();
}
7. JTable: JTable is a component that represents a data table. It is
typically used to present data in a tabular fashion, such as a list of
products or a list of orders.
1. JTable()
Program:In this we create a table using Jtable class and then add it to
the Jframe container.
import javax.swing.*;
JFrame table_f;
STableDemo1(){
table_f=new JFrame();
String table_column[]={"SID","SNAME"};
table_jt.setBounds(30,40,200,300);
table_f.add(table_sp);
table_f.setSize(300,400);
table_f.setVisible(true);
new STableDemo1();
Output
8. JTextArea
1. JTextArea()
2. JTextArea(String s)
Program
import javax.swing.*;
{
STextAreaDemo1()
textArea_area.setBounds(10,30, 200,200);
textArea_f.add(textArea_area);
textArea_f.setSize(400,400);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
new STextAreaDemo1();
Output
9. JPasswordField
1. JPasswordField()
2. JPasswordField(int columns)
3. JPasswordField(String text)
passWord_l1.setBounds(20,100, 100,30);
passWord_value.setBounds(100,100,100,30);
passWord_f.add(passWord_value);
passWord_f.add(passWord_l1);
passWord_f.setSize(300,300);
passWord_f.setLayout(null);
passWord_f.setVisible(true);
Output
10. JScrollPane: JScrollPane is a component that provides scrolling
functionality to other components. It is commonly used to add
scrolling to a panel or a table.JRadioButton
JRadioButton(String str)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public Test()
{
add(jcb);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new Test();
Output:
12. JComboBox:Combo box is a combination of text fields and drop-
down list.JComboBox component is used to create a combo box in
Swing. Following is the constructor for JComboBox,
JComboBox(String arr[])
Copy
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public Test()
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
new Test();
Output
2. Look and Feel: While Swing’s pluggable look and feel feature
allows for customization of the components, it can be difficult to
achieve a truly native look and feel. This can make Swing
applications look and feel different from other native applications on
the same platform, which can be confusing for users.
o Heavyweight.
2. Lightweight Containers
o Example: JPanel
JPanel: JPanel is a container that can hold other
components. It is commonly used to group related components
together.
Constructors of JPanel
3. updateUI(): resets the UI property with a value from the current look
and feel.
4. setUI(PanelUI ui): sets the look and feel of an object that renders
this component.
5. getUI(): returns the look and feel object that renders this
component.
7. getUIClassID(): returns the name of the Look and feel class that
renders this component.
Let us take a sample program in order to illustrate the use of JPanel class
// Java Program to Create a Simple JPanel
// and Adding Components to it
// Class 1
// Helper class extending JFrame class
class solution extends JFrame {
// JFrame
static JFrame f;
// JButton
static JButton b, b1, b2;
// Main class
public static void main(String[] args)
{
// Creating a new frame to store text field and
// button
f = new JFrame("panel");
// setbackground of panel
p.setBackground(Color.red);
f.show();
}
}
Output
Look and Feel in java swing:Swing provides platform specific Look and
Feel. Initially there were very few options for colors and other settings in Java Swing,
that made the entire application look boring and monotonous. With the growth in
Java framework, new changes were introduced to make the UI better and thus giving
developer opportunity to enhance the look of a Java Swing Application.
Look refers to the appearance of GUI widgets.
Feel refers to the way the widgets behave.
Sun’s JRE provides the following L&Fs:
1. CrossPlatformLookAndFeel: this is the “Java L&F” also known as “Metal” that looks
the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the
default.
2. SystemLookAndFeel: here, the application uses the L&F that is default to the system
it is running on. The System L&F is determined at runtime, where the application asks
the system to return the name of the appropriate L&F. For Linux and Solaris, the
System L&Fs are “GTK+” if GTK+ 2.2 or later is installed, “Motif” otherwise. For
Windows, the System L&F is “Windows”.
3. Synth: the basis for creating your own look and feel with an XML file.
4. Multiplexing: a way to have the UI methods delegate to a number of different look
and feel implementations at the same time.
We can use UIManager to load the L&F class directly from classpath.
For which the code goes like this:
UIManager.setLookAndFeel("fully qualified name of look and feel");
Event:Change in the state of an object is known as Event, i.e., event describes the
change in the state of the source. Events are generated as a result of user interaction
with the graphical user interface components. For example, clicking on a button,
moving the mouse, entering a character through keyboard, selecting an item from
the list, and scrolling the page are the activities that causes an event to occur.
Types of Event
The events can be broadly classified into two categories −
Foreground Events − These events require direct interaction of the user. They are
generated as consequences of a person interacting with the graphical components in
the Graphical User Interface. For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item from list, scrolling the page,
etc.
Background Events − These events require the interaction of the end user. Operating
system interrupts, hardware or software failure, timer expiration, and operation
completion are some examples of background events.
Java uses the Delegation Event Model to handle the events. This model
defines the standard mechanism to generate and handle the events.
1. Event Source: This is the object that generates an event. For example, a
button can be an event source.
2. Event Object: When an event occurs, an event object is created. This
object contains information about the event.
3. Event Listener: This is an interface that defines methods to handle
specific types of events. For example, ActionListener is used to handle
action events like button clicks.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new java.awt.FlowLayout());
frame.add(button);
frame.setVisible(true);
}
}
In this example:
Why scala?
Scala has many reasons for being popular among programmers.
Easy to Start: Scala is a high level language so it is closer to other
popular programming languages like Java, C, C++. Thus it becomes
very easy to learn Scala for anyone. For Java programmers, Scala is
more easy to learn.
Contains best Features: Scala contains the features of different
languages like C, C++, Java, etc. which makes the it more useful,
scalable and productive.
Close integration with Java: The source code of the Scala is
designed in such a way that its compiler can interpret the Java
classes. Also, Its compiler can utilize the frameworks, Java Libraries,
and tools etc. After compilation, Scala programs can run on JVM.
Web – Based & Desktop Application Development: For the web
applications it provides the support by compiling to JavaScript.
Similarly for desktop applications, it can be compiled to JVM bytecode.
Used by Big Companies: Most of the popular companies like Apple,
Twitter, Walmart, Google etc. move their most of codes to Scala from
some other languages, reason being it is highly scalable and can be
used in backend operations.
Note-People always thinks that Scala is a extension of Java. But it is not
true. It is just completely interoperable with Java. Scala programs get
converted into .class file which contains Java Byte Code after the
successful compilation and then can run on JVM
Example : A simple program to print Hello world! Using scala( object-
oriented approach).
Object HelloWorld{
Def main(args:Array[String]){
Println(“Hello world”);
}
}
Output:
Hello world
Explaination-
object HelloWorld: object is the keyword which is used to create the
objects. Here “HelloWorld” is the name of the object.
def main(args: Array[String]): def is the keyword in Scala which is
used to define the function and “main” is the name of Main
Method. args: Array[String] are used for the command line arguments.
println(“Hello, Geeks!”): println is a method in Scala which is used to
display the string on console.
Advantages
Object- Oriented: Every value in Scala is an object so it is
a purely object-oriented programming language. The behavior and
type of objects are depicted by the classes and traits in Scala.
Functional: It is also a functional programming language as every
function is a value and every value is an object. It provides the
support for the high-order functions, nested functions, anonymous
functions, etc.
Statically Typed: The process of verifying and enforcing the
constraints of types is done at compile time in Scala. Unlike other
statically typed programming languages like C++, C, etc., Scala
doesn’t expect the redundant type information from the user. In most
cases, the user has no need to specify a type.
Extensible: New language constructs can be added to Scala in form
of libraries. Scala is designed to interpolate with the JRE(Java Runtime
Environment).
Concurrent & Synchronize Processing: Scala allows the user to
write the codes in an immutable manner that makes it easy to apply
the parallelism(Synchronize) and concurrency.
Run on JVM & Can Execute Java Code: Java and Scala have a
common runtime environment. So the user can easily move from Java
to Scala. The Scala compiler compiles the program into .class file,
containing the Bytecode that can be executed by JVM. All the classes
of Java SDK can be used by Scala. With the help of Scala user can
customize the Java classes.
Disadvantages
Sometimes, two approaches make the Scala hard to understand.
There is a limited number of Scala developers available in comparison
to Java developers.
It has no true-tail recursive optimization as it runs on JVM.
It always revolves around the object-oriented concept because every
function is value and every value is an object in Scala.