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

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

locking

DBMS Locking Part I (DBMS only) TECHNICAL ARTICLES -> PERFORMANCE ARTICLES [  Back  ] [  Next  ] DBMS is often criticized for excessive locking – resulting in poor database performance when sharing data among multiple concurrent processes. Is this criticism justified, or is DBMS being unfairly blamed for application design and implementation shortfalls? To evaluate this question, we need to understand more about DBMS locking protocols. In this article, we examine how, why, what and when DBMS locks and unlocks database resources. Future articles will address how to minimize the impact of database locking. THE NEED FOR LOCKING In an ideal concurrent environment, many processes can simultaneously access data in a DBMS database, each having the appearance that they have exclusive access to the database. In practice, this environment is closely approximated by careful use of locking protocols. Locking is necessary in a concurrent environment to as...

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...