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

0 comments:

Post a Comment