Skip to main content

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

  1. public class Scrollbar extends Component implements Adjustable, Accessible  

Java AWT Scrollbar Example

  1. import java.awt.*;  
  2. class ScrollbarExample{  
  3. ScrollbarExample(){  
  4.             Frame f= new Frame("Scrollbar Example");  
  5.             Scrollbar s=new Scrollbar();  
  6.             s.setBounds(100,10050,100);  
  7.             f.add(s);  
  8.             f.setSize(400,400);  
  9.             f.setLayout(null);  
  10.             f.setVisible(true);  
  11. }  
  12. public static void main(String args[]){  
  13.        new ScrollbarExample();  
  14. }  
  15. }  

Output:

java awt scrollbar example 1

Java AWT Scrollbar Example with AdjustmentListener

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. class ScrollbarExample{  
  4.      ScrollbarExample(){  
  5.             Frame f= new Frame("Scrollbar Example");  
  6.             final Label label = new Label();          
  7.             label.setAlignment(Label.CENTER);  
  8.             label.setSize(400,100);  
  9.             final Scrollbar s=new Scrollbar();  
  10.             s.setBounds(100,10050,100);  
  11.             f.add(s);f.add(label);   
  12.             f.setSize(400,400);  
  13.             f.setLayout(null);  
  14.             f.setVisible(true);  
  15.             s.addAdjustmentListener(new AdjustmentListener() {  
  16.                 public void adjustmentValueChanged(AdjustmentEvent e) {  
  17.                    label.setText("Vertical Scrollbar value is:"+ s.getValue());  
  18.                 }  
  19.             });  
  20.          }  
  21. public static void main(String args[]){  
  22. new ScrollbarExample();  
  23. }  
  24. }  

Output:

java awt scrollbar example 2

Java AWT MenuItem and Menu

The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass.

The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.

AWT MenuItem class declaration

  1. public class MenuItem extends MenuComponent implements Accessible  

AWT Menu class declaration

  1. public class Menu extends MenuItem implements MenuContainer, Accessible  

Java AWT MenuItem and Menu Example

  1. import java.awt.*;  
  2. class MenuExample  
  3. {  
  4.      MenuExample(){  
  5.          Frame f= new Frame("Menu and MenuItem Example");  
  6.          MenuBar mb=new MenuBar();  
  7.          Menu menu=new Menu("Menu");  
  8.          Menu submenu=new Menu("Sub Menu");  
  9.          MenuItem i1=new MenuItem("Item 1");  
  10.          MenuItem i2=new MenuItem("Item 2");  
  11.          MenuItem i3=new MenuItem("Item 3");  
  12.          MenuItem i4=new MenuItem("Item 4");  
  13.          MenuItem i5=new MenuItem("Item 5");  
  14.          menu.add(i1);  
  15.          menu.add(i2);  
  16.          menu.add(i3);  
  17.          submenu.add(i4);  
  18.          submenu.add(i5);  
  19.          menu.add(submenu);  
  20.          mb.add(menu);  
  21.          f.setMenuBar(mb);  
  22.          f.setSize(400,400);  
  23.          f.setLayout(null);  
  24.          f.setVisible(true);  
  25. }  
  26. public static void main(String args[])  
  27. {  
  28. new MenuExample();  
  29. }  
  30. }  

Output:

java awt menuitem and menu example 1

ava AWT PopupMenu

PopupMenu can be dynamically popped up at specific position within a component. It inherits the Menu class.

AWT PopupMenu class declaration

  1. public class PopupMenu extends Menu implements MenuContainer, Accessible  

Java AWT PopupMenu Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. class PopupMenuExample  
  4. {  
  5.      PopupMenuExample(){  
  6.          final Frame f= new Frame("PopupMenu Example");  
  7.          final PopupMenu popupmenu = new PopupMenu("Edit");   
  8.          MenuItem cut = new MenuItem("Cut");  
  9.          cut.setActionCommand("Cut");  
  10.          MenuItem copy = new MenuItem("Copy");  
  11.          copy.setActionCommand("Copy");  
  12.          MenuItem paste = new MenuItem("Paste");  
  13.          paste.setActionCommand("Paste");      
  14.          popupmenu.add(cut);  
  15.          popupmenu.add(copy);  
  16.          popupmenu.add(paste);        
  17.          f.addMouseListener(new MouseAdapter() {  
  18.             public void mouseClicked(MouseEvent e) {              
  19.                 popupmenu.show(f , e.getX(), e.getY());  
  20.             }                 
  21.          });  
  22.          f.add(popupmenu);   
  23.          f.setSize(400,400);  
  24.          f.setLayout(null);  
  25.          f.setVisible(true);  
  26.      }  
  27. public static void main(String args[])  
  28. {  
  29.         new PopupMenuExample();  
  30. }  
  31. }  

Output:

java awt popup example 1 java awt popup example 2
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...

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