Skip to main content

JAVA AWT: Choice, List & Canvas

Java AWT Choice

The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits Component class.

AWT Choice Class Declaration

  1. public class Choice extends Component implements ItemSelectable, Accessible  

Java AWT Choice Example

  1. import java.awt.*;  
  2. public class ChoiceExample  
  3. {  
  4.         ChoiceExample(){  
  5.         Frame f= new Frame();  
  6.         Choice c=new Choice();  
  7.         c.setBounds(100,10075,75);  
  8.         c.add("Item 1");  
  9.         c.add("Item 2");  
  10.         c.add("Item 3");  
  11.         c.add("Item 4");  
  12.         c.add("Item 5");  
  13.         f.add(c);  
  14.         f.setSize(400,400);  
  15.         f.setLayout(null);  
  16.         f.setVisible(true);  
  17.      }  
  18. public static void main(String args[])  
  19. {  
  20.    new ChoiceExample();  
  21. }  
  22. }  

Output:

java awt choice example 1

Java AWT Choice Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class ChoiceExample  
  4. {  
  5.         ChoiceExample(){  
  6.         Frame f= new Frame();  
  7.         final Label label = new Label();          
  8.         label.setAlignment(Label.CENTER);  
  9.         label.setSize(400,100);  
  10.         Button b=new Button("Show");  
  11.         b.setBounds(200,100,50,20);  
  12.         final Choice c=new Choice();  
  13.         c.setBounds(100,10075,75);  
  14.         c.add("C");  
  15.         c.add("C++");  
  16.         c.add("Java");  
  17.         c.add("PHP");  
  18.         c.add("Android");  
  19.         f.add(c);f.add(label); f.add(b);  
  20.         f.setSize(400,400);  
  21.         f.setLayout(null);  
  22.         f.setVisible(true);  
  23.         b.addActionListener(new ActionListener() {  
  24.             public void actionPerformed(ActionEvent e) {       
  25.          String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());  
  26.          label.setText(data);  
  27.         }  
  28.         });           
  29.         }  
  30. public static void main(String args[])  
  31. {  
  32.    new ChoiceExample();  
  33. }  
  34. }  

Java AWT List

The object of List class represents a list of text items. By the help of list, user can choose either one item or multiple items. It inherits Component class.

AWT List class Declaration

  1. public class List extends Component implements ItemSelectable, Accessible  

Java AWT List Example

  1. import java.awt.*;  
  2. public class ListExample  
  3. {  
  4.      ListExample(){  
  5.         Frame f= new Frame();  
  6.         List l1=new List(5);  
  7.         l1.setBounds(100,10075,75);  
  8.         l1.add("Item 1");  
  9.         l1.add("Item 2");  
  10.         l1.add("Item 3");  
  11.         l1.add("Item 4");  
  12.         l1.add("Item 5");  
  13.         f.add(l1);  
  14.         f.setSize(400,400);  
  15.         f.setLayout(null);  
  16.         f.setVisible(true);  
  17.      }  
  18. public static void main(String args[])  
  19. {  
  20.    new ListExample();  
  21. }  
  22. }  

Output:

java awt list example 1

Java AWT List Example with ActionListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class ListExample  
  4. {  
  5.      ListExample(){  
  6.         Frame f= new Frame();  
  7.         final Label label = new Label();          
  8.         label.setAlignment(Label.CENTER);  
  9.         label.setSize(500,100);  
  10.         Button b=new Button("Show");  
  11.         b.setBounds(200,150,80,30);  
  12.         final List l1=new List(4false);  
  13.         l1.setBounds(100,10070,70);  
  14.         l1.add("C");  
  15.         l1.add("C++");  
  16.         l1.add("Java");  
  17.         l1.add("PHP");  
  18.         final List l2=new List(4true);  
  19.         l2.setBounds(100,20070,70);  
  20.         l2.add("Turbo C++");  
  21.         l2.add("Spring");  
  22.         l2.add("Hibernate");  
  23.         l2.add("CodeIgniter");  
  24.         f.add(l1); f.add(l2); f.add(label); f.add(b);  
  25.         f.setSize(450,450);  
  26.         f.setLayout(null);  
  27.         f.setVisible(true);  
  28.         b.addActionListener(new ActionListener() {  
  29.          public void actionPerformed(ActionEvent e) {       
  30.           String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());  
  31.           data += ", Framework Selected:";  
  32.           for(String frame:l2.getSelectedItems()){  
  33.                    data += frame + " ";  
  34.           }  
  35.           label.setText(data);  
  36.           }  
  37.          });          
  38. }  
  39. public static void main(String args[])  
  40. {  
  41.    new ListExample();  
  42. }  
  43. }  

Output:

java awt list example 2

Java AWT Canvas

The Canvas control represents a blank rectangular area where the application can draw or trap input events from the user. It inherits the Component class.

AWT Canvas class Declaration

  1. public class Canvas extends Component implements Accessible  

Java AWT Canvas Example

  1. import java.awt.*;  
  2. public class CanvasExample  
  3. {  
  4.   public CanvasExample()  
  5.   {  
  6.     Frame f= new Frame("Canvas Example");  
  7.     f.add(new MyCanvas());  
  8.     f.setLayout(null);  
  9.     f.setSize(400400);  
  10.     f.setVisible(true);  
  11.   }  
  12.   public static void main(String args[])  
  13.   {  
  14.     new CanvasExample();  
  15.   }  
  16. }  
  17. class MyCanvas extends Canvas  
  18. {  
  19.         public MyCanvas() {  
  20.         setBackground (Color.GRAY);  
  21.         setSize(300200);  
  22.      }  
  23.   public void paint(Graphics g)  
  24.   {  
  25.     g.setColor(Color.red);  
  26.     g.fillOval(757515075);  
  27.   }  
  28. }    
Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);               f.setSize( 400 , 400 );               f.setLayout( null );               f.setVisible( true );   }   public   static   void  main(String args[]){           new  ScrollbarExample();   }   }   Output: Java AWT Scrollbar Example with AdjustmentListener import  java.awt.*;   import  java.awt.event.*;   class  ScrollbarExample{        ScrollbarExample(){               Frame f=  new  Frame( "Scro

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari

C++ this Pointer, static, struct and Enumeration

C++ this Pointer In C++ programming,  this  is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used  to pass current object as a parameter to another method. It can be used  to refer current class instance variable. It can be used  to declare indexers. C++ this Pointer Example Let's see the example of this keyword in C++ that refers to the fields of current class. #include <iostream>    using   namespace  std;   class  Employee {       public :           int  id;  //data member (also instance variable)               string name;  //data member(also instance variable)            float  salary;          Employee( int  id, string name,  float  salary)             {                   this ->id = id;                  this ->name = name;                  this ->salary = salary;            }             void  display()             {                 cout<<id<< "  " <<name<&