Add to Google Reader or Homepage

JTextArea - An Example

JTextArea


Before discussing TextArea let us know the difference between TextField and TextArea,
The difference between these two components,are as follows.

 "A TextField is an input box where we can only enter single line of text whereas a TextArea allows you to enter multiple line of texts".

 " If the text is more than the display area of the Field a TextField will never grow but the TextArea will grow".

Creating a TextArea


A TextArea can be created by using the  JTextArea class of the Swing package as follows,

JTextArea area=new JTextArea(height,width);

Here 'height' and 'width' specifies the number of rows and columns  of text that should be  supported respectively.

Following program explains you how to create and add TextArea into a container,

TextAreaDemo.java

import javax.swing.*;
import java.awt.*;
class TextAreaDemo extends JFrame
{
public TextAreaDemo()
{
setTitle("TextAreaDemo");
setSize(400,400);
setLocationByPlatform(true);
JPanel p=new JPanel();
JLabel lb=new JLabel("TextArea");
p.add(lb);
JPanel p1=new JPanel();
JTextArea area=new JTextArea(10,25);//Text Area of height 10 rows and width 25 Columns 
area.setLineWrap(true);
p1.add(area);
add(p,BorderLayout.NORTH);
add(p1,BorderLayout.CENTER);
}
} 
 
 

TextAreaMain.java


import javax.swing.*;
import java.util.*;
class TextAreaMain
{
public static void main(String args[])
{
TextAreaDemo Areademo=new TextAreaDemo();
Areademo.setVisible(true);
Areademo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


OUTPUT:



 



 As you see the above program it is very simple to create and add a textarea into a panel,but there are some important things that should be noted here, they are,

1. If you see this line  area.setLineWrap(true);  you can identify that Line Wrap is not supported in TextArea by default. So you have to invoke this method explicitly to include this feature into TextArea.

2. As i have said before, if the text to be displayed is more than the display size then the TextArea will grow until it reaches the window size and after that text would be clipped.

3. Like TextField, it also supports setText() and getText() methods to set and retrieve the text to/from the TextArea...



 

 

Java Object Serialization

What is meant by Object Serialization?


In General, Object Serialization refers to writing an object into a stream,you can consider a stream as a data structure used to store values. Mostly the stream used for object serialization is a FILE. The reverse process is called object deserialization i.e, reading the object from the stream.

Object serialization is useful when you want to store objects into a file like primitive data types are stored.



What do they actually meant by writing(storing) objects?

 Are we really writing the objects in to  a stream? The answer is certainly "no". Then what we are writing into a stream?.Actually we are writing the state of the objects,to put it simply 
"we are storing the non-static and non-transient fields of the class in to an external file".


Non-static field:


You could have known that a Non-Static field is a field(or variable) which is not preceded by the static modifier.

For example,  int a=10; 

You could have asked why a static field is not serialized by default?

The  reason is " If there is a non-static field in your class,then each object that you would create will have their won copy of that field but on the other hand if there is a static field then it will be shared by each object". So rather than serializing a static field for each object they(Sun Microsystems) could have left that.


Non-transient field:


Similar to Non-Static field, here the field is not preceded by the  transient keyword. What is its use? There are certain fields which cannot be serialized in java,and if you try to do so then Not Serializable Exception will be thrown.  So, in order to avoid this exception you have to use this keyword.

 Now  let's see how to serialize  and deserialize the object. If you want to serialize an object you need to use the ObjectOutputStream class of the java.io package as follows,

ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("primit.dat"));
os.writeObject(this);// Here "this" specifies the object to be serialized.
 
 Similarly for deserializing the object you need to use the ObjetInputStream class of the java.io package as follows,

ObjectInputStream is=new ObjectInputStream(new FileInputStream("primit.dat"));
SerializeDemo s=(SerializeDemo)is.readObject();
 
 
  what happens when you read object from the stream?

When reading(or Deserializing) the object,"we are not actually reading the object, a new object is re-constructed from the stream with the field values stored in it". Here i would like to highlight one thing, you all know that to construct an object we need the class definition and this applies even if you reconstruct the object i.e, when reading the object from the stream we need the actual class definition in the class path.

 

Program:

  
SerializeMain.java

class Serializemain
{
public static void main(String args[])
{
SerializeDemo p=new SerializeDemo();
p.execute();
}
}


SerializeDemo.java
 

import java.io.*;
import java.util.*;
import java.lang.*;
class SerializeDemo implements Serializable
{
String name;
int Mark;
int rollno;
public void execute()
{
try
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the name:");
name=in.next();
System.out.println("Enter the marks");
Mark=in.nextInt();
System.out.println("Enter the rollno");
rollno=in.nextInt();
ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("primit.dat"));
os.writeObject(this);// Serializing the object
ObjectInputStream is=new ObjectInputStream(new FileInputStream("primit.dat"));
SerializeDemo s=(SerializeDemo)is.readObject();// Deserializing the object
System.out.println("Name:"+s.name);
System.out.println("Mark:"+s.Mark);
System.out.println("RollNo:"+s.rollno);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}


 Output:

Enter the name:
ganesh
Enter the marks
80
Enter the rollno
34
Name:ganesh
Mark:80
RollNo:34

 
 

Executing an RMI

How to execute a RMI program?


There are three steps involved in executing a RMI program,

1. Executing the RMI registry

2.Executing the server

3 Executing the Client


1)Executing the RMI registry



 In windows type in,

c:\>rmiregistry  and press enter key, then the RMI registry will  get started

 

2)Executing the server


Executing a RMI server is quite difficult for newbies. In my previous post RMI tutorial i told you that if a client requires a remote object then it will request the object from the RMI registry with a name.


 who is registering the remote object in the RMI registry?

The answer is server,  but when the server is registering the object it needs to provide the details about the location of the classes and interfaces that may be required  by the remote object. Otherwise javax.naming.communicationexception will be thrown. So in order to avoid this error you need to provide the location of the required classes using the codebase property.


There are two ways to do that,

Way 1:

 Here if you see the line,

java -Djava.rmi.server.codebase=http://localhost:80/ NameServer.




I am saying to the registry that all the required files will be available in the port 80 of this system  using the url http://localhost:80/ . Since i have implemented this in a single system i have done like this, but this will be more useful when the required files are available in another remote machine. At that time you have to specify the hostname of the remote machine in the url like as follows,

http://metallica:80/  if the host name is metallica.


Way 2: 

When all the required files are available in the server itself then you don't need to provide the url but you have to provide the location of the class files in the server like as follows


Since my class files are available in the location file:c:\blog\rmi1\downloads/  i have given like this, so during execution of the server all the required files will be downloaded from the given location.


Executing a client:


It is simple as executing the normal java program,say for an example java NameClient


 

Java RMI Tutorial

How to perform Remote Method Invocation in java


What is meant  by RMI?

Remote Method Invocation refers to invoking a method on an object which resides in a remote machine.For example, let's take the client/server model. In which the server is  usually a remote machine and the client invokes a method of an object which resides in the server to accomplish the required task.

Let us see what are the components needed  by us to perform  the RMI,

Server - A remote machine which serves us the requested operation.

Client - A machine that requests the server to perform some tasks on its behalf.

RMI Registry

In general, if you want to invoke a method of a class what do you actually need? an object of that class. If the class is available in your machine, then there is no problem for you to create an object of that class and invoke the method.

But in RMI the class for which you would like to create an object is not in your machine it is available in the remote machine i.e, the server. So you would need some means to get the object of that class(in the server). What to do? RMI registry has been created for this purpose.

Consider RMI registry as a book where in the objects needed by the clients are registered by the server with a name (in our case it is a RMI url) ,So if any clients needs  an object, it will request the RMI registry(book) with a name, then the RMI registry provides the object corresponding to that name to the client. Now client can access the methods.

 Stub - I would consider a stub as a cached copy of the remote object in the client machine. What is a remote object? An object of a class that implements the Remote Interface is called remote object.  

what do you meant by Remote Interface? An interface that extends the Remote Interface of the java.rmi package

Why should i need to extend the remote interface?

If you want to make a method to be remotely accessible then you should include this method in the interface that extends the Remote Interface.

For example,

import java.rmi.*;
public interface NameCollections extends Remote //Remote interface
{
public String getName(int index)throws RemoteException;//Remotely 
                                                                                                                                   accessible method
}


 A class that implements the Remote interface is like as follows:

import java.rmi.*;
import java.util.*;
import java.rmi.server.*;


class NameStorage extends UnicastRemoteObject implements NameCollections //A
                                                                                                    class that implements the remote interface
{
private Map name;
public NameStorage()throws RemoteException
{

name=new HashMap();
name.put(1,"Ganesh");
name.put(2,"jazz");
}
public String getName(int index)throws RemoteException
{
String str=name.get(new Integer(index));
return str;
}
}

 NameStorage name=new NameStorage(); //Here name is the remote object

Skeleton - It is exactly similar to the stub but it resides in the server machine, After java 1.2 the skeleton has been omitted. So let's leave this component.


Server


import java.rmi.*;
import javax.naming.*;
class NameServer 
{
public static void main(String args[])throws RemoteException,NamingException
{
Context ictxt=new InitialContext();
NameStorage store=new NameStorage();
ictxt.bind("rmi:Storage",store);//Binding the remote object(store) with a 
                                                                                           name(storage)
System.out.println("Clients can invoke the methods....");
}
}

Client


import java.rmi.*;
import java.util.*;
import javax.naming.*;
class NameClient
{
public static void main(String args[])throws NamingException,RemoteException
{
Context itxt=new InitialContext();
 
//Searching for the remote object in the RMI registry  
 
NameCollections coll=(NameCollections)itxt.lookup("rmi://localhost/Storage");
 
String n1=coll.getName(2);// Invoking the remote method
System.out.println("Name Obtained from the server:"+n1);
}
} 

Though it appears as invoking the remote method, it doesn't actually invoke the remote method, then what? It actually invokes the method on the stub object(which is available in the client machine).

This stub object would in turn  call the remote method which is available in the server and forwards the return value from the server to  the  client.


Since it is a lengthy post, i have dealt with the execution of the RMI program in another
 post How to execute a RMI program .  



JLabel - An Example

How to create and use JLabel

  
In java, labels are components used to hold the texts. A Label will look like "Text engraved   in the container(may be  a frame or panel)".

In general what is meant by a Label?


 A Label is a piece of a paper that provides information about the object to which it is affixed with. Similarly, here also the sole purpose of a label  is to provide information about the object  that cannot able to identify itself to the user.

Say, for  an example, In java,On seeing a button we can able to tell what it is intended to do, like NEXT, CANCEL, but a text field , text area cannot be able to describe themselves. So, in these kind of situations labels helps us to identify them.

Creating a Label 


In order to create a label,we need to create an object for the JLabel class by giving the text to be displayed as a label, like

JLabel l1=new JLabel("NAME");

JLabel l2=new JLabel(ADDRESS);


Program:

 

LabelDemo.java


import java.util.*;
import javax.swing.*;
import java.awt.*;
class LabelDemo extends JFrame
{
public LabelDemo()
{
setSize(300,300);
setTitle("LabelDemo");
setLayout(new FlowLayout(FlowLayout.CENTER,0,100));
setLocationByPlatform(true);
JPanel p=new JPanel();
JLabel l1=new JLabel("NAME :");
p.add(l1);
JTextField tf=new JTextField(20);
tf.setColumns(10);
p.add(tf);
add(p);
}
}


LabelMain.java


import java.util.*;
import javax.swing.*;

class LabelMain
{
public static void main(String args[])
{
LabelDemo ld=new LabelDemo();
ld.setVisible(true);
ld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


output:

 



In java, there is a facility available for us to either add an image , or an image along with the text into a label.Let's see how to add an image into a label.

JLabel l1=new JLabel(new ImageIcon("Nature.jpg"));

JLabel l2=JLabel("Nature",new ImageIcon("Nature.jpg"),SwingConstants.LEFT);


In tha above example, if you modify the Label creation statement like  as follows it will enable you to add  an image to a label.







JTextField - An Example

How to create and use JTextField 


In an user interface design,a text field provides a way to interact with the user either by prompting the user to enter an input  or by displaying some information in the textfield.

In this post we are going to see how to create a textfield and use it in our application.

To create a text field you need to create an object of the JTextField class in the javax.swing package as follows.

JTextField tf=new JTextField(20);

The argument value "20" in the above statement specifies the width of the TextField.

JTextField class has four constructors which are of great concern to us. They are...

1. JTextField tf=new JTextField() - creates a text field with width "0"

2. JTextField tf=new JTextField(int columns)-creates a text field with the given   
width.

3. JTextField tf=new JTextField(String str,int columns) ;

creates a text field with the given width and displays the string given in the first argument.

4. JTextField tf=new JTextField(String str)

creates a text field displaying the string with the length of the string as the size of the text field.


Programs

 

JTextFieldDemo

import java.util.*;
import javax.swing.*;
import java.awt.*;
class TextFieldDemo extends JFrame
{
public TextFieldDemo()
{
setSize(300,300);
setTitle("JTextFieldDemo");
setLayout(new FlowLayout(FlowLayout.CENTER,0,100));// used to center the panel in the 
                                                                                                                                                 frame   
setLocationByPlatform(true);
JPanel p=new JPanel();

JLabel lb=new JLabel("INPUT1");
p.add(lb);
JTextField tf=new JTextField();
tf.setText("Hi!Enter the input here.. ");
p.add(tf);
add(p);
}
} 
 
 

TextFieldMain

 
import java.util.*;
import javax.swing.*;

class TextFieldMain
{
public static void main(String args[])
{
TextFieldDemo tfd=new TextFieldDemo();
tfd.setVisible(true);
tfd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
 


Output:





JTextField class has two important methods namely,

void  setText(String str) - to display a particular text in the text field say for an example,

JTextField tf=new JTextField(20);
tf.setText("Hai");
  
String getText ()-  to retrieve text from the particular text field like as follows,

String s=tf.getText();

You can also dynamically change the "size" of the text field using setColumns(int width).

for example tf.setColumns(10); will  provide a output like this,












javax.naming.communicationexception in RMI

When javax.naming.communicationexception will be thrown in RMI?


In java API, it is given that this exception will be thrown when the client is unable to communicate with the naming services.The reason for this exception may be due to server or client side failures.

In this post we will see how server-side failure causes this exception in RMI .

I guess you know about RMI(Remote Method Invocation) through which we can able to access the methods of a remote object. To execute an RMI application we need these components like,

1.Server

2.Client

3.Stub

4.Skeleton

5.RMI registry

Here we are going to focus on the stub, 

what is meant by a stub? 

It is a client-side object used to communicate with the server-side object(skeleton). The client needs to get a stub object.

 From where the client gets its stub object?

The client will get the stub object from the RMI registry, where the server would have already registered  a remote object with a name(RMI url).The client will use the name(RMI url) to get the stub object.


This problem arises when the server is unable to register( or bind) an object to a name (RMI url).

" The reason for this problem is that,when registering a remote object in the RMI registry we need to specify the "path" of the remote interface(in our example - NameCollections) implemented by the object and other components needed by that object, if any of the components needed is not available in the given path then this exception will be thrown."

 
 See the following example to understand about this exception.


Program:


Prog1:


import java.rmi.*;
public interface NameCollections extends Remote
{
public String getName(int index)throws RemoteException;
}


Prog2:

import java.rmi.*;
import java.util.*;
import java.rmi.server.*;

class NameStorage extends UnicastRemoteObject implements NameCollections 
{
private Map name;
public NameStorage()throws RemoteException
{
name=new HashMap();
name.put(1,"Ganesh");
name.put(2,"jazz");
}
public String getName(int index)throws RemoteException
{
String str=name.get(new Integer(index));
return str;
}
}
 
 
Prog 3: 


import java.rmi.*;
import javax.naming.*;
class NameServer 
{
public static void main(String args[])throws RemoteException,NamingException
{
Context ictxt=new InitialContext();
NameStorage store=new NameStorage();
ictxt.bind("rmi:Storage",store);
System.out.println("Clients can invoke the methods....");
}
} 
 
 

Output:


Screen1:

 



Screen2:



Here the NameServer program tries to register a remote object with the RMI url and when registering the object as i have said before we need to specify the path for required files. Here i  have tried to look for files in port:8080,but see what happens.

Have a look at the screen1, there i had executed a command like the following

java -cp . NanoHTTPD 8080 , implies that the server NanoHTTPD has to  serve the files in the port 8080, but if you see the screen 1 it says that the server is serving files from port 80.

I didn't noticed that and i have given the path as codebase= http://localhost:8080/  to look for files that's why i got this error?. 

So i have to change the command as follows codebase= http://localhost:80/ to resolve the above problem. I don't  know is there any problem in NanoHTTPD server or in my laptop.

"But an important thing to be noted here is the path that you provide should exactly point to the location where the necessary files are residing in the server."


In some cases, if you left the trailing slash in the command as given in the below line
 codebase =  http://localhost:8080   then you would get this error.





 
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top