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

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

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 following header files important to C++ pro