SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Checkbox and CheckboxGroup




   http://improvejava.blogspot.in/
                                     1
Objective

On completion of this period, you would be
able to learn

• Checkbox
• Checkboxgroup




              http://improvejava.blogspot.in/
                                                2
Recap

In the previous class, you have leant

• Displaying information within a window
   • Working with color and font




                  http://improvejava.blogspot.in/
                                                    3
Checkbox
• A check box is a control that is used to turn an option
  on or off
• It consists of a small box that can either contain a
  check mark or not
• There is a label associated with each check box that
  describes what option the box represents
• You change the state of a check box by clicking on it
• Check boxes can be used individually or as part of a
  group
• Check boxes are objects of the Checkbox class




                     http://improvejava.blogspot.in/        4
Checkbox                         contd..


• Checkbox supports these constructors:
  –   Checkbox( )
  –   Checkbox(String str)
  –   Checkbox(String str, boolean on)
  –   Checkbox(String str, boolean on, CheckboxGroup
      cbGroup)




                    http://improvejava.blogspot.in/       5
Checkbox                           contd..

• The first form creates a check box whose label is initially
  blank
   – The state of the check box is unchecked
• The second form creates a check box whose label is
  specified by str
   – The state of the check box is unchecked
• The third form allows you to set the initial state of the
  check box
   – If on is true, the check box is initially checked; otherwise, it is
     cleared




                        http://improvejava.blogspot.in/                6
Checkbox                            contd..

• The fourth and fifth forms create a check box
  whose label is specified by str and whose group
  is specified by cbGroup
   – If this check box is not part of a group, then cbGroup
     must be null
   – The value of on determines the initial state of the
     check box
• To retrieve the current state of a check box, call
  getState( )
• To set its state, call setState( )

                    http://improvejava.blogspot.in/        7
Handling Check Boxes

• Each time a check box is selected or deselected, an item
  event is generated
• This is sent to any listeners that previously registered an
  interest in receiving item event notifications from that
  component
• Each listener implements the ItemListener interface
• It defines the itemStateChanged() method




                     http://improvejava.blogspot.in/        8
Handling Check Boxes                            contd..
// Demonstrate check boxes
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo" width=250 height=200>
</applet>
*/
public class CheckboxDemo extends Applet implements ItemListener {
    String msg = "";
    Checkbox Win98, winNT, solaris, mac;




                       http://improvejava.blogspot.in/               9
Handling Check Boxes                           contd..

public void init() {
     Win98 = new Checkbox("Windows 98/XP", null, true);
     winNT = new Checkbox("Windows NT/2000");
     solaris = new Checkbox("Solaris");
     mac = new Checkbox("MacOS");
     add(Win98);
     add(winNT);
     add(solaris);
     add(mac);
     Win98.addItemListener(this);
     winNT.addItemListener(this);
     solaris.addItemListener(this);
     mac.addItemListener(this);
}

                    http://improvejava.blogspot.in/             10
Handling Check Boxes                            contd..
    public void itemStateChanged(ItemEvent ie) {
          repaint();
    }
    // Display current state of the check boxes.
    public void paint(Graphics g) {
          msg = "Current state: ";
          g.drawString(msg, 6, 80);
          msg = " Windows 98/XP: " + Win98.getState();
          g.drawString(msg, 6, 100);
          msg = " Windows NT/2000: " + winNT.getState();
          g.drawString(msg, 6, 120);
          msg = " Solaris: " + solaris.getState();
          g.drawString(msg, 6, 140);
          msg = " MacOS: " + mac.getState();
          g.drawString(msg, 6, 160);
    }
}                        http://improvejava.blogspot.in/             11
Handling Check Boxes                       contd..



• Sample output is shown here




                Fig. 71.1 CheckboxDemo output

                    http://improvejava.blogspot.in/             12
CheckboxGroup
• It is possible to create a set of mutually exclusive check
  boxes in which
   – one and only one check box in the group can be checked at any
     one time
• These check boxes are often called radio buttons
• CheckboxGroup is used to create such a set of mutually
  exclusive check boxes
• You can determine which check box in a group is
  currently selected by calling
   – getSelectedCheckbox( )
• You can set a check box by calling
   – setSelectedCheckbox( )


                      http://improvejava.blogspot.in/           13
CheckboxGroup                                contd..

// Demonstrate check box group.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CBGroup" width=250 height=200>
</applet>
*/
public class CBGroup extends Applet implements ItemListener {
    String msg = "";
    Checkbox Win98, winNT, solaris, mac;
    CheckboxGroup cbg;



                        http://improvejava.blogspot.in/             14
CheckboxGroup                               contd..

public void init() {
     cbg = new CheckboxGroup();
     Win98 = new Checkbox("Windows 98/XP", cbg, true);
     winNT = new Checkbox("Windows NT/2000", cbg, false);
     solaris = new Checkbox("Solaris", cbg, false);
     mac = new Checkbox("MacOS", cbg, false);
     add(Win98);
     add(winNT);
     add(solaris);
     add(mac);
     Win98.addItemListener(this);
     winNT.addItemListener(this);
     solaris.addItemListener(this);
     mac.addItemListener(this);
}
                    http://improvejava.blogspot.in/             15
CheckboxGroup                                contd..

    public void itemStateChanged(ItemEvent ie) {
          repaint();
    }
    // Display current state of the check boxes.
    public void paint(Graphics g) {
          msg = "Current selection: ";
          msg += cbg.getSelectedCheckbox().getLabel();
          g.drawString(msg, 6, 100);
    }
}




                         http://improvejava.blogspot.in/             16
CheckboxGroup                             contd..


• Sample output is shown here




              Fig. 71.2 CBGroup output

                 http://improvejava.blogspot.in/             17
Summary

• In this class we have seen
   – Checkbox
   – Checkbox group
   – The related programs




                 http://improvejava.blogspot.in/   18
Quiz

1. How many types of constructors are discussed
    for a Checkbox
  a)   1
  b)   2
  c)   3
  d)   4




                 http://improvejava.blogspot.in/   19
Frequently Asked Questions

1. List and explain the constructors of Checkbox
2. List and explain the constructors of
  CheckboxGroup




                 http://improvejava.blogspot.in/   20

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
GCC compiler
GCC compilerGCC compiler
GCC compiler
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Layout manager
Layout managerLayout manager
Layout manager
 
CS6611 Mobile Application Development Lab Manual-2018-19
CS6611 Mobile Application Development Lab Manual-2018-19CS6611 Mobile Application Development Lab Manual-2018-19
CS6611 Mobile Application Development Lab Manual-2018-19
 
C# Lab Programs.pdf
C# Lab Programs.pdfC# Lab Programs.pdf
C# Lab Programs.pdf
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Vb.net class notes
Vb.net class notesVb.net class notes
Vb.net class notes
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Core java
Core javaCore java
Core java
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
Presentation of awk
Presentation of awkPresentation of awk
Presentation of awk
 

Ähnlich wie Checkbox and checkbox group

Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttonsmyrajendra
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbarsmyrajendra
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOSKremizas Kostas
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Bhushan Mulmule
 
Goroutine stack and local variable allocation in Go
Goroutine stack and local variable allocation in GoGoroutine stack and local variable allocation in Go
Goroutine stack and local variable allocation in GoYu-Shuan Hsieh
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsClare Macrae
 
Beirut Java User Group JVM presentation
Beirut Java User Group JVM presentationBeirut Java User Group JVM presentation
Beirut Java User Group JVM presentationMahmoud Anouti
 
Tool Development 02 - Advanced WPF Controls
Tool Development 02 - Advanced WPF ControlsTool Development 02 - Advanced WPF Controls
Tool Development 02 - Advanced WPF ControlsNick Pruehs
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projectsVincent Massol
 
JAVA Jcheckbox in simple and easy .pptx
JAVA Jcheckbox in simple and  easy .pptxJAVA Jcheckbox in simple and  easy .pptx
JAVA Jcheckbox in simple and easy .pptxPrajwalGowda320238
 
React Native One Day
React Native One DayReact Native One Day
React Native One DayTroy Miles
 
The Ring programming language version 1.6 book - Part 74 of 189
The Ring programming language version 1.6 book - Part 74 of 189The Ring programming language version 1.6 book - Part 74 of 189
The Ring programming language version 1.6 book - Part 74 of 189Mahmoud Samir Fayed
 
Combo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.pptCombo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.pptUjwala Junghare
 

Ähnlich wie Checkbox and checkbox group (20)

Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
 
GWT Widgets
GWT WidgetsGWT Widgets
GWT Widgets
 
Lists and scrollbars
Lists and scrollbarsLists and scrollbars
Lists and scrollbars
 
Swing basics
Swing basicsSwing basics
Swing basics
 
Java 102
Java 102Java 102
Java 102
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
 
Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3Windows Forms For Beginners Part - 3
Windows Forms For Beginners Part - 3
 
Goroutine stack and local variable allocation in Go
Goroutine stack and local variable allocation in GoGoroutine stack and local variable allocation in Go
Goroutine stack and local variable allocation in Go
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
Constructor
ConstructorConstructor
Constructor
 
Beirut Java User Group JVM presentation
Beirut Java User Group JVM presentationBeirut Java User Group JVM presentation
Beirut Java User Group JVM presentation
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Tool Development 02 - Advanced WPF Controls
Tool Development 02 - Advanced WPF ControlsTool Development 02 - Advanced WPF Controls
Tool Development 02 - Advanced WPF Controls
 
Qtp questions
Qtp questionsQtp questions
Qtp questions
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
JAVA Jcheckbox in simple and easy .pptx
JAVA Jcheckbox in simple and  easy .pptxJAVA Jcheckbox in simple and  easy .pptx
JAVA Jcheckbox in simple and easy .pptx
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
The Ring programming language version 1.6 book - Part 74 of 189
The Ring programming language version 1.6 book - Part 74 of 189The Ring programming language version 1.6 book - Part 74 of 189
The Ring programming language version 1.6 book - Part 74 of 189
 
Combo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.pptCombo box and List box in VB.Net.ppt
Combo box and List box in VB.Net.ppt
 
Easy mock
Easy mockEasy mock
Easy mock
 

Mehr von myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Checkbox and checkbox group

  • 1. Checkbox and CheckboxGroup http://improvejava.blogspot.in/ 1
  • 2. Objective On completion of this period, you would be able to learn • Checkbox • Checkboxgroup http://improvejava.blogspot.in/ 2
  • 3. Recap In the previous class, you have leant • Displaying information within a window • Working with color and font http://improvejava.blogspot.in/ 3
  • 4. Checkbox • A check box is a control that is used to turn an option on or off • It consists of a small box that can either contain a check mark or not • There is a label associated with each check box that describes what option the box represents • You change the state of a check box by clicking on it • Check boxes can be used individually or as part of a group • Check boxes are objects of the Checkbox class http://improvejava.blogspot.in/ 4
  • 5. Checkbox contd.. • Checkbox supports these constructors: – Checkbox( ) – Checkbox(String str) – Checkbox(String str, boolean on) – Checkbox(String str, boolean on, CheckboxGroup cbGroup) http://improvejava.blogspot.in/ 5
  • 6. Checkbox contd.. • The first form creates a check box whose label is initially blank – The state of the check box is unchecked • The second form creates a check box whose label is specified by str – The state of the check box is unchecked • The third form allows you to set the initial state of the check box – If on is true, the check box is initially checked; otherwise, it is cleared http://improvejava.blogspot.in/ 6
  • 7. Checkbox contd.. • The fourth and fifth forms create a check box whose label is specified by str and whose group is specified by cbGroup – If this check box is not part of a group, then cbGroup must be null – The value of on determines the initial state of the check box • To retrieve the current state of a check box, call getState( ) • To set its state, call setState( ) http://improvejava.blogspot.in/ 7
  • 8. Handling Check Boxes • Each time a check box is selected or deselected, an item event is generated • This is sent to any listeners that previously registered an interest in receiving item event notifications from that component • Each listener implements the ItemListener interface • It defines the itemStateChanged() method http://improvejava.blogspot.in/ 8
  • 9. Handling Check Boxes contd.. // Demonstrate check boxes import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="CheckboxDemo" width=250 height=200> </applet> */ public class CheckboxDemo extends Applet implements ItemListener { String msg = ""; Checkbox Win98, winNT, solaris, mac; http://improvejava.blogspot.in/ 9
  • 10. Handling Check Boxes contd.. public void init() { Win98 = new Checkbox("Windows 98/XP", null, true); winNT = new Checkbox("Windows NT/2000"); solaris = new Checkbox("Solaris"); mac = new Checkbox("MacOS"); add(Win98); add(winNT); add(solaris); add(mac); Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } http://improvejava.blogspot.in/ 10
  • 11. Handling Check Boxes contd.. public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current state: "; g.drawString(msg, 6, 80); msg = " Windows 98/XP: " + Win98.getState(); g.drawString(msg, 6, 100); msg = " Windows NT/2000: " + winNT.getState(); g.drawString(msg, 6, 120); msg = " Solaris: " + solaris.getState(); g.drawString(msg, 6, 140); msg = " MacOS: " + mac.getState(); g.drawString(msg, 6, 160); } } http://improvejava.blogspot.in/ 11
  • 12. Handling Check Boxes contd.. • Sample output is shown here Fig. 71.1 CheckboxDemo output http://improvejava.blogspot.in/ 12
  • 13. CheckboxGroup • It is possible to create a set of mutually exclusive check boxes in which – one and only one check box in the group can be checked at any one time • These check boxes are often called radio buttons • CheckboxGroup is used to create such a set of mutually exclusive check boxes • You can determine which check box in a group is currently selected by calling – getSelectedCheckbox( ) • You can set a check box by calling – setSelectedCheckbox( ) http://improvejava.blogspot.in/ 13
  • 14. CheckboxGroup contd.. // Demonstrate check box group. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="CBGroup" width=250 height=200> </applet> */ public class CBGroup extends Applet implements ItemListener { String msg = ""; Checkbox Win98, winNT, solaris, mac; CheckboxGroup cbg; http://improvejava.blogspot.in/ 14
  • 15. CheckboxGroup contd.. public void init() { cbg = new CheckboxGroup(); Win98 = new Checkbox("Windows 98/XP", cbg, true); winNT = new Checkbox("Windows NT/2000", cbg, false); solaris = new Checkbox("Solaris", cbg, false); mac = new Checkbox("MacOS", cbg, false); add(Win98); add(winNT); add(solaris); add(mac); Win98.addItemListener(this); winNT.addItemListener(this); solaris.addItemListener(this); mac.addItemListener(this); } http://improvejava.blogspot.in/ 15
  • 16. CheckboxGroup contd.. public void itemStateChanged(ItemEvent ie) { repaint(); } // Display current state of the check boxes. public void paint(Graphics g) { msg = "Current selection: "; msg += cbg.getSelectedCheckbox().getLabel(); g.drawString(msg, 6, 100); } } http://improvejava.blogspot.in/ 16
  • 17. CheckboxGroup contd.. • Sample output is shown here Fig. 71.2 CBGroup output http://improvejava.blogspot.in/ 17
  • 18. Summary • In this class we have seen – Checkbox – Checkbox group – The related programs http://improvejava.blogspot.in/ 18
  • 19. Quiz 1. How many types of constructors are discussed for a Checkbox a) 1 b) 2 c) 3 d) 4 http://improvejava.blogspot.in/ 19
  • 20. Frequently Asked Questions 1. List and explain the constructors of Checkbox 2. List and explain the constructors of CheckboxGroup http://improvejava.blogspot.in/ 20