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,100, 50,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,100, 50,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

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

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

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