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

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...