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);
    }
}
Oop lecture9
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 (20)

Drinking the free kool-aid
Drinking the free kool-aidDrinking the free kool-aid
Drinking the free kool-aid
David Hoyt
 
slides
slidesslides
slides
thamerr
 
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
 
Code Generation
Code GenerationCode Generation
Code Generation
Dmitri Nesteruk
 
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
 
Lr5
Lr5Lr5
Lr5
metallurg056
 
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
 
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
 
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
 
Running Free with the Monads
Running Free with the MonadsRunning Free with the Monads
Running Free with the Monads
kenbot
 
The secrets of inverse brogramming
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
Richie Cotton
 
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
 
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
 

Viewers also liked (7)

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.
 
Oop lecture4
Oop lecture4Oop lecture4
Oop lecture4
Shahriar Robbani
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
Shahriar Robbani
 
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.
 
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.
 

Similar to Oop lecture9 (20)

Oop lecture6
Oop lecture6Oop lecture6
Oop lecture6
Shahriar Robbani
 
Awt
AwtAwt
Awt
Swarup Saha
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
Shahriar Robbani
 
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.2012
Anton 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
Matthew McCullough
 
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
 
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
 
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
 
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.2012
Anton 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
Matthew McCullough
 
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
 
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
 
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
 

More from Shahriar Robbani (10)

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

Recently uploaded (20)

Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docxPeer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
19lburrell
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERSIMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
rajaselviazhagiri1
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
The History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.pptThe History of Kashmir Lohar Dynasty NEP.ppt
The History of Kashmir Lohar Dynasty NEP.ppt
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docxPeer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
Peer Assessment_ Unit 2 Skills Development for Live Performance - for Libby.docx
19lburrell
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
Rebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter worldRebuilding the library community in a post-Twitter world
Rebuilding the library community in a post-Twitter world
Ned Potter
 
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERSIMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
IMPACT_OF_SOCIAL-MEDIA- AMONG- TEENAGERS
rajaselviazhagiri1
 
How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18How to Use Upgrade Code Command in Odoo 18
How to Use Upgrade Code Command in Odoo 18
Celine George
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFAMCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
MCQS (EMERGENCY NURSING) DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Cyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top QuestionsCyber security COPA ITI MCQ Top Questions
Cyber security COPA ITI MCQ Top Questions
SONU HEETSON
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 

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); } }
  • 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
  翻译: