Differentiate between Choice class and List class in AWT with suitable description and example.

  Choice Class

Choice control is used to show the popup menu of choices. The selected choice is shown at the top of the menu. Thus, a Choice control is a form of a menu. When inactive, a Choice component takes up only enough space to show the currently selected item. When the user clicks on it, the whole list of choices pops up, and a new selection can be made.

                          or,

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


Commonly used Constructors

Choice(); //Creates a new choice menu.


AWT Choice Class Declaration

public class Choice extends Component implements ItemSelectable, Accessible  


Commonly used Public Methods

void add(String item); //Adds an item to this Choice menu. 

String getItem(int index)://Gets the string at the specified index in this Choice menu.

int getItemCount();//Returns the number of items in this Choice menu.

int getSelectedIndex(); //Returns the index of the currently selected item. 

String getSelectedItem(); //Gets a representation of the current choice as a string.

void remove(int position);//Removes an item from the choice menu at the specified position.

void removeAll()://Removes all items from the choice menu.

void select(int pos)://Sets the selected item in this Choice menu to be the item at the specified position.

void select(String str); //Sets the selected item in this Choice menu to be the item whose name is equal to the specified string.


Java AWT Choice Example

In the following example, we are creating a choice menu using Choice() constructor. Then we add 5 items to the menu using add() method and Then add the choice menu into the Frame.

ChoiceExample1.java

// importing awt class  

import java.awt.*;   

public class ChoiceExample1 {    

         // class constructor  

        ChoiceExample1() {    

        // creating a frame  

        Frame f = new Frame();    

        // creating a choice component  

        Choice c = new Choice();   

        // setting the bounds of choice menu   

        c.setBounds(100, 100, 75, 75);    

        // adding items to the choice menu  

        c.add("Item 1");    

        c.add("Item 2");    

        c.add("Item 3");    

        c.add("Item 4");    

        c.add("Item 5");    

        // adding choice menu to frame  

        f.add(c);    

        // setting size, layout and visibility of frame  

        f.setSize(400, 400);    

        f.setLayout(null);    

        f.setVisible(true);    

     }    

// main method  

public static void main(String args[])    

{    

   new ChoiceExample1();    

}    

}     



List Class
The List represents a list of text items. The list can be configured to that user can choose either one item or multiple items. Unlike the Choice object, which shows only the single selected item in the menu, a List object can be constructed to show any number of choices in the visible window.
              or,
The object of the List class represents a list of text items. With the help of the List class, user can choose either one item or multiple items. It inherits the Component class.

AWT List class Declaration
public class List extends Component implements ItemSelectable, Accessible  

Commonly used Constructors
List(); //Creates a new scrolling list.
List(int rows); //Creates a new scrolling list initialized with the specified number of visible lines.
List(int rows, boolean multipleMode); //Creates a new scrolling list initialized to display the specified number of rows.

Commonly used Public Methods
void add(String item); //Adds the specified item to the end of the scrolling list.
void add(String item, int index); //Adds the specified item to the scrolling list at the position indicated by the index.
void deselect(int index); //Deselects the item at the specified index.
String getItem(int index); //Gets the item associated with the specified index.
int getItemCount(); //Gets the number of items in the list.
String[] getItems(); //Gets the items in the list.

Java AWT List Example
In the following example, we are creating a List component with 5 rows and adding it into the Frame.

ListExample1.java
// importing awt class  
import java.awt.*;    
  
public class ListExample1  
{    
    // class constructor   
     ListExample1() {    
     // creating the frame  
        Frame f = new Frame();   
       // creating the list of 5 rows   
        List l1 = new List(5);   
  
        // setting the position of list component   
        l1.setBounds(100, 100, 75, 75);    
  
        // adding list items into the list  
        l1.add("Item 1");    
        l1.add("Item 2");    
        l1.add("Item 3");    
        l1.add("Item 4");    
        l1.add("Item 5");    
  
        // adding the list to frame  
        f.add(l1);   
  
        // setting size, layout and visibility of frame  
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
  
// main method  
public static void main(String args[])    
{    
   new ListExample1();    
}    
}    


Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?