SlideShare a Scribd company logo
Interfaces
• Lists a set of methods and their signatures
– A class that ‘implements’ the interface must implement
all of the methods of the interface
– It is similar to a class, but there are differences:
• All methods in an interface type are abstract
They have a name, parameters, and a return type, but they
don’t have an implementation
• All methods in an interface type are automatically public
• An interface type cannot have instance variables
• An interface type cannot have static methods
Interface Syntax
• An interface declaration and a class that
implements the interface.
FileHelper Interface for the Classroom
Project
public interface FileHelper {
public boolean doesAFileExist();
public ArrayList<?> readFile();
public boolean writeFile(ArrayList<?> list);
}
Generics
• Code that uses generics has many benefits over non-
generic code:
– Elimination of casts. The following code snippet
without generics requires casting:
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
– When re-written to use generics, the code does not
require casting:
• List<String> list = new ArrayList<String>();
• list.add("hello");
• String s = list.get(0); // no cast
• Enables programmers to implement generic algorithms
– Implementing generic algorithms that work on
collections of different types, can be customized, and
are type safe and easier to read.
ClassroomFileHelper
public class ClassroomFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
InstructorFileHelper
public class InstructorFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
CourseFileHelper
public class CourseFileHelper implements FileHelper {
@Override
public boolean doesAFileExist(){
}
@Override
public ArrayList<?> readFile(){
}
@Override
public boolean writeFile(ArrayList<?> list){
}
}
Demo in Eclipse
• Implementing interfaces quickly
Moves
• heal( )
• attack( )
• jump( )
• defend( )
Good Guys
Bad Guys
implements
Items of notes
-return types
-parameters
-@Override
Java Swing
Frame Windows
 Java provides classes to create graphical applications
that can run on any major graphical user interface
 A graphical application shows information inside a
frame: a window with a title bar
 Java’s JFrame class allows you to display a frame
 It is part of the javax.swing package
Five steps to displaying a frame
1) Construct an object of the JFrame class in the main method
2) Set the size of the frame
3) Set the title of the frame
4) Set the “default close operation”
5) Make it visible
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setTitle(“An Empty Frame”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible (true);
Adding Components to the JFrame
 You cannot draw directly on a JFrame object
 Instead, construct an object and add it to the frame
 A few examples objects to add are:
 JComponent
 JPanel
 JTextComponent
 Jlabel
 If you have more than one component, put them into a
panel (a container for other user-interface components),
and then add the panel to the frame
Create the Components
public class FormPanel extends JPanel{
JButton button = new JButton("Click me!");
JLabel label = new JLabel("Hello, World!");
public FormPanel( ) {
add(button);
add(label);
}
}
• Design a subclass of JFrame
• Store the components as
instance variables
• Initialize them in the
constructor of your subclass
Add the Panel to the Frame
public class StartProgram {
public static void main(String[ ] args) {
JFrame frame = new JFrame();
JPanel panel = new FormPanel();
frame.add(panel);
frame.setSize(250, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Project Notes
• Let Eclipse handle the imports
• Divide classes by function
– model
– view
– tests
– [controller]
• Always set the frame visible last
Your Book Favors…
private void createComponents() {
button = new JButton("Click me!");
label = new JLabel("Hello, World!");
JPanel panel = new JPanel();
panel.add(button);
panel.add(label);
add(panel);
}
Your Book Favors…
public class FilledFrame extends Jframe {
private JButton button;
private JLabel label;
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 100;
public FilledFrame()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
}
Your Book Favors
public class FilledFrameViewer2 {
public static void main(String[] args)
{
JFrame frame = new FilledFrame();
frame.setTitle("A frame with two
components");
frame.setDefaultCloseOperation(JFrame.EXIT
_ON_CLOSE);
frame.setVisible(true);
}
}
Event Handling
 A program must indicate which events it wants to receive
 It does so by installing event listener objects
 An event listener object belongs to a class that you declare
 The methods of your event listener classes contain the
instructions that you want to have executed when the
events occur
 To install a listener, you need to know the event source
 You add an event listener object to selected event sources
 OK Button clicked, Cancel Button clicked, Menu Choice
 Whenever the event occurs, the event source calls the
appropriate methods of the attached event listeners
Implementation for Event Handling
• Create the class as an inner class inside the class
that contains the elements you want to listen
– Typically in your JPanel because your buttons are
created in it
• Implement the ActionListener interface
– Add the method
– Inside the method are the instructions to execute
once the action has been triggered
• Add the ActionListener to the Button
Inner Classes
 Inner classes are often used for ActionListeners
 An inner class is a class that is declared inside another class
 It may be declared inside or outside a method of the class
 Why inner classes? Two reasons:
1) It places the trivial listener class exactly where it is
needed, without cluttering up the remainder of the project
2) Their methods can access variables that are declared in
surrounding blocks
public class ButtonFrame2 extends JFrame
{
private JButton button;
private JLabel label;
. . .
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent
event)
{
label.setText("I was clicked");
}
}
. . .
}
Can easily access methods of
the private instance of a label
object.
Outer
Block
Inner
Block
Class & Interface Implementation
public class ClearButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dollarField.setText("");
euroField.setText("");
gbpField.setText("");
}
Attaching to Button
//inside the JPanel constructor
ClearButtonListener clearlistener = new ClearButtonListener( );
clearButton.addActionListener(clearlistener);
add(clearButton);
In a way, these are the ‘controllers’ for
our Swing interfaces. They are
contained in another class though.
Demo in Eclipse
StartProgram.java
Main Method
Creates the JFrame
Adds the JPanel to it
ConverterPanel
extends JPanel
All the components
All the clicklisteners
CurrencyConverter
Contains the business logic
Actually does the conversion
Demo in Eclipse
StartProgram.java
Main Method
Creates the JFrame
Adds the JPanel to it
ConverterPanel
extends JPanel
All the components
All the clicklisteners
model
package
view
packag
e
default
package
CurrencyConverter
Contains the business logic
Actually does the conversion

More Related Content

What's hot

Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
M Vishnuvardhan Reddy
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
DevaKumari Vijay
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
Lorna Mitchell
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
Padma Kannan
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 

What's hot (20)

Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Unit3 part3-packages and interfaces
Unit3 part3-packages and interfacesUnit3 part3-packages and interfaces
Unit3 part3-packages and interfaces
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 

Similar to Logic and Coding of Java Interfaces & Swing Applications

Gui
GuiGui
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
OktJona
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
Chhom Karath
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).ppt
ssuser076380
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
Partnered Health
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
SamyakJain710491
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
Payal Dungarwal
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
PRN USM
 
Chapter 2 JavaFX UI Controls and Multimedia.pptx
Chapter 2 JavaFX UI Controls and Multimedia.pptxChapter 2 JavaFX UI Controls and Multimedia.pptx
Chapter 2 JavaFX UI Controls and Multimedia.pptx
SamatarHussein
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
Kalai Selvi
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
yugandhar vadlamudi
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
abdullah al mahamud rosi
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Cse java
Cse javaCse java
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 

Similar to Logic and Coding of Java Interfaces & Swing Applications (20)

Gui
GuiGui
Gui
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
 
Interface
InterfaceInterface
Interface
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Chapter iv(modern gui)
Chapter iv(modern gui)Chapter iv(modern gui)
Chapter iv(modern gui)
 
Java Swing Handson Session (1).ppt
Java Swing Handson Session (1).pptJava Swing Handson Session (1).ppt
Java Swing Handson Session (1).ppt
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
SWING.pptx
SWING.pptxSWING.pptx
SWING.pptx
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Chapter 2 JavaFX UI Controls and Multimedia.pptx
Chapter 2 JavaFX UI Controls and Multimedia.pptxChapter 2 JavaFX UI Controls and Multimedia.pptx
Chapter 2 JavaFX UI Controls and Multimedia.pptx
 
Unit3 packages &amp; interfaces
Unit3 packages &amp; interfacesUnit3 packages &amp; interfaces
Unit3 packages &amp; interfaces
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
 
#_ varible function
#_ varible function #_ varible function
#_ varible function
 
Design patterns
Design patternsDesign patterns
Design patterns
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
 
Cse java
Cse javaCse java
Cse java
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 

More from kjkleindorfer

Week11 Inheritance class relationships in Java
Week11 Inheritance class relationships in JavaWeek11 Inheritance class relationships in Java
Week11 Inheritance class relationships in Java
kjkleindorfer
 
Intro to Bootstrap
Intro to BootstrapIntro to Bootstrap
Intro to Bootstrap
kjkleindorfer
 
Layouts Part 2
Layouts Part 2Layouts Part 2
Layouts Part 2
kjkleindorfer
 
Layouts
Layouts Layouts
Layouts
kjkleindorfer
 
Using PHP to submit forms
Using PHP to submit formsUsing PHP to submit forms
Using PHP to submit forms
kjkleindorfer
 
Forms Part 1
Forms Part 1Forms Part 1
Forms Part 1
kjkleindorfer
 
CSS Box Model
CSS Box ModelCSS Box Model
CSS Box Model
kjkleindorfer
 
CSS Selectors and Fonts
CSS Selectors and FontsCSS Selectors and Fonts
CSS Selectors and Fonts
kjkleindorfer
 

More from kjkleindorfer (8)

Week11 Inheritance class relationships in Java
Week11 Inheritance class relationships in JavaWeek11 Inheritance class relationships in Java
Week11 Inheritance class relationships in Java
 
Intro to Bootstrap
Intro to BootstrapIntro to Bootstrap
Intro to Bootstrap
 
Layouts Part 2
Layouts Part 2Layouts Part 2
Layouts Part 2
 
Layouts
Layouts Layouts
Layouts
 
Using PHP to submit forms
Using PHP to submit formsUsing PHP to submit forms
Using PHP to submit forms
 
Forms Part 1
Forms Part 1Forms Part 1
Forms Part 1
 
CSS Box Model
CSS Box ModelCSS Box Model
CSS Box Model
 
CSS Selectors and Fonts
CSS Selectors and FontsCSS Selectors and Fonts
CSS Selectors and Fonts
 

Recently uploaded

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 

Recently uploaded (20)

Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 

Logic and Coding of Java Interfaces & Swing Applications

  • 1. Interfaces • Lists a set of methods and their signatures – A class that ‘implements’ the interface must implement all of the methods of the interface – It is similar to a class, but there are differences: • All methods in an interface type are abstract They have a name, parameters, and a return type, but they don’t have an implementation • All methods in an interface type are automatically public • An interface type cannot have instance variables • An interface type cannot have static methods
  • 2. Interface Syntax • An interface declaration and a class that implements the interface.
  • 3. FileHelper Interface for the Classroom Project public interface FileHelper { public boolean doesAFileExist(); public ArrayList<?> readFile(); public boolean writeFile(ArrayList<?> list); }
  • 4. Generics • Code that uses generics has many benefits over non- generic code: – Elimination of casts. The following code snippet without generics requires casting: List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); – When re-written to use generics, the code does not require casting: • List<String> list = new ArrayList<String>(); • list.add("hello"); • String s = list.get(0); // no cast • Enables programmers to implement generic algorithms – Implementing generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.
  • 5. ClassroomFileHelper public class ClassroomFileHelper implements FileHelper { @Override public boolean doesAFileExist(){ } @Override public ArrayList<?> readFile(){ } @Override public boolean writeFile(ArrayList<?> list){ } }
  • 6. InstructorFileHelper public class InstructorFileHelper implements FileHelper { @Override public boolean doesAFileExist(){ } @Override public ArrayList<?> readFile(){ } @Override public boolean writeFile(ArrayList<?> list){ } }
  • 7. CourseFileHelper public class CourseFileHelper implements FileHelper { @Override public boolean doesAFileExist(){ } @Override public ArrayList<?> readFile(){ } @Override public boolean writeFile(ArrayList<?> list){ } }
  • 8. Demo in Eclipse • Implementing interfaces quickly Moves • heal( ) • attack( ) • jump( ) • defend( ) Good Guys Bad Guys implements Items of notes -return types -parameters -@Override
  • 10. Frame Windows  Java provides classes to create graphical applications that can run on any major graphical user interface  A graphical application shows information inside a frame: a window with a title bar  Java’s JFrame class allows you to display a frame  It is part of the javax.swing package
  • 11. Five steps to displaying a frame 1) Construct an object of the JFrame class in the main method 2) Set the size of the frame 3) Set the title of the frame 4) Set the “default close operation” 5) Make it visible JFrame frame = new JFrame(); frame.setSize(300,400); frame.setTitle(“An Empty Frame”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible (true);
  • 12. Adding Components to the JFrame  You cannot draw directly on a JFrame object  Instead, construct an object and add it to the frame  A few examples objects to add are:  JComponent  JPanel  JTextComponent  Jlabel  If you have more than one component, put them into a panel (a container for other user-interface components), and then add the panel to the frame
  • 13. Create the Components public class FormPanel extends JPanel{ JButton button = new JButton("Click me!"); JLabel label = new JLabel("Hello, World!"); public FormPanel( ) { add(button); add(label); } } • Design a subclass of JFrame • Store the components as instance variables • Initialize them in the constructor of your subclass
  • 14. Add the Panel to the Frame public class StartProgram { public static void main(String[ ] args) { JFrame frame = new JFrame(); JPanel panel = new FormPanel(); frame.add(panel); frame.setSize(250, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
  • 15. Project Notes • Let Eclipse handle the imports • Divide classes by function – model – view – tests – [controller] • Always set the frame visible last
  • 16. Your Book Favors… private void createComponents() { button = new JButton("Click me!"); label = new JLabel("Hello, World!"); JPanel panel = new JPanel(); panel.add(button); panel.add(label); add(panel); }
  • 17. Your Book Favors… public class FilledFrame extends Jframe { private JButton button; private JLabel label; private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 100; public FilledFrame() { createComponents(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } }
  • 18. Your Book Favors public class FilledFrameViewer2 { public static void main(String[] args) { JFrame frame = new FilledFrame(); frame.setTitle("A frame with two components"); frame.setDefaultCloseOperation(JFrame.EXIT _ON_CLOSE); frame.setVisible(true); } }
  • 19. Event Handling  A program must indicate which events it wants to receive  It does so by installing event listener objects  An event listener object belongs to a class that you declare  The methods of your event listener classes contain the instructions that you want to have executed when the events occur  To install a listener, you need to know the event source  You add an event listener object to selected event sources  OK Button clicked, Cancel Button clicked, Menu Choice  Whenever the event occurs, the event source calls the appropriate methods of the attached event listeners
  • 20. Implementation for Event Handling • Create the class as an inner class inside the class that contains the elements you want to listen – Typically in your JPanel because your buttons are created in it • Implement the ActionListener interface – Add the method – Inside the method are the instructions to execute once the action has been triggered • Add the ActionListener to the Button
  • 21. Inner Classes  Inner classes are often used for ActionListeners  An inner class is a class that is declared inside another class  It may be declared inside or outside a method of the class  Why inner classes? Two reasons: 1) It places the trivial listener class exactly where it is needed, without cluttering up the remainder of the project 2) Their methods can access variables that are declared in surrounding blocks
  • 22. public class ButtonFrame2 extends JFrame { private JButton button; private JLabel label; . . . class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { label.setText("I was clicked"); } } . . . } Can easily access methods of the private instance of a label object. Outer Block Inner Block
  • 23. Class & Interface Implementation public class ClearButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dollarField.setText(""); euroField.setText(""); gbpField.setText(""); }
  • 24. Attaching to Button //inside the JPanel constructor ClearButtonListener clearlistener = new ClearButtonListener( ); clearButton.addActionListener(clearlistener); add(clearButton); In a way, these are the ‘controllers’ for our Swing interfaces. They are contained in another class though.
  • 25. Demo in Eclipse StartProgram.java Main Method Creates the JFrame Adds the JPanel to it ConverterPanel extends JPanel All the components All the clicklisteners CurrencyConverter Contains the business logic Actually does the conversion
  • 26. Demo in Eclipse StartProgram.java Main Method Creates the JFrame Adds the JPanel to it ConverterPanel extends JPanel All the components All the clicklisteners model package view packag e default package CurrencyConverter Contains the business logic Actually does the conversion