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

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

C++ (Object and Class)

The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language. Object Oriented Programming is a paradigm that provides many concepts such as  inheritance, data binding, polymorphism etc. The programming paradigm where everything is represented as an object is known as truly object-oriented programming language.  Smalltalk  is considered as the first truly object-oriented programming language. OOPs (Object Oriented Programming System) Object  means a real word entity such as pen, chair, table etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation C++ Object In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an ent...