Button widget : Button « Ajax Layer « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » Ajax Layer » Button 
Button widget


http://dynapi.sourceforge.net/
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999

<html>
<head><title>DynAPI Tutor - Button widget</title>
<script language="Javascript" src="./dynapisrc/dynapi.js"></script>
<script language="Javascript">
  dynapi.library.setPath('./dynapisrc/');
  dynapi.library.include('dynapi.api');
</script>
<script language="Javascript">

  //The widget (later we include it as a .js file)
  function Button(x,y,w,h,caption) {
    this.Dynlayer=DynLayer;
    this.Dynlayer(null,x,y,w,h);

    this.caption=caption||'';
    this.onPreCreate(Button.PreCreate);

    this.addEventListener(Button.listener);
  }
  var p = dynapi.setPrototype('Button','DynLayer');
  Button.PreCreate = function() {
    this.setBgColor('#c0c0c0')  // make the widget look gray

    // create child layer for caption
    this.addChild(new DynLayer(null,1,1,this.w-2,this.h-2),'dyncaption');
    this.dyncaption.setHTML('<center>'+this.caption+'</center>');

    // add 3D looking borders at the edges of the widget
    this.BorderL=new DynLayer(null,0,0,1,this.h,'#f0f0f0');
    this.BorderT=new DynLayer(null,0,0,this.w,1,'#f0f0f0');
    this.BorderR=new DynLayer(null,this.w-1,1,1,this.h-1,'#808080');
    this.BorderB=new DynLayer(null,1,this.h-1,this.w-1,1,'#808080');

    this.addChild(this.BorderL);
    this.addChild(this.BorderT);
    this.addChild(this.BorderR);
    this.addChild(this.BorderB);

    this.setVisible(true)// make sure the widget is visible

    // add layer for event handling
    this.dynevents = new DynLayer(null,0,0,this.w,this.h);
    this.addChild(this.dynevents);
  }
  Button.listener = {
    onmousedown : function(e) { // add onmousedown handler
      var o=e.getSource();
      // switch colors to make the button look pressed
      o.BorderL.setBgColor('#808080');
      o.BorderR.setBgColor('#f0f0f0');
      o.BorderT.setBgColor('#808080');
      o.BorderB.setBgColor('#f0f0f0');
    },
    onmouseup : function(e) {
      var o=e.getSource();
      // switch colors to make the button look normal
      o.BorderL.setBgColor('#f0f0f0');
      o.BorderR.setBgColor('#808080');
      o.BorderT.setBgColor('#f0f0f0');
      o.BorderB.setBgColor('#808080');

      o.invokeEvent("pressed")// new line to invoke the onpressed() event
    }
  }

  myListener = {
    onpressed : function(e) {
      var o=e.getSource();
      alert('You hit '+o.caption+'!')
    }
  }

  myButton=new Button(100,100,80,23,'Ok')
  myButton.addEventListener(myListener);

  myButton2=new Button(180,100,80,23,'Cancel')
  myButton2.addEventListener(myListener);

  dynapi.document.addChild(myButton);
  dynapi.document.addChild(myButton2);

</script>
</head>
<body bgcolor=#d0d0d0>
</body>
</html>


           
       
dynapi.zip( 791 k)
Related examples in the same category
1. HTML Button
2. Button control based on layer
w___ww.___java___2__s_._c_om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.