Friday 9 September 2016

Set Multiple Selection Mode For AWT List Example

  1. /*
  2.         Set Multiple Selection Mode For AWT List Example
  3.         This java example shows how to set multiple selection mode for a list
  4.         using Java AWT List class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Graphics;
  9. import java.awt.List;
  10.  
  11. /*
  12. <applet code="SetMultiSelectModeExample" width=200 height=200>
  13. </applet>
  14. */
  15. public class SetMultiSelectModeExample extends Applet{
  16.  
  17.         List list = null;
  18.        
  19.         public void init(){
  20.                
  21.                 //create a single selection list
  22.                 list = new List(5);
  23.                
  24.  
  25.                 //add items
  26.                 list.add("One");
  27.                 list.add("Two");
  28.                 list.add("Three");
  29.                 list.add("Four");
  30.                 list.add("Five");
  31.                 list.add("Six");
  32.                 list.add("Seven");
  33.                
  34.                 //add list
  35.                 add(list);
  36.                
  37.                 /*
  38.                  * To set multiple selection mode for a list, use
  39.                  * void setMultipleMode(boolean mode)
  40.                  * method.
  41.                  */
  42.                
  43.                 list.setMultipleMode(true);
  44.         }
  45.        
  46.         public void paint(Graphics g){
  47.                 g.drawString("Is Multiple Selection Allowed? " + list.isMultipleMode(), 10, 120);
  48.         }
  49. }

Get Selected Item Of AWT Choice Or Combobox Example

  1. /*
  2.         Get Selected Item Of AWT Choice Or Combobox Example
  3.         This java example shows how to get selected item of a choice or combobox
  4.         using Java AWT Choice class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Choice;
  9. import java.awt.Graphics;
  10. import java.awt.event.ItemEvent;
  11. import java.awt.event.ItemListener;
  12.  
  13. /*
  14. <applet code="GetSelectedItemExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class GetSelectedItemExample extends Applet implements ItemListener{
  19.  
  20.         Choice language = null;
  21.        
  22.         public void init(){
  23.                
  24.                 //create choice or combobox
  25.                 language = new Choice();
  26.                
  27.                 //add items to the choice
  28.                 language.add("Java");
  29.                 language.add("C++");
  30.                 language.add("VB");
  31.                 language.add("Perl");
  32.                
  33.                 //add choice or combobox
  34.                 add(language);
  35.                
  36.                 //add item listener
  37.                 language.addItemListener(this);
  38.                
  39.         }
  40.        
  41.         public void paint(Graphics g){
  42.                 /*
  43.                  * To get selected item, use
  44.                  * String getSelectedItem()
  45.                  * method of AWT Choice class.
  46.                  */
  47.                 g.drawString(language.getSelectedItem(),10, 70);
  48.         }
  49.  
  50.         public void itemStateChanged(ItemEvent arg0) {
  51.                 repaint();             
  52.         }
  53. }

Set Action Command For AWT Button Example

  1. /*
  2.         Set Action Command For AWT Button Example
  3.         This java example shows how to set custom action command for AWT button using
  4.         setActionCommand method of Java AWT Button class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Button;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. /*
  14. <applet code="ButtonSetActionCommandExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class ButtonSetActionCommandExample extends Applet implements ActionListener{
  19.  
  20.         String actionMessage="";
  21.        
  22.         public void init(){
  23.                 //create Button
  24.                 Button Button1 = new Button("I agree with the terms and conditions");
  25.                                
  26.                 //add Button
  27.                 add(Button1);
  28.                                
  29.                 //set action listeners for buttons
  30.                 Button1.addActionListener(this);
  31.                
  32.                 /*
  33.                  * By default, button's action command is it's label. But in
  34.                  * some cases, labels are too long and is not appropriate to use
  35.                  * it as an action command. In such situation you would want to
  36.                  * define custom short action command for a button.
  37.                  *
  38.                  * To set custom action command for a button, use
  39.                  * void setActionCommand(String command)
  40.                  * method of AWT Button class.
  41.                  */
  42.                
  43.                 Button1.setActionCommand("Agree");
  44.         }
  45.        
  46.         public void paint(Graphics g){
  47.                 g.drawString(actionMessage,10,50);
  48.         }
  49.        
  50.         public void actionPerformed(ActionEvent ae){
  51.                
  52.                 /*
  53.                  * Get the action command using
  54.                  * String getActionCommand() method.
  55.                  */
  56.                
  57.                 String action = ae.getActionCommand();
  58.                 actionMessage = action + " button pressed!";
  59.        
  60.                 repaint();
  61.         }
  62. }

Handle Action Events for AWT Button Example

  1. /*
  2.         Handle Action Events for AWT Button Example
  3.         This java example shows how to handle action event of AWT Button by implementing
  4.         ActionListener interface.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Button;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. /*
  14. <applet code="HandleActionEventExample" width=200 height=200>
  15. </applet>
  16. */
  17.  
  18. public class HandleActionEventExample extends Applet implements ActionListener{
  19.  
  20.         String actionMessage="";
  21.        
  22.         public void init(){
  23.                 //create Buttons
  24.                 Button Button1 = new Button("Ok");
  25.                 Button Button2 = new Button("Cancel");
  26.                
  27.                 //add Buttons
  28.                 add(Button1);
  29.                 add(Button2);
  30.                
  31.                 //set action listeners for buttons
  32.                 Button1.addActionListener(this);
  33.                 Button2.addActionListener(this);
  34.         }
  35.        
  36.         public void paint(Graphics g){
  37.                 g.drawString(actionMessage,10,50);
  38.         }
  39.        
  40.         public void actionPerformed(ActionEvent ae){
  41.                
  42.                 /*
  43.                  * Get the action command using
  44.                  * String getActionCommand() method.
  45.                  */
  46.                
  47.                 String action = ae.getActionCommand();
  48.                
  49.                 if(action.equals("Ok"))
  50.                         actionMessage = "Ok Button Pressed";
  51.                 else if(action.equals("Cancel"))
  52.                         actionMessage = "Cancel Button Pressed";
  53.                
  54.                 repaint();
  55.         }
  56. }
     

Get Selected Radio Button Example

  1. /*
  2.         Get Selected Radio Button Example
  3.         This java example shows how to get selected radio button using
  4.         Java AWT CheckboxGroup class.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Checkbox;
  9. import java.awt.CheckboxGroup;
  10. import java.awt.Graphics;
  11. import java.awt.event.ItemEvent;
  12. import java.awt.event.ItemListener;
  13.  
  14.  
  15. /*
  16. <applet code="GetSelectedRadioButtonExample" width=200 height=200>
  17. </applet>
  18. */
  19.  
  20.  
  21. public class GetSelectedRadioButtonExample extends Applet implementsItemListener{
  22.        
  23.         CheckboxGroup lngGrp = null;
  24.        
  25.         public void init(){
  26.                
  27.                 //create group
  28.                 lngGrp = new CheckboxGroup();
  29.                
  30.                 //create checkboxes and add to group
  31.                 Checkbox java = new Checkbox("Java", lngGrp, true);
  32.                 Checkbox cpp = new Checkbox("C++", lngGrp, false);
  33.                 Checkbox vb = new Checkbox("VB", lngGrp, false);
  34.                
  35.                 //add radio buttons
  36.                 add(java);
  37.                 add(cpp);
  38.                 add(vb);
  39.                
  40.                 //add listeners
  41.                 java.addItemListener(this);
  42.                 cpp.addItemListener(this);
  43.                 vb.addItemListener(this);
  44.         }
  45.        
  46.        
  47.         public void itemStateChanged(ItemEvent ie) {
  48.                 repaint();
  49.         }
  50.        
  51.         public void paint(Graphics g){
  52.                
  53.                 /*
  54.                  * To get selected radio button, use
  55.                  * Checkbox getSelectedCheckbox()
  56.                  * method of CheckboxGroup class.
  57.                  */
  58.                
  59.                 Checkbox chk = lngGrp.getSelectedCheckbox();
  60.                
  61.                 g.drawString(chk.getLabel() + " is selected"10 ,70);
  62.         }
  63. }

Handle Checkbox Event Example

  1. /*
  2.         Handle Checkbox Event Example
  3.         This java example shows how to handle checkbox event. When checkbox
  4.         is selected and deselected, item event is generated.
  5. */
  6.  
  7. import java.applet.Applet;
  8. import java.awt.Checkbox;
  9. import java.awt.Graphics;
  10. import java.awt.event.ItemEvent;
  11. import java.awt.event.ItemListener;
  12.  
  13.  
  14. /*
  15. <applet code="HandleCheckboxEvent" width=200 height=200>
  16. </applet>
  17. */
  18.  
  19. public class HandleCheckboxEvent extends Applet implements ItemListener{
  20.  
  21.         Checkbox java = null;
  22.         Checkbox vb = null;
  23.         Checkbox c = null;
  24.        
  25.         public void init(){
  26.                
  27.                 //create checkboxes
  28.                 java = new Checkbox("Java");
  29.                 vb = new Checkbox("Visual Basic");
  30.                 c = new Checkbox("C");
  31.                
  32.                 add(java);
  33.                 add(vb);
  34.                 add(c);
  35.                
  36.                 //add item listeners
  37.                 java.addItemListener(this);
  38.                 vb.addItemListener(this);
  39.                 c.addItemListener(this);
  40.         }
  41.        
  42.         public void paint(Graphics g){
  43.                
  44.                 g.drawString("Java: " + java.getState(),10,80);
  45.                 g.drawString("VB: " + vb.getState()10100);
  46.                 g.drawString("C: " + c.getState()10120);
  47.                
  48.         }
  49.        
  50.         public void itemStateChanged(ItemEvent ie) {
  51.                 repaint();             
  52.         }
  53. }

Swap Numbers Without Using Third Variable Java Example

  1. /*
  2.         Swap Numbers Without Using Third Variable Java Example
  3.         This Swap Numbers Java Example shows how to
  4.         swap value of two numbers without using third variable using java.
  5. */
  6.  
  7. public class SwapElementsWithoutThirdVariableExample {
  8.  
  9.         public static void main(String[] args) {
  10.                
  11.                 int num1 = 10;
  12.                 int num2 = 20;
  13.                
  14.                 System.out.println("Before Swapping");
  15.                 System.out.println("Value of num1 is :" + num1);
  16.                 System.out.println("Value of num2 is :" +num2);
  17.                
  18.                 //add both the numbers and assign it to first
  19.                 num1 = num1 + num2;
  20.                 num2 = num1 - num2;
  21.                 num1 = num1 - num2;
  22.                
  23.                 System.out.println("Before Swapping");
  24.                 System.out.println("Value of num1 is :" + num1);
  25.                 System.out.println("Value of num2 is :" +num2);
  26.         }
  27.  
  28.  
  29. }
  30.  
  31. /*
  32. Output of Swap Numbers Without Using Third Variable example would be
  33. Before Swapping
  34. Value of num1 is :10
  35. Value of num2 is :20
  36. Before Swapping
  37. Value of num1 is :20
  38. Value of num2 is :10
  39. */