SlideShare a Scribd company logo
Lecture 9
More on implementing Listeners




                 Object Oriented Programming
                 Eastern University, Dhaka
                         Md. Raihan Kibria
Add a combo box

                  We will add an
                  item change
                  listener and print
                  to console.

                  See next page
                  for code
public class ComboDemo extends JFrame {

    ComboDemo(){
      super.setBounds(0, 0, 500, 400);
      super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      super.setLayout(new FlowLayout());

        final JTextField text = new JTextField("Select an item");
        class MyItemListener implements ItemListener{

            JComboBox combo;
            MyItemListener(JComboBox combo){
              this.combo = combo;
            }

            public void itemStateChanged(ItemEvent e) {
              text.setText(combo.getSelectedItem().toString());
            }
        }

    JComboBox combo = new JComboBox(new String[]{"Square", "Circle",
"Triangle"});
    combo.addItemListener(new MyItemListener(combo));
    super.add(combo);
    super.add(text);

        super.setVisible(true);
    }

    public static void main(String[] args){
      new ComboDemo();
    }
}
Output
Overriding paint method of
                     Component
public class OverridePaintDemo {

    public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setBounds(0, 0, 500, 400);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setLayout(new FlowLayout());

        f.add(new MyFirstPanel());

        f.setVisible(true);
    }
}

class MyFirstPanel extends JPanel{
  MyFirstPanel(){
    setBackground(Color.GRAY);
    setPreferredSize(new Dimension(100, 100));
  }
}
Output




We want to add a circle to the panel above
public class OverridePaintDemo {

    public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setBounds(0, 0, 500, 400);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setLayout(new FlowLayout());

        f.add(new MyFirstPanel());

        f.setVisible(true);
    }
}

class MyFirstPanel extends JPanel{
  MyFirstPanel(){
    setBackground(Color.GRAY);
    setPreferredSize(new Dimension(100, 100));
  }

    public void paint(Graphics g) {
      super.paint(g);
      g.setColor(Color.RED);
      g.drawOval(5, 5, 90, 90);
    }
}
Output
Now we want to add a circle, a
      rectangle and a triangle
public class OverridePaintDemo {

    public static void main(String[] args) {
      JFrame f = new Jframe();
      f.setBounds(0, 0, 500, 400);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE   );
      f.setLayout(new FlowLayout());

        f.add(new MyFirstPanel());
        f.add(new MySecondPanel());
        f.add(new MyThirdPanel());

        f.setVisible(true);
    }
}

    More code next..
class MyFirstPanel extends JPanel{
  MyFirstPanel(){
    setBackground(Color.GRAY);
    setPreferredSize(new Dimension(100, 100));
  }

    public void paint(Graphics g) {
      super.paint(g);
      g.setColor(Color.RED);
      g.drawOval(5, 5, 90, 90);
    }
}

class MySecondPanel extends JPanel{
  MySecondPanel(){
    setBackground(Color.GRAY);
    setPreferredSize(new Dimension(100, 100));
  }

    public void paint(Graphics g) {
      super.paint(g);
      g.setColor(Color.RED);
      g.drawRect(10, 15, 80, 60);
    }
}

    More code next..
class MyThirdPanel extends JPanel{
  MyThirdPanel(){
    setBackground(Color.GRAY);
    setPreferredSize(new Dimension(100, 100));
  }

    public void paint(Graphics g) {
      super.paint(g);
      g.setColor(Color.RED);
      g.drawLine(10, 90, 90, 90);
      g.drawLine(10, 90, 90, 10);
      g.drawLine(90, 10, 90, 90);
    }
}
More on overriding
public class OverrideMethodDemo {

    public static void main(String[] args) {
      System.out.println("CGPA by class I: " + new   I().getCgpa());
      System.out.println("CGPA by class J: " + new J().getCgpa());
    }
}
class I{
  int[] gpas = new int[]{3, 4, 2, 4, 5};

    int calculateSum(){
      int sum = 0;
      for (int i=0; i<gpas.length; i++)
        sum += gpas[i];
      return sum;
    }

    public float getCgpa(){
      int sum = calculateSum();
        return sum/gpas.length;
    }
}

class J extends I{

    public float getCgpa() {
      int sum = calculateSum();
      return (float)sum / (float)gpas.length;
    }
}
Output
CGPA by class I: 3.0
CGPA by class J: 3.6

More Related Content

What's hot

Drinking the free kool-aid
Drinking the free kool-aidDrinking the free kool-aid
Drinking the free kool-aid
David Hoyt
 
The Ring programming language version 1.10 book - Part 127 of 212
The Ring programming language version 1.10 book - Part 127 of 212The Ring programming language version 1.10 book - Part 127 of 212
The Ring programming language version 1.10 book - Part 127 of 212
Mahmoud Samir Fayed
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
Fabio Collini
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Hammurabi
HammurabiHammurabi
Hammurabi
Mario Fusco
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
kenbot
 
Programação funcional em Python
Programação funcional em PythonProgramação funcional em Python
Programação funcional em Python
Juarez da Silva Bochi
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
Richie Cotton
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
tahir_ali786
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
Mahmoud Samir Fayed
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
pramodkumar1804
 
Teeing Up Python - Code Golf
Teeing Up Python - Code GolfTeeing Up Python - Code Golf
Teeing Up Python - Code Golf
Yelp Engineering
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)stasimus
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functions
kenbot
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
Martin McBride
 

What's hot (20)

Drinking the free kool-aid
Drinking the free kool-aidDrinking the free kool-aid
Drinking the free kool-aid
 
slides
slidesslides
slides
 
The Ring programming language version 1.10 book - Part 127 of 212
The Ring programming language version 1.10 book - Part 127 of 212The Ring programming language version 1.10 book - Part 127 of 212
The Ring programming language version 1.10 book - Part 127 of 212
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italyFrom java to kotlin beyond alt+shift+cmd+k - Droidcon italy
From java to kotlin beyond alt+shift+cmd+k - Droidcon italy
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
 
Programação funcional em Python
Programação funcional em PythonProgramação funcional em Python
Programação funcional em Python
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
 
Mcq cpup
Mcq cpupMcq cpup
Mcq cpup
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212
 
Code Generation
Code GenerationCode Generation
Code Generation
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Lr5
Lr5Lr5
Lr5
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
Teeing Up Python - Code Golf
Teeing Up Python - Code GolfTeeing Up Python - Code Golf
Teeing Up Python - Code Golf
 
Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)Introduction to Monads in Scala (1)
Introduction to Monads in Scala (1)
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functions
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 

Viewers also liked

DCRS Annual Review 2011
DCRS Annual Review 2011DCRS Annual Review 2011
DCRS Annual Review 2011
Kanda P.
 
DCFA Nov-Dec 2011 Newsletter
DCFA Nov-Dec 2011 NewsletterDCFA Nov-Dec 2011 Newsletter
DCFA Nov-Dec 2011 Newsletter
Kanda P.
 
DCRS ACC 2011
DCRS ACC 2011DCRS ACC 2011
DCRS ACC 2011
Kanda P.
 
PLC 110606 AGENDA
PLC 110606 AGENDAPLC 110606 AGENDA
PLC 110606 AGENDA
Kanda P.
 
110614 GEOFF'S LATEST MEDICAL BULLETIN
110614 GEOFF'S LATEST MEDICAL BULLETIN110614 GEOFF'S LATEST MEDICAL BULLETIN
110614 GEOFF'S LATEST MEDICAL BULLETIN
Kanda P.
 

Viewers also liked (7)

DCRS Annual Review 2011
DCRS Annual Review 2011DCRS Annual Review 2011
DCRS Annual Review 2011
 
DCFA Nov-Dec 2011 Newsletter
DCFA Nov-Dec 2011 NewsletterDCFA Nov-Dec 2011 Newsletter
DCFA Nov-Dec 2011 Newsletter
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
DCRS ACC 2011
DCRS ACC 2011DCRS ACC 2011
DCRS ACC 2011
 
PLC 110606 AGENDA
PLC 110606 AGENDAPLC 110606 AGENDA
PLC 110606 AGENDA
 
110614 GEOFF'S LATEST MEDICAL BULLETIN
110614 GEOFF'S LATEST MEDICAL BULLETIN110614 GEOFF'S LATEST MEDICAL BULLETIN
110614 GEOFF'S LATEST MEDICAL BULLETIN
 

Similar to Oop lecture9

Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
AroraRajinder1
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
 
package employeeType.employee;public class Employee {    private.pdf
package employeeType.employee;public class Employee {    private.pdfpackage employeeType.employee;public class Employee {    private.pdf
package employeeType.employee;public class Employee {    private.pdf
sharnapiyush773
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
Andres Almiray
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
JUSTSTYLISH3B2MOHALI
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
mary772
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
mccormicknadine86
 
Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Anton Arhipov
 
AJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docxAJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docx
RenuDeshmukh5
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
Iram Ramrajkar
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
Eric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
Matthew McCullough
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
Kuldeep Jain
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
venkt12345
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 
applet.docx
applet.docxapplet.docx
applet.docx
nofakeNews
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 

Similar to Oop lecture9 (20)

Oop lecture6
Oop lecture6Oop lecture6
Oop lecture6
 
Awt
AwtAwt
Awt
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Keep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdfKeep getting a null pointer exception for some odd reasonim creati.pdf
Keep getting a null pointer exception for some odd reasonim creati.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
package employeeType.employee;public class Employee {    private.pdf
package employeeType.employee;public class Employee {    private.pdfpackage employeeType.employee;public class Employee {    private.pdf
package employeeType.employee;public class Employee {    private.pdf
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
You are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdfYou are to simulate a dispatcher using a priority queue system in C+.pdf
You are to simulate a dispatcher using a priority queue system in C+.pdf
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docxCodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
CodeZipButtonDemo.javaCodeZipButtonDemo.java Demonstrate a p.docx
 
Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012
 
AJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docxAJP Practical Questions with Solution.docx
AJP Practical Questions with Solution.docx
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdfimport java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
import java.awt.Color;import java.awt.Insets;import java.awt.Con.pdf
 
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
applet.docx
applet.docxapplet.docx
applet.docx
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 

More from Shahriar Robbani

Group111
Group111Group111
SQL
SQLSQL

More from Shahriar Robbani (10)

Group111
Group111Group111
Group111
 
SQL
SQLSQL
SQL
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Oop lecture9 12
Oop lecture9 12Oop lecture9 12
Oop lecture9 12
 
Oop lecture8
Oop lecture8Oop lecture8
Oop lecture8
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Oop lecture3
Oop lecture3Oop lecture3
Oop lecture3
 
Oop lecture1
Oop lecture1Oop lecture1
Oop lecture1
 

Recently uploaded

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
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
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
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
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 

Recently uploaded (20)

The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
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
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
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
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 

Oop lecture9

  • 1. Lecture 9 More on implementing Listeners Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  • 2. Add a combo box We will add an item change listener and print to console. See next page for code
  • 3. public class ComboDemo extends JFrame { ComboDemo(){ super.setBounds(0, 0, 500, 400); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); super.setLayout(new FlowLayout()); final JTextField text = new JTextField("Select an item"); class MyItemListener implements ItemListener{ JComboBox combo; MyItemListener(JComboBox combo){ this.combo = combo; } public void itemStateChanged(ItemEvent e) { text.setText(combo.getSelectedItem().toString()); } } JComboBox combo = new JComboBox(new String[]{"Square", "Circle", "Triangle"}); combo.addItemListener(new MyItemListener(combo)); super.add(combo); super.add(text); super.setVisible(true); } public static void main(String[] args){ new ComboDemo(); } }
  • 5. Overriding paint method of Component public class OverridePaintDemo { public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.setVisible(true); } } class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } }
  • 6. Output We want to add a circle to the panel above
  • 7. public class OverridePaintDemo { public static void main(String[] args) { JFrame f = new JFrame(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.setVisible(true); } } class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); } }
  • 9. Now we want to add a circle, a rectangle and a triangle public class OverridePaintDemo { public static void main(String[] args) { JFrame f = new Jframe(); f.setBounds(0, 0, 500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); f.setLayout(new FlowLayout()); f.add(new MyFirstPanel()); f.add(new MySecondPanel()); f.add(new MyThirdPanel()); f.setVisible(true); } } More code next..
  • 10. class MyFirstPanel extends JPanel{ MyFirstPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawOval(5, 5, 90, 90); } } class MySecondPanel extends JPanel{ MySecondPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawRect(10, 15, 80, 60); } } More code next..
  • 11. class MyThirdPanel extends JPanel{ MyThirdPanel(){ setBackground(Color.GRAY); setPreferredSize(new Dimension(100, 100)); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawLine(10, 90, 90, 90); g.drawLine(10, 90, 90, 10); g.drawLine(90, 10, 90, 90); } }
  • 12.
  • 13. More on overriding public class OverrideMethodDemo { public static void main(String[] args) { System.out.println("CGPA by class I: " + new I().getCgpa()); System.out.println("CGPA by class J: " + new J().getCgpa()); } }
  • 14. class I{ int[] gpas = new int[]{3, 4, 2, 4, 5}; int calculateSum(){ int sum = 0; for (int i=0; i<gpas.length; i++) sum += gpas[i]; return sum; } public float getCgpa(){ int sum = calculateSum(); return sum/gpas.length; } } class J extends I{ public float getCgpa() { int sum = calculateSum(); return (float)sum / (float)gpas.length; } }
  • 15. Output CGPA by class I: 3.0 CGPA by class J: 3.6