Friday, August 12, 2011

Deleting The List Item

The Program To Delete A List Item
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DeleteListItem
{
JFrame jf;
JScrollPane sp;
JList list;
DefaultListModel model;
MyListListener listener;
JLabel lb;
DeleteListItem()
{
jf=new JFrame("Delete List Item");
model=new DefaultListModel();
addElementsToModel();
list=new JList(model);
listener=new MyListListener();
list.addListSelectionListener(listener);
list.addKeyListener(new DeleteListener());
list.addMouseListener(new ListMouseListener());
lb=new JLabel("Status:");

sp=new JScrollPane(list);
jf.add(sp,BorderLayout.CENTER);
jf.add(lb,BorderLayout.SOUTH);
jf.setSize(200,200);
jf.setLocation(300,300);
jf.setVisible(true);
}

public void addElementsToModel()
{
model.addElement("Red");
model.addElement("Blue");
model.addElement("Green");
model.addElement("White");
model.addElement("Black");
model.addElement("Orange");
}

public static void main(String... s)
{
new DeleteListItem();
}

class DeleteListener extends KeyAdapter
{
public void keyReleased(KeyEvent ke)
{
if(ke.getKeyCode()==KeyEvent.VK_DELETE)
{
String obj = (String)list.getSelectedValue();
int result;
result=JOptionPane.showConfirmDialog(null,"Do You Want To Delete the "+obj,"Delete The Client",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
{
model.remove(list.getSelectedIndex());
JOptionPane.showMessageDialog(null,obj+" Deleted","Deleted",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}

class ListMouseListener extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
if(me.getClickCount()==2)
{
JList list=(JList)me.getComponent();
int index = list.locationToIndex(me.getPoint());
ListModel model=list.getModel();
String item = (String)model.getElementAt(index);
list.ensureIndexIsVisible(index);
lb.setText("Double Clicked On "+item);
}
}
}

class MyListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{
String obj = (String)list.getSelectedValue();
lb.setText("Status:"+obj+" Clicked");
}
}
}

}//end class

EveryThing is OK with this program if you didnot call any function on the Object fetched from the List on clicking,Notice the status when you delete an item from the list ,you will see the "null clicked" there....
Now suppose we want the find the length of the item whenever it is clicked ,so we have to call the length function on the String object which is fetched from the list on clicking...
So,we have to do something like this...change the red line by the following line and run the program again:
lb.setText("Status:"+obj+" Clicked Length:"+obj.length());
And now try to delete the item again You will Get the NPE that is Null Pointer Exception,because length funtion is called on the Null String...and the list will not work properly,you can see that....

So,To remove this problem we have to:
1.)First,Remove the ListSelectionListener,
2.)Now,Remove the item from the model,
3.)Add the ListSelectionListener again.

So,Replace if block in the keyRelease method by the following if block
if(result==JOptionPane.YES_OPTION)
{
list.removeListSelectionListener(listener);
model.remove(list.getSelectedIndex());
list.addListSelectionListener(listener);
JOptionPane.showMessageDialog(null,obj+" Deleted","Deleted",JOptionPane.INFORMATION_MESSAGE);
}

Now This will work fine......

DreamHost Discount Code
hostgator coupon

Sunday, August 7, 2011

Splash Screen

Below is the Example that shows the splash screen without using any special class:

import javax.swing.*;
import java.awt.*;
import java.awt.event .*;
import java.io.*;
import javax.swing.border.*;
public class Splash
{
JFrame frame;
JPanel jf;
JLabel lb,imglb,locklb;
JPasswordField pw;
Splash()
{

try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e){}

frame=new JFrame("Splash");
jf=new JPanel();
lb=new JLabel("Enter Password:");
imglb=new JLabel(new ImageIcon(".//bg.gif"));


pw=new JPasswordField();
pw.setFont(new Font("comic sans ms",Font.BOLD,18));
jf.setLayout(null);
lb.setBounds(265,30,150,30);
lb.setForeground(Color.white);
lb.setFont(new Font("Comic Sans ms",Font.BOLD,18));
imglb.setBounds(0,0,480,200);

Border blackline = BorderFactory.createLineBorder(Color.red);
imglb.setBorder(blackline);

pw.setBounds(250,60,180,30);
jf.add(lb);
jf.add(pw);

jf.add(imglb);

frame.add(jf);
frame.setSize(480,200);
frame.setLocation(350,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setUndecorated(true);
try
{
JFrame sframe=new JFrame("Splash");
JPanel jp=new JPanel();
JLabel lb=new JLabel("Loading.........");
jp.setLayout(null);
JLabel piclb=new JLabel(new ImageIcon(".//splash.gif"));
piclb.setBounds(0,0,300,200);
lb.setBounds(10,200,300,25);
jp.add(piclb);
jp.add(lb);
lb.setFont(new Font("Comic Sans Ms",Font.BOLD,20));
jp.setBackground(Color.white);
sframe.add(jp);
sframe.setResizable(false);
sframe.setSize(350,240);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-300)/2;
int y = (screen.height-200)/2;
sframe.setLocation(x,y);
sframe.setUndecorated(true);
sframe.setVisible(true);
String st="Loading..."+".";
for(int i=0;i<15;i++)
{
st+=".";
lb.setText(st);
Thread.sleep(700);
}
sframe.setVisible(false);
frame.setVisible(true);
}catch(Exception e){System.out.println("exception in login splash:"+e);}
}

public static void main(String... s)
{
new Splash();
}
}//end class

Download

setUndecorated before setVisible

As we seen in the post,we can show the frame without the Title Bar by calling the method setUndecorated(true) on a JFrame.

One thing that should be noted is that we should call this method before calling the method setVisible(true); on a JFrame eotherwise we will not be able to draw the frame without Title Bar..
as it will throw IllegalComponentStateException if we call setUndecorated(true) after calling the setVisible(true) method on a JFrame...

So,be sure that you call setUndecorated method before calling the setVisible method....

Saturday, August 6, 2011

JFrame Without TitleBar

import javax.swing.*;
import java.awt.*;
import java.awt.event .*;
import java.io.*;
import javax.swing.border.*;
public class Login implements ActionListener
{
JFrame frame;
JPanel jp;
JLabel lb;
JLabel imglb;
JPasswordField pw;
Login()
{

frame=new JFrame("Login");
jp=new JPanel();
lb=new JLabel("Enter Password:");
imglb=new JLabel(new ImageIcon(".//bg.gif"));


pw=new JPasswordField();
pw.setFont(new Font("comic sans ms",Font.BOLD,18));
jp.setLayout(null);
lb.setBounds(265,30,150,30);
lb.setForeground(Color.white);
lb.setFont(new Font("Comic Sans ms",Font.BOLD,18));
imglb.setBounds(0,0,480,200);

Border blackline = BorderFactory.createLineBorder(Color.red);
imglb.setBorder(blackline);

pw.setBounds(250,60,180,30);
pw.addActionListener(this);
jp.add(lb);
jp.add(pw);

jp.add(imglb);

frame.add(jp);
frame.setSize(480,200);
frame.setLocation(350,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String... s)
{
new Login();
}
public void actionPerformed(ActionEvent e)
{
if(String.valueOf(pw.getPassword()).equals("admin"))
{
JOptionPane.showMessageDialog(null,"Password is right,Moving to the next Frame","Sucess",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Password is Wrong,Re-enter","Sucess",JOptionPane.ERROR_MESSAGE);
pw.requestFocus();
}
}

}

output:

Welcome Back

Hello Friends!!!!!!!!!
Welcome back after a long time...
In the mean while was busy in finding a job,exams and all.....
Got placed in Sasken Communication Technologies.
but still waiting for the DOJ...Free at home trying to do something beneficial

Recently created a project for the Vets (named it as Pet Treatment System)or in their language you can call it a software......

Technology used --Pure Core Java,
Database--MS-ACCESS..

Learnt a lot from the project ,its very hard to complete the need of a client that keeps on changing day by day,have to change the concepts or logic 2-3 times but finally ,its working fine,and generic to a gud extent it took around 1 and half month to complete....

Now,will post some interested things that I had learnt while creating the project like......

Create a JFrame without having title bar
Creating the Splash Screen without using any special class
Deleting The item from the list having a listener
Finding The DOB from Age
Finding Age from DOB
Adding a panel to the Scrollpane
Manipulating a List Item as on Oject
Using Web-Cam with Java
Printing a JFrame in Java
Printing a Table in java
Validation of data enterned by the user
Making the trial version
Registering the software with a key
Making it somewhat secure so that it is difficult to copy
DSN handling
BackUp and restore facility
Running the Java Program without manually installing the JDK...