View RSS Feed

All Blog Entries

  1. SWT Button

    by , 11-10-2011 at 05:44 PM (My Java Tips)
    Button is a selectable user interface object that issues notification when pressed and released. It is a widget that is clicked by the user in order to initiate some processing.


    The following are the important constructors of Button class.

    Button(Composite parent, int style)
    Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

    The following are the important methods of Button class. ...
    Categories
    SWT / JFace
  2. SWT Events

    by , 11-10-2011 at 05:41 PM (My Java Tips)
    In order to work with events in SWT, you create a listener specific to the event that you wish to capture. There is an interface for each listener that you can use (for example SelectionListener). There is also a class that will provide you with the event information (for example SelectionEvent). Within the listener that you create, you must implement the methods defined in the interface.



    Java Code:
    SelectionListener listener = new SelectionListener()
    {
    	public
    ...
    Categories
    SWT / JFace
  3. SWT Layout

    by , 11-10-2011 at 05:36 PM (My Java Tips)
    A layout controls the position and size of the children of a composite widget. This class is the abstract base class for layouts.


    Layout class constructor

    Layout()

    Following are details of all the methods of Layout class.

    1. protected abstract Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache)

    Computes and returns the size of the specified composite's client area according to this layout. ...
  4. Eclipse Templates

    by , 11-10-2011 at 05:30 PM (My Java Tips)
    Eclipse Templates are used to make programming easier and simpler. Eclipse IDE provides many templates by default which can be used to save time. Also one can add more templates as required. In this post, I will briefly write about creating and using templates in Eclipse.


    Open the preferences window in Eclipse. Under Java > Editor you will find Templates. Click it and you will see the available templates in the right had side window.Name:  templates.PNG
Views: 284
Size:  21.6 KB
    Making own ...
    Categories
    Java SE
  5. Comparing files

    by , 11-10-2011 at 05:24 PM (My Java Tips)
    While working on projects, sometimes you wish to see the changes you made since last version of the file. This is important to rollback changes or simple to know what new has been added or what has been removed. Eclipse provides a useful feature for this very purpose. In this post, I will write about compare feature.


    Whenever you save a Java file, Eclipse keep a copy of the older one if this feature is enabled. This is required for comparing files in case you need that. To view the ...
    Categories
    Eclipse
  6. Matching Patters using Regex

    by , 11-10-2011 at 05:18 PM (My Java Tips)
    Java developers who are working on text parser or text extractor in Java face problems while search a specific pattern in the text. Searching patterns is not an easy task using standard java string operations. Fortunately we can use regular expressions in java which makes pattern matching easier and simply. Java.util.regex is the package which comes to our rescue.


    One should have a little knowledge of regular expressions to make use of regex. For instance [] is used to give range ...
    Categories
    Java SE
  7. Reading an ASCII file (BufferedReader)

    by , 11-10-2011 at 05:14 PM (My Java Tips)
    Normally we give input to Java applications from command prompt or through GUI. But sometimes, when inputs are in bulk, we can use ASCII files for input. Such files must have some agreed upon pattern so Java parser can understand the meaning of the text. There are many ways to read a file but one of the simplest way is to use BufferedReader class which is found in java.io package.


    First prompt the user to enter the file name with path. This can be done using:

    Java Code:
      
    ...
    Categories
    Java SE
  8. Copying Arrays

    by , 11-10-2011 at 05:10 PM (My Java Tips)
    Arrays are very useful and are commonly used in our Java programs. Array name is actually a pointer that points to the continuous memory location. Many Java developers make some mistakes while copying arrays, which become difficult to cater.


    Consider the following scenario:

    I have a String array called 'array' which have 5 elements in it. I declared another String array called 'array_tmp'. I want to copy the contents of 'array' to 'array_tmp'. One simple way is to do ...
    Tags: arrays, copying Add / Edit Tags
    Categories
    Java SE
  9. Using Vectors in Java

    by , 11-10-2011 at 05:03 PM (My Java Tips)
    Vectors are very useful to store objects for later use. One may define vector as a growable array of objects, so no need to specify the size of Vector while declaring it.


    Vector stores objects which can even be an object of user made class. For example: we want to store 2 string objects in a vector. Then we want to store an object of user made class TestClass also in the same vector. This is possible.

    Vectors belong to java.util package so import it before using. While ...
    Categories
    Java SE
  10. Fetching HTML content of a Web Page

    by , 11-10-2011 at 04:46 PM (My Java Tips)
    Sometimes you are required to fetch and store data from web pages. If there are too many pages to parse, then obviously this cannot be done manually. Java provides support for web text extraction.


    The approach is simple. You have to fetch all the HTML contents of a webpage and then you can write your own parser to extract the required info. For example: you might be asked to only store the text in table data tag with caption Hobbies. So you will store all the HTML contents of web ...
  11. Saving objects to file

    by , 11-10-2011 at 04:39 PM (My Java Tips)
    Sometimes it is useful to store objects in files which can be read later. Object is saved along with its state. Simply saying, an object with its data can be saved in a file. Objects are made persistent this way.


    The class whose object is to be saved should implements Serializable interface. We as a programmer are not supposed to add any method. The purpose of implementing interface is to tell the Java run time, which classes are to be serialized.

    Serializable interface ...
  12. Retrieving serialized objects from file

    by , 11-10-2011 at 04:35 PM (My Java Tips)
    I assume that you are aware of how to serialize objects and save them into files on the disk. Now I will talk about how to retrieve serialized objects.


    You have to use FileInputStream and ObjectInputStream to fetch the object. Specify the file which contains the object in FileInputStream. Once ObjectInputStrem is initialized, just use in.readObject method of ObjectInputStream to fetch the object.

    In the following example, I am fetching a Vector from a file on disk. ...
  13. Using Interfaces

    by , 11-10-2011 at 04:32 PM (My Java Tips)
    Interfaces are used to define a contract for the classes. It has its own importance when it comes to defining standards.


    Consider the following example:

    Cars have some standard and their basic operations are the same. Each car should have operations like startCar, moveCar, stopCar etc. Toyota, Mazda and Honda are cars and each has its own way of starting, moving and stoping. So each has to have its own startCar, moveCar and stopCar method.

    How to enforce ...
  14. Serializing Arrays

    by , 11-10-2011 at 04:08 PM (My Java Tips)
    We already know that we can serialize object of classes that implement serializable interface. Make a class that implements serializable interface, make an object of it and save it to disk. You may retrieve to confirm that the object has the required data or not. Vector class also implements serializable interface, so you can also store vectors on disk.


    How about Arrays?

    Arrays are of some type. Thing to note is which classes implement Serializable interface. Integer, ...
    Categories
    Java SE
  15. Parsing XML Documents-1 (DOM and SAX)

    by , 11-10-2011 at 04:03 PM (My Java Tips)
    XML files are very useful. Usually they are used to store configuration settings. We can also use XML files to store data so it can be used as a database. A lot of work has been done in this domain and its really very useful working with XML files.



    Now suppose you have a XML file with some useful data.

    college.xml
    XML Code:
    <college>
    <student>
      <first>Shoaib</first>
      <last>Malik</last>
    ...
    Tags: dom parser, sax, xml Add / Edit Tags
    Categories
    Java SE
  16. Programming in an efficient way

    by , 11-09-2011 at 06:12 PM (My Java Tips)
    I have seen even matured programmers making few mistakes while programming, which might have performance bottleneck. I would like to mention few of such mistakes here.


    A function call takes some resources. Even it seems a simple line of code, Java Runtime has to take case of few things to manage it. Consider following example:

    Java Code:
    String str = "Lets play football. If you are interested, let me know.";
    for (int i=0; i < str.length();i++)
    ...
    Categories
    Java SE
  17. Defining own Exceptions

    by , 11-09-2011 at 06:10 PM (My Java Tips)
    Exception is defined as:
    "An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions."

    This event (exception generation event) can be specific to your requirement as well. Defining own exception makes sense if you want to generate exceptions specific to your logic (business logic). .


    Consider following scenario:

    You have an environment where student id cannot be ...
    Categories
    Java SE
  18. Varargs – Java 5.0 addition

    by , 11-09-2011 at 06:08 PM (My Java Tips)
    We all are familiar with methods in lava. Methods are an important artifact of programming. Method contains signature and body. Method body is enclosed in braces. Method signature comprises of its name, access modifier, return type and list of parameters.



    Java Code:
    public String methodName(String arg0,int arg1)
    {
    …
    return string;
    }
    Methods can also be static which means they can be accessed without using any object.
    ...
    Categories
    Java SE
  19. Using Reflection to run methods

    by , 11-09-2011 at 05:59 PM (My Java Tips)
    Reflection is used when we want examine or modify the runtime behavior of applications running in the Java virtual machine. Java’s Reflection API provides very useful functionality for programmers. Here we will go through an example to understand what can be done with Reflection.

    Consider the following class called Countries:


    Java Code:
    public class Countries {
    
    String name;
    String capital;
    
    public void setName(String name) {
    ...
    Categories
    Java SE
  20. using Packages

    by , 11-09-2011 at 05:57 PM (My Java Tips)
    Packages are used to group related classes, interfaces and enumerations together. Each package has its own namespace and provide access protection and name space management.


    Using packages in your projects is a good programming approach. It makes more sense to use packages if you are working in a team on some big project.

    Suppose you write a group of classes that represent vehicle objects like bus, truck and car. You also have interfaces called Drive and Stop and each ...
    Categories
    Java SE
  21. Using enum types (with example)

    by , 11-09-2011 at 05:55 PM (My Java Tips)
    Sometimes it is useful to have a set of values for a variable. Examples can be days of week, planets in the solar system, months of a year, grades of a course, directions etc. This control is useful specially when you are developing a software in a team. So you define boundaries of that type and others have to follow those rules.


    In such scenarios, we use enum types. An enum type implicitly extend java.lang.Enum. Its declaration syntax is easy:

    Java Code:
    public enum Day
    ...
    Categories
    Java SE
  22. Inheritance by example

    by , 11-09-2011 at 05:53 PM (My Java Tips)
    I have fond many developers who very seldom use inheritance while coding in Java. The reason they give is why one should make code difficult to understand? I am amazed at this reason because Java inheritance is similar to natural inheritance.


    Lets fist understand the basics.

    Mazda is a Car > Mazda inherits Car
    Java is a programming language > Java inherits Prog. Language
    Australia is a country > Australia inherits Country

    Whenever ...
    Categories
    Java SE
  23. Method shadowing (Inheritance)

    by , 11-09-2011 at 05:50 PM (My Java Tips)
    Method shadowing is a term used for situations, when child and parent classes have methods with exactly same signature. This situation can create problems that sometimes become difficult to understand.



    Method shadowing can be done on purpose or by mistake.

    Consider the example below:

    Java Code:
    public class Car {
    
         	public void startCar()
    	{
    		System.out.println("Car class - Car started");
    	}
    ...
    Categories
    Java SE
  24. Parsing XML documents using DOM

    by , 11-09-2011 at 05:48 PM (My Java Tips)
    Parsing XML documents using DOM is easy and simple. Let do this with example.
    DocumentBuilder is used to obtain Document from XML file. It is an abstract class. Once we have reference of DocumentBuilder, we will create Document instance. Document class instance is created using parse method of DocumentBuilder class which takes xml file as parameter. By doing so, we will have XML document in the memory.


    We have following XML file to parse:
    XML Code:
     <?xml version="1.0"?>
    ...

    Updated 11-09-2011 at 06:06 PM by Java Tip

    Categories
    XML
  25. Introduction to Hashtable

    by , 11-09-2011 at 05:46 PM (My Java Tips)
    Class Hashtable is found in java.util package and is very useful data structure. If used sensibly, it can save time and can produce results efficiently. Hashtable class implements Cloneable, Map and Serializable interfaces.


    Hashtable can be simply a table in the memory. Each value stored as a key associated to it. Values are retrieved also using keys so searching is very fast.. It is always a good idea to declare Hashtable of size much greater than what you need for faster processing/lookups. ...
    Categories
    Java SE
  26. Method parameters bounded by a Class

    by , 11-09-2011 at 05:45 PM (My Java Tips)
    Java Generics are introduced in Java 5.0 and have introduced new exciting ways to code. In this post, I will show you how you can use generic types for method parameters.


    Lets take an example: You have Integer, Float, BigInteger and Double values and want to compare them using user made functions.
    You have to write separate functions for comparing Integer with BigInteger, Integer with Double, Integer with Float and so on. But there is an easy way to it. Simply use generics. ...
    Categories
    Java SE
  27. Creating JavaDocs in Eclipse

    by , 11-09-2011 at 05:42 PM (My Java Tips)
    Java Docs are of great importance for Java programmers. Every Java programmer uses it to know which functions are to be used to achieve the results.
    Java Docs provide useful information including:
    - Which classes exist in a package
    - Purpose of each class
    - Constructors in a class
    - Methods in a class
    - Method parameters and return types
    - Deprecated methods
    - Exceptions that a method raises
    - Version and Author of the class
    - ...
    Categories
    Eclipse
  28. Javadoc tags

    by , 11-09-2011 at 05:40 PM (My Java Tips)
    JavaDoc comments start with a forward slash and two asterisks (/**) and ends with one asterisk and a forward slash (*/). Everything between the delimiters (even if it spans multiple lines) is a comment.



    /**
    * @author Gapper
    * @version 1.0
    */

    One has to use javadoc tags. The following are special tags in Javadoc that have predefined purposes.

    @see
    It is used to create links to other javadoc documentation.
    ...
    Categories
    Java SE
  29. Creating JavaDocs – An Example

    by , 11-09-2011 at 05:33 PM (My Java Tips)
    You are developing a software in a team and you are assigned to work on a particular package. You worked on it and submitted it after completion. The development team has to use the classes and methods written by you. To use them, they have to know a lot of things.


    For a class, they should know:
    - Constructors of class
    - Is class a static?
    Etc

    For a method, they should know:
    - Is method static?
    - Return type of method.
    - Parameter ...
    Categories
    Eclipse
  30. Socket Programming (Server)

    by , 11-09-2011 at 05:14 PM (My Java Tips)
    Socket programming is easy and fun. In this post, I will create a simple Socket Server that will listen on a post for client requests. It will get data from the client and will send the acknowledgment.


    First step is to declare a server socket, a client socket and input/output streams. I assume that you are familiar with streams. Then we have to open a connection on a port. Usually it is not a wise decision to use port number that is less than 1023 because ports less than 1023 are ...

    Updated 11-09-2011 at 05:28 PM by Java Tip

    Categories
    Network Programming
Page 39 of 48 FirstFirst ... 293738394041 ... LastLast