Tuesday, November 30, 2010

Java swing event handling example programs for beginners

Java uses delegation event model approach to handle events.

What is delegation event model?
Concept is quite simple:a source generate an event and sends it to one or ore listeners.In this scheme, the listener waits until it receive an event. Once an event is received, the listener process the event and and then returns.

Here are short definitions of event, source, listeners.

Event :
An event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface.

Monday, November 22, 2010

How to create a menu bar and add a menu separator in java swing with eclipse

As we know java swing provides more liveliness to a web page, we can create menu bar using simple steps in java swing, which helps user to do his/her operations quickly.With out menu the task will be more tedious and very complex, so every one needs to menu bar  in web page. Now lets start with a simple swing program, as by program you can get it more quickly and easily.
To start a Java  program in Eclipse see here

//Java swing menu bar program starts

Java swing tutorial

In short swing is a widget toolkit for Java. It is part of Sun Microsystems' Java Foundation Classes (JFC) -- an API for providing a graphical user.

Multithreading in java examples

Multithreading computers have hardware support to efficiently execute multiple threads. These are distinguished from multiprocessing systems.


Sunday, November 21, 2010

java Applet example tutorial for beginners

Applet is a Java application; an application program that uses the client's web browser to provide a user interface.

Packages in java

Packages in java  is a mechanism for organizing Java classes into namespaces similar to the modules of Modula

Thursday, November 18, 2010

How to create a swing applet and application in eclipse

What are applets in java?
Applets are not  stand-alone programs.They run within either a web browser or applet viewer. Applet is included in html page using <applet>. See a applet example here.

What is swing in java?
 swing can be used as stand-alone and also as a applet.
Lets start a swing applet.Before that just have a look at simple swing stand-alone program.
Now start Eclipse IDE. See here how to start an eclipse 
A swing applet program
/*

Monday, November 15, 2010

Java swing tutorial examples for beginners with eclipse

About Java swing
 
Java introduced swing concept to give a response to deficiency present in java awt. The AWT defines the basic set of controls, windows, and dialog boxes that support usable, but limited graphical interface.

As AWT uses native resources they are called as heavy weight components, and swing are light weight components. Presently most of programmers prefer swing to awt.

This doesn`t mean that AWT are replaced by swing.To become a swing programmer you have to learn AWT has many of awt components are used in swings.

In this tutorial you will learn both awt and swing simultaneously.
Now, lets start with basic one. How to write a swing?

Thursday, November 11, 2010

How to open notepad using java program

It is very simple to open a note pad using your java program..

And this is achieved by a class named ProcessBuilder ,
All processes are represented Process class and ProcessBuilder offers more control more over the processes.

ProcessBuilder defines three constructors.

ProcessBuilder(List<String> args)

ProcessBuilder(String.....arg)

Saturday, November 6, 2010

How to compile java program in Eclipse IDE

We all know Eclipse is an IDE (integrated development environment) for Java.We have many IDE but I prefer Eclipse.

Start with First Download Eclipse  and J2SE (JDK kit).(Download ECLIPSE) and (Download Java).

You will get Eclipse in Winrar file extract it.And then double click on eclipse.exe icon to start.

How to start a java program?
After installing Eclipse,a window will open.
Go to File--->New-->(Click)Java project.

Wednesday, November 3, 2010

How to set thread priority in java - tutorial with examples

As an application program may have many Threads ,to run all threads at a time,Thread scheduler is run.Though in theory,higher priority threads get more CPU time than lower priority threads,but in practice CPU time may vary from one operating system to other,on how OS is implementing multitask.

   As a Java programmer you need to be very careful while handling threads,as java is designed to work on different environments of OS,programmer  should be see that all threads will get a chance to run.
This can be achieved by handling threads using Thread priorities...
setting priority :

Friday, October 29, 2010

What is Multithreading in java examples

Multithreading,as the name itself tells that it is regarding,multi tasks(thread).In short Multithreading is a specialized form of multi tasking.
what is a thread?
Threads are separate parts of execution which are functionally independent of each other.

What is multitasking?
Single user multi process processed by a single processor.
Now what is a thread in java ?
In simple terms, a thread is a program's path of execution.Programs written without multithreading  run as a single thread, causing problems when multiple events or actions need to occur at the same time.
For instance, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. 
How to create a thread in java ?

Monday, October 25, 2010

Java applet tag parameters turtorials for beginners

 Pram tag in applet is used to pass parameter from applet tag to program..(See basic applet)
import java.awt.*;                                //importing awt,applet packages
import java.applet.*;
/*
<applet code="AppleParam" width="200" height="300">          
<param name=fontname value=Applet>                                       //passing parameter
<param name=fontsize value=>
</applet>
*/

Saturday, October 23, 2010

How to make a java applet banner

As discussed Applet is small application accessed over internet.
Applet is a class in java,using extends keyword. (see how to extend Applet)
Applets can used to develop banner on your web page.
Lets see it with an example....
//Java Applet banner program
import java.awt.*;                                      //importing applet and awt package
import java.applet.*;
/*
<applet code="AppletBanner" width=300 height=50>
</applet>
*/
public class AppletBanner extends Applet implements Runnable{  //Multithreading is used

Thursday, October 21, 2010

Introducing Java applet tutorial with examples

What is Applet?
Applet is a small application that are accessed on an internet server,transported over the internet,automatically installed and run as part of a web document.
Security by applet:
After an applet arrives on the client,it has limited access to resources so thatit can produce a GUI and run complex computation without introducing the risk of viruses or breaching data integrity.
Starting Applet
There are two varieties of Applets.One that uses AWT to provide GUI,and second that uses SWINGS for GUI.
   All applets are subclasses of Applet.Applet is not standalone it runs on web browser or on Appletviewer.  (See ROBOT Class in java)
appletviewer is provided by JDK.

java program to move mouse

A sample application which makes your mouse to move automatically randomly on screen with high speed.

Robot Mouse code:

//Program starts here 
import java.awt.Robot;
public class MouseControl
{
public static void main(String[] hj)
{
   try
    {
       //to move mouse with more speed use this for loop

how to use robot class in java

/*Here is another Robot java code for you,with explanation....
 this program when you run it writes something for you...mean this program will communicate with you!
  Just kidding.
About Robot:
Robot is a class in Java, where it controls mouse, keyboard with the help of its methods.
Lets start with example...*/
 //Program starts here

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotExp {

 public static void main(String[] args){

//code to open a notepad
  try{

 ProcessBuilder proc=new ProcessBuilder("notepad.exe","AM-ROBOT");
  proc.start();
   }
  catch(Exception k){
  System.out.println(k);
  }

//code to write message
 try {
  Robot robot = new Robot();
  robot.delay(5000); 
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_I);  
  robot.keyPress(KeyEvent.VK_A);
  robot.keyPress(KeyEvent.VK_M);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_R);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_B);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_A);
  robot.keyPress(KeyEvent.VK_L);
  robot.keyPress(KeyEvent.VK_K);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_T); 
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_M);
  robot.keyPress(KeyEvent.VK_E);
  } 
  catch (AWTException e) {
                 e.printStackTrace();
   }
  }
}
//program completed 


Now compile and run the program.See how compile java in Eclipse  


For better result Just click yes when a notepad opens..

Explanation:


 It is very simple all you have to know is Robot class and its methods.
here in    robot.keyPress(KeyEvent.VK_O);
robot is object created in above program,KeyPress() is a method,and keyEvent is parameter.
VK is virtual key and O is character to be displayed.
you can write any message.With SPACE for gap.


NOTE:

      USE ONLY Single character and Capital,Or else you will get compile time error as compiler  will not understand it.

SEE ROBOT MOUSE which moves mouse pointer

Tags----      Packages in Java     ,       Access specifier      ,     Java history

Sunday, October 17, 2010

How to import packages

Importing packages

,simply include  import command in the program you want to import, that`s all you can use all those features in the package.
Not clear!
Lets see an example it will be cleared..(am giving very simple programs as example so that it will be cleared)
example:
 import Mypack1.*;                 //see from where this Mypack1 came.
class Package extends Pack         
//here Pack is name of class I used  in Mypack1
{       
public static void main(String s[])

{
  Pack p=new Pack();     //here creating object for that class
  int k=90;
  p.show();                          //access it through dot operator
 }
}

What are Access specifiers in java with examples

Here is a table showing the effects of

access specifiers

for class members. “
Y---yes
blank---no

Specifierclasssubclasspackageworld
privateY


protectedYYY
publicYYYY
(none)Y
Y

If a variable is declared protected, then the class itself can access it, its subclass can access it, and any class in the same package can also access it, but otherwise a class cannot access it.
If a class memeber doesn't have any access specifier (the “none” row in above), its access level is sometimes known as “package”.
Here's a example.

Java package tutorial for beginners

Packages in Java

,are containers for classes that are used to keep the class name space compartmentalized.For example,a package allows you to create a class named Xyz (any name given by you),which you can store in your own package with out any concern that it will collide with other packages.

Define Package:
                              Package is, a collection of .class files (know what is .class file),created by the user or predefined.

How to create a package:
                                      It is very simple, simply include package command as the first statement in a Java source file.Any classes declared in that file will belong to the specified package.
  

Friday, October 15, 2010

How to start with java programming

A simple program on Java

,that displays a  message.
                                                                             
class FirstProgram
{
     public static void main(String args[])    //main method
     {
          System.out.println(“Welcome to Java 4r beginners”); /* will be printed on output*/
          System.out.println(“This Simple Java program”)
      }
}
 
/*



now save this program with “FirstProgram.java” as class name is
FirstProgram.
-Program should be saved with its class name.

About java language

Java

is OOPS language, it supports OOPS features… go2 first page
Encapsulation is the mechanism that binds the code and the data it manipulates, keeps safe from both out side inheritance and misuse.
  • Inheritance
Inheritance is the process by which one object`s properties are acquired and used by other object. In short code re-usability. It is achieved by extends keyword in Java.
  • Polymorphism
In short Polymorphism mean many forms, one interface will be used for many tasks.This will be explained detailed with example.
…These are few among them..

Java program starts with class and main method will be included in the class.
Unlike C,C++ in java main method will pass String.Lets go to an example.

Thursday, October 14, 2010

Know what is history of java

Java

, as all know it`s a programming language developed by James Gosling and his team.
Java is still a relatively new language, so it is quite amusing to some to think that Java has a "history" behind it. One of the most frequent questions I've been getting lately though is about the origins of Java.

The first publicly available version of Java, however, was as Java applets, in the original HotJava browser. From there, Java grew to what it is today.