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,100, 75,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,100, 75,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,100, 75,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(4, false);  
  13.         l1.setBounds(100,100, 70,70);  
  14.         l1.add("C");  
  15.         l1.add("C++");  
  16.         l1.add("Java");  
  17.         l1.add("PHP");  
  18.         final List l2=new List(4, true);  
  19.         l2.setBounds(100,200, 70,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(400, 400);  
  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(300, 200);  
  22.      }  
  23.   public void paint(Graphics g)  
  24.   {  
  25.     g.setColor(Color.red);  
  26.     g.fillOval(75, 75, 150, 75);  
  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...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...

Genotype Representation

Binary Representation This is one of the simplest and most widely used representation in GAs. In this type of representation the genotype consists of bit strings. For some problems when the solution space consists of Boolean decision variables – yes or no, the binary representation is natural. Take for example the 0/1 Knapsack Problem. If there are n items, we can represent a solution by a binary string of n elements, where the x th  element tells whether the item x is picked (1) or not (0). For other problems, specifically those dealing with numbers, we can represent the numbers with their binary representation. The problem with this kind of encoding is that different bits have different significance and therefore mutation and crossover operators can have undesired consequences. This can be resolved to some extent by using  Gray Coding,  as a change in one bit does not have a massive effect on the solution. Real Valued Representation For problems where we want to define ...