Header Ads

test

Address Book in Java

 

asy to keep records first create a directory named data which should be in Bin folder of
jdk and in that create a file name data.dat

Java » Java Swing

   1: import java.io.*;
   2: import java.awt.*;
   3: import java.awt.event.*;
   4: import javax.swing.*;
   5: import javax.swing.event.*;
   6: import java.util.*;
   7:  
   8: class AddressBook implements ActionListener
   9:     {
  10:  
  11:           JPanel topPanel,bottomPanel;
  12:           JScrollPane scrollPane;
  13:           JFrame frame;
  14:  
  15:           JMenuBar menubar = new JMenuBar(); ;
  16:              JMenu menu = new JMenu();
  17:              JMenuItem menuItem;
  18:  
  19:              Toolkit kit = Toolkit.getDefaultToolkit();
  20:              Dimension screenSize = kit.getScreenSize();
  21:              int screenHeight = screenSize.height;
  22:              int screenWidth = screenSize.width;
  23:  
  24:              Image img = kit.getImage("images/icon.JPG");
  25:  
  26:           AddressBook()
  27:             {
  28:  
  29:                     frame = new JFrame("Address Book");
  30:                     frame.setSize(680,200);
  31:                     
  32: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33:                     frame.setLocation(screenWidth/4, screenHeight/4);
  34:                     frame.setIconImage(img);
  35:                     addWidgets();
  36:                     frame.show();
  37:  
  38:             }
  39:  
  40:  
  41:  
  42:          public void addWidgets()
  43:  
  44:             {
  45:                        menubar.add(menu);
  46:  
  47:                        menu = new JMenu("Options");
  48:                        menuItem = new JMenuItem("Add New Contact");
  49:                        menu.add(menuItem);
  50:                        menuItem.addActionListener(this);
  51:  
  52:                        menuItem = new JMenuItem("Delete Contact");
  53:                        menu.add(menuItem);
  54:                        menuItem.addActionListener(this);
  55:  
  56:                        menuItem = new JMenuItem("Search Contacts");
  57:                        menu.add(menuItem);
  58:                        menuItem.addActionListener(this);
  59:  
  60:                        menuItem = new JMenuItem("Sort Contacts");
  61:                        menu.add(menuItem);
  62:                        menuItem.addActionListener(this);
  63:  
  64:                        menuItem = new JMenuItem("View All Contacts");
  65:                        menu.add(menuItem);
  66:                        menuItem.addActionListener(this);
  67:  
  68:                        menuItem = new JMenuItem("Backup Contacts");
  69:                        menu.add(menuItem);
  70:                        menuItem.addActionListener(this);
  71:  
  72:  
  73:                        menubar.add(menu);
  74:  
  75:                        menu = new JMenu("Help");
  76:  
  77:                        menuItem = new JMenuItem("Help Contents");
  78:                        menu.add(menuItem);
  79:                        menuItem.addActionListener(this);
  80:  
  81:                        menuItem = new JMenuItem("About");
  82:                        menu.add(menuItem);
  83:                        menuItem.addActionListener(this);
  84:  
  85:                        menubar.add(menu);
  86:  
  87:                        frame.setJMenuBar(menubar);
  88:  
  89:  
  90:                        JPanel topPanel = new JPanel();
  91:                     JPanel bottomPanel = new JPanel();
  92:  
  93:                     //Add Buttons To Bottom Panel
  94:                     JButton AddNew = new JButton("Add New Contact");
  95:                     JButton DeleteContact = new JButton("Delete 
  96: Contact");
  97:                     JButton SearchContacts = new JButton("Search
  98: Contacts");
  99:                     JButton SortContacts = new JButton("Sort 
 100: Contacts");
 101:                     JButton ViewContactList = new JButton("View All
 102: Contacts");
 103:  
 104:                     JLabel label = new JLabel("<HTML><FONT FACE = ARIAL
 105: SIZE = 2><B>Use The options below and In The Menu To Manage Contacts");
 106:  
 107:                     //Add Action Listeners
 108:                     AddNew.addActionListener(this);
 109:                     DeleteContact.addActionListener(this);
 110:                     SearchContacts.addActionListener(this);
 111:                     SortContacts.addActionListener(this);
 112:                     ViewContactList.addActionListener(this);
 113:  
 114:                     topPanel.add(label);
 115:  
 116:                     bottomPanel.add(AddNew);
 117:                     bottomPanel.add(DeleteContact);
 118:                     bottomPanel.add(SearchContacts);
 119:                     bottomPanel.add(SortContacts);
 120:                     bottomPanel.add(ViewContactList);
 121:  
 122:                     frame.getContentPane().add(topPanel,
 123: BorderLayout.NORTH);
 124:                     frame.getContentPane().add(bottomPanel,
 125: BorderLayout.SOUTH);
 126:                     frame.setResizable(false);
 127:  
 128:  
 129:  
 130:             }
 131:  
 132:  
 133:          public static void main(String args[])
 134:             {
 135:                 AddressBook add = new AddressBook();
 136:  
 137:             }
 138:  
 139:  
 140:          OperationHandler oh = new OperationHandler();
 141:  
 142:          public void actionPerformed(ActionEvent ae)
 143:             {
 144:                 if(ae.getActionCommand() == "Add New Contact")
 145:                     {
 146:                          oh.AddNew();
 147:  
 148:                     }
 149:  
 150:                 else  if(ae.getActionCommand() == "Search Contacts")
 151:                     {
 152:                          oh.SearchContacts();
 153:  
 154:                     }
 155:  
 156:                 else  if(ae.getActionCommand() == "Sort Contacts")
 157:                     {
 158:                          oh.SortContacts();
 159:  
 160:                     }
 161:  
 162:                 else  if(ae.getActionCommand() == "Delete Contact")
 163:                     {
 164:                           oh.DeleteContact();
 165:  
 166:                     }
 167:  
 168:                 else  if(ae.getActionCommand() == "View All Contacts")
 169:                     {
 170:  
 171:                           oh.ViewAllContacts();
 172:  
 173:                     }
 174:  
 175:                 else  if(ae.getActionCommand() == "About")
 176:                     {
 177:                           JOptionPane.showMessageDialog(frame, "About 
 178: Address
 179: Book:
 180:  
 181: Created By Rahul Chouhan
 182: Computer Engg, IET-DAVV,
 183: Indore.
 184: Website:
 185: http://www.Rahul Chouhan.tk","About Address Book",
 186: JOptionPane.INFORMATION_MESSAGE);
 187:  
 188:                     }
 189:                 else  if(ae.getActionCommand() == "Help Contents")
 190:                     {
 191:                         try
 192:                          {
 193:                              oh.showHelp();
 194:                          }
 195:                         catch(IOException e)
 196:                          {
 197:                          }
 198:  
 199:                     }
 200:                  else  if(ae.getActionCommand() == "Backup Contacts")
 201:                     {
 202:                         JFileChooser chooser = new JFileChooser();
 203:                          chooser.setCurrentDirectory(new File("."));
 204:                          chooser.setMultiSelectionEnabled(false);
 205:  
 206: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 207:                          chooser.showSaveDialog(frame);
 208:                          FileOutputStream  bfout = null;
 209:                          FileInputStream bfin = null;
 210:                          String filename=null;
 211:  
 212:                          int p;
 213:  
 214:                          try
 215:                           {
 216:                               filename = chooser.getSelectedFile().getPath();
 217:                           }
 218:                          catch(Exception e)
 219:                           {
 220:                           }
 221:  
 222:                          try
 223:                             {
 224:                                bfout = new FileOutputStream(filename +
 225: "\data.dat");
 226:                             }
 227:                          catch(Exception e)
 228:                             {
 229:  
 230:                             }
 231:                          try
 232:                            {
 233:                               bfin = new FileInputStream("data/data.dat");
 234:                            }
 235:                          catch(Exception e)
 236:                            {
 237:  
 238:                            }
 239:  
 240:                          try
 241:                             {
 242:                                    do
 243:                                      {  p = bfin.read();
 244:                                         if(p!=-1)
 245:                                             bfout.write(p);
 246:                                      }while(p!=-1);
 247:                                 }
 248:                           catch(Exception e)
 249:                              {
 250:  
 251:                              }
 252:  
 253:  
 254:                     }
 255:  
 256:             }
 257:  
 258:  
 259:     }
 260:  
 261:  
 262: class Contact implements Serializable
 263:     {
 264:          private String FName;
 265:          private String LName;
 266:          private String Nname;
 267:          private String EMail;
 268:          private String Address;
 269:          private String PhoneNo;
 270:          private String Webpage;
 271:          private String Bday;
 272:  
 273:          public void setDetails(String fname, String lname, String nname,
 274: String email, String address, String phone, String web, String bday)
 275:              {
 276:                    FName = fname;
 277:                    LName = lname;
 278:                    Nname = nname;
 279:                    EMail = email;
 280:                    Address = address;
 281:                    PhoneNo = phone;
 282:                    Webpage = web;
 283:                    Bday = bday;
 284:              }
 285:  
 286:  
 287:          public String getFName()
 288:              {
 289:                    return FName;
 290:              }
 291:  
 292:          public String getLName()
 293:              {
 294:                    return LName;
 295:              }
 296:  
 297:          public String getNname()
 298:              {
 299:                    return Nname;
 300:              }
 301:  
 302:           public String getEMail()
 303:              {
 304:                    return EMail;
 305:              }
 306:  
 307:           public String getAddress()
 308:              {
 309:                    return Address;
 310:              }
 311:  
 312:           public String getPhoneNo()
 313:              {
 314:                    return PhoneNo;
 315:              }
 316:  
 317:           public String getWebpage()
 318:              {
 319:                    return Webpage;
 320:              }
 321:  
 322:           public String getBday()
 323:              {
 324:                    return Bday;
 325:              }
 326:  
 327:  
 328:     }
 329:  
 330:  
 331: class OperationHandler implements ListSelectionListener, 
 332: ActionListener,
 333: Runnable
 334:     {
 335:  
 336:  
 337:        JFrame newFrame;
 338:        JTextField txtFirstName;
 339:        JTextField txtLastName;
 340:        JTextField txtNickname;
 341:        JTextField txtEMail;
 342:        JTextField txtAddress;
 343:        JTextField txtPhoneNo;
 344:        JTextField txtWebpage;
 345:        JTextField txtBDay;
 346:  
 347:        JButton BttnSaveAdded;
 348:  
 349:        Vector v = new Vector(10,3);
 350:        int i=0,k=0;
 351:  
 352:        Toolkit kit = Toolkit.getDefaultToolkit();
 353:        Dimension screenSize = kit.getScreenSize();
 354:        int screenHeight = screenSize.height;
 355:        int screenWidth = screenSize.width;
 356:  
 357:        Image img = kit.getImage("images/icon.JPG");
 358:  
 359:        FileInputStream fis;
 360:        ObjectInputStream ois;
 361:  
 362:        JList list;
 363:        DefaultListModel listModel;
 364:        ListSelectionModel listSelectionModel;
 365:  
 366:        JRadioButton byfname, bylname;
 367:  
 368:        Thread t;
 369:  
 370:        JTable searchTable;
 371:  
 372:        JTextField txtSearch;
 373:  
 374:        String columnNames[] = { "First Name",
 375:                                                   "Last Name",
 376:                                                   "Nickname",
 377:                                                   "E Mail Address",
 378:                                                   "Address",
 379:                                                   "Phone No.",
 380:                                                   "Webpage",
 381:                                                   "B'day"
 382:                                                 };
 383:  
 384:        Object data[][]= new Object[5][8];
 385:  
 386:        OperationHandler()
 387:            {
 388:  
 389:                 try {
 390:                        fis = new FileInputStream("data/data.dat");
 391:                        ois = new ObjectInputStream(fis);
 392:                        v = (Vector) ois.readObject();
 393:                        ois.close();
 394:                     }
 395:  
 396:                catch(Exception e)
 397:                     {
 398:  
 399:                     }
 400:  
 401:            }
 402:  
 403:  
 404:         public void run()
 405:            {
 406:  
 407:                   try
 408:                    {
 409:                         FileOutputStream fos = new
 410: FileOutputStream("data/data.dat");
 411:                         ObjectOutputStream oos = new 
 412: ObjectOutputStream(fos);
 413:                      oos.writeObject(v);
 414:                         oos.flush();
 415:                         oos.close();
 416:  
 417:                    }
 418:                   catch(Exception e)
 419:                    {
 420:                          JOptionPane.showMessageDialog(newFrame, "Error 
 421: Opening
 422: Data File: Could Not Save Contents.", "Error Opening Data File",
 423: JOptionPane.INFORMATION_MESSAGE);
 424:                    }
 425:  
 426:            }
 427:  
 428:  
 429:        public void AddNew()
 430:            {
 431:                newFrame = new JFrame("Add New");
 432:                newFrame.setSize(220,250);
 433:                newFrame.setResizable(false);
 434:                newFrame.setIconImage(img);
 435:  
 436:                JLabel lblFirstName = new JLabel("First Name: ");
 437:                JLabel lblLastName = new JLabel("Last Name: ");
 438:                JLabel lblNickname = new JLabel("Nickname: ");
 439:                JLabel lblEMail = new JLabel("EMail: ");
 440:                JLabel lblAddress = new JLabel("Address: ");
 441:                JLabel lblPhoneNo = new JLabel("Phone No: ");
 442:                JLabel lblWebpage = new JLabel("Webpage: ");
 443:                JLabel lblBDay = new JLabel("BDay: ");
 444:                JLabel lblEmpty1 = new JLabel("");
 445:                JLabel lblEmpty2 = new JLabel("");
 446:  
 447:                txtFirstName = new JTextField(10);
 448:                txtLastName = new JTextField(10);
 449:                txtNickname = new JTextField(10);
 450:                txtEMail = new JTextField(10);
 451:                txtAddress = new JTextField(10);
 452:                txtPhoneNo = new JTextField(10);
 453:                txtWebpage = new JTextField(10);
 454:                txtBDay = new JTextField(10);
 455:  
 456:                JButton BttnAdd = new JButton("Add New!");
 457:                BttnSaveAdded = new JButton("Save Added!");
 458:  
 459:                BttnAdd.addActionListener(this);
 460:                BttnSaveAdded.addActionListener(this);
 461:                BttnSaveAdded.setEnabled(false);
 462:  
 463:  
 464:                JPanel centerPane = new JPanel();
 465:                JPanel bottomPane = new JPanel();
 466:  
 467:                centerPane.add(lblFirstName);
 468:                centerPane.add(txtFirstName);
 469:                centerPane.add(lblLastName);
 470:                centerPane.add(txtLastName);
 471:                centerPane.add(lblNickname);
 472:                centerPane.add(txtNickname);
 473:                centerPane.add(lblEMail);
 474:                centerPane.add(txtEMail);
 475:                centerPane.add(lblAddress);
 476:                centerPane.add(txtAddress);
 477:                centerPane.add(lblPhoneNo);
 478:                centerPane.add(txtPhoneNo);
 479:                centerPane.add(lblWebpage);
 480:                centerPane.add(txtWebpage);
 481:                centerPane.add(lblBDay);
 482:                centerPane.add(txtBDay);
 483:                bottomPane.add(BttnAdd);
 484:                bottomPane.add(BttnSaveAdded);
 485:  
 486:                centerPane.setLayout(new GridLayout(0,2));
 487:  
 488:  
 489:  
 490: newFrame.getContentPane().add(centerPane,BorderLayout.CENTER);
 491:  
 492: newFrame.getContentPane().add(bottomPane,BorderLayout.SOUTH);
 493:                newFrame.setLocation(screenWidth/4, screenHeight/4);
 494:                newFrame.show();
 495:  
 496:            }
 497:  
 498:  
 499:        public void SearchContacts()
 500:            {
 501:                   newFrame = new JFrame("Search Contacts");
 502:                newFrame.setSize(700,220);
 503:                newFrame.setLocation(screenWidth/4, screenHeight/4);
 504:                newFrame.setIconImage(img);
 505:                newFrame.setResizable(false);
 506:  
 507:                JPanel topPane = new JPanel();
 508:                JLabel label1 = new JLabel("Search Contacts by First 
 509: Name
 510: or Last Name or Both Spaced Via a Single Space:");
 511:                topPane.add(label1);
 512:  
 513:                JPanel centerPane = new JPanel();
 514:                JLabel label2 = new JLabel("Search String:");
 515:                txtSearch = new JTextField(20);
 516:                JButton bttnSearch = new JButton("Search!");
 517:                bttnSearch.addActionListener(this);
 518:                JButton bttnCancel = new JButton("Cancel");
 519:                bttnCancel.addActionListener(this);
 520:                centerPane.add(label2);
 521:                centerPane.add(txtSearch);
 522:                centerPane.add(bttnSearch);
 523:                centerPane.add(bttnCancel);
 524:  
 525:                searchTable = new JTable(data,columnNames);
 526:                JScrollPane scrollPane = new JScrollPane(searchTable);
 527:                searchTable.setPreferredScrollableViewportSize(new
 528: Dimension(500, 90));
 529:  
 530: newFrame.getContentPane().add(scrollPane,BorderLayout.SOUTH);
 531:  
 532:                newFrame.getContentPane().add(topPane, 
 533: BorderLayout.NORTH);
 534:                newFrame.getContentPane().add(centerPane,
 535: BorderLayout.CENTER);
 536:                newFrame.show();
 537:            }
 538:  
 539:  
 540:        public void SortContacts()
 541:            {
 542:                newFrame = new JFrame("Sort Contacts");
 543:                newFrame.setSize(250,160);
 544:                newFrame.setLocation(screenWidth/4, screenHeight/4);
 545:                newFrame.setIconImage(img);
 546:                newFrame.setResizable(false);
 547:  
 548:                byfname = new JRadioButton("By First Name");
 549:                byfname.setActionCommand("By First Name");
 550:                byfname.setSelected(true);
 551:  
 552:                bylname = new JRadioButton("By Last Name");
 553:                bylname.setActionCommand("By Last Name");
 554:  
 555:                ButtonGroup group = new ButtonGroup();
 556:                group.add(byfname);
 557:                group.add(bylname);
 558:  
 559:                JPanel topPane = new JPanel();
 560:                JLabel label = new JLabel("Sort Contacts By:");
 561:                topPane.add(label);
 562:  
 563:                JPanel pane = new JPanel(new GridLayout(0,1));
 564:                pane.add(byfname);
 565:                pane.add(bylname);
 566:  
 567:                JPanel bottomPane = new JPanel();
 568:                JButton sortBttn = new JButton("Sort Contacts");
 569:                JButton cancelBttn = new JButton("Cancel");
 570:                bottomPane.add(sortBttn);
 571:                bottomPane.add(cancelBttn);
 572:                sortBttn.addActionListener(this);
 573:                cancelBttn.addActionListener(this);
 574:  
 575:                newFrame.getContentPane().add(topPane, 
 576: BorderLayout.NORTH);
 577:                newFrame.getContentPane().add(pane, 
 578: BorderLayout.CENTER);
 579:                newFrame.getContentPane().add(bottomPane,
 580: BorderLayout.SOUTH);
 581:  
 582:                newFrame.show();
 583:  
 584:            }
 585:  
 586:  
 587:        public void DeleteContact()
 588:            {
 589:                String fname, lname;
 590:                newFrame = new JFrame("Delete Contact");
 591:                newFrame.setSize(300,300);
 592:                newFrame.setLocation(screenWidth/4, screenHeight/4);
 593:                newFrame.setIconImage(img);
 594:  
 595:                JPanel centerPane = new JPanel();
 596:                listModel = new DefaultListModel();
 597:  
 598:                Contact contact = new Contact();
 599:  
 600:                for(int l=0;l<v.size();l++)
 601:                      {
 602:                          contact = (Contact) v.elementAt(l);
 603:  
 604:                          fname = contact.getFName();
 605:                          lname = contact.getLName();
 606:                          listModel.addElement(fname + " " + lname);
 607:  
 608:                      }
 609:  
 610:  
 611:                list = new JList(listModel);
 612:                
 613: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 614:                listSelectionModel = list.getSelectionModel();
 615:                listSelectionModel.addListSelectionListener(this);
 616:  
 617:                JScrollPane listScrollPane = new JScrollPane(list);
 618:  
 619:                JPanel topPane = new JPanel();
 620:                JLabel label = new JLabel("Current Contacts:");
 621:                topPane.add(label);
 622:  
 623:                JPanel bottomPane = new JPanel();
 624:  
 625:                JButton bttnDelete = new JButton("Delete Selected");
 626:                bottomPane.add(bttnDelete);
 627:                bttnDelete.addActionListener(this);
 628:  
 629:                JButton bttnCancel = new JButton("Cancel");
 630:                bottomPane.add(bttnCancel);
 631:                bttnCancel.addActionListener(this);
 632:  
 633:                newFrame.getContentPane().add(topPane, 
 634: BorderLayout.NORTH);
 635:                newFrame.getContentPane().add(listScrollPane,
 636: BorderLayout.CENTER);
 637:                newFrame.getContentPane().add(bottomPane,
 638: BorderLayout.SOUTH);
 639:  
 640:                newFrame.show();
 641:  
 642:            }
 643:  
 644:  
 645:        public void ViewAllContacts()
 646:            {
 647:  
 648:                        newFrame = new JFrame("All Contacts In The Address
 649: Book");
 650:                     newFrame.setSize(600,300);
 651:                     newFrame.setIconImage(img);
 652:  
 653:                     Contact con = new Contact();
 654:  
 655:  
 656:                        String columnNames[] = { "First Name",
 657:                                           "Last Name",
 658:                                           "Nickname",
 659:                                           "E Mail Address",
 660:                                           "Address",
 661:                                           "Phone No.",
 662:                                           "Webpage",
 663:                                           "B'day"
 664:                                          };
 665:  
 666:  
 667:  
 668:                     Object data[][]= new Object[v.size()][8];
 669:  
 670:  
 671:                     for(int j=0;j<v.size();j++)
 672:                             {
 673:  
 674:                                  con = (Contact) v.elementAt(k);
 675:  
 676:                                  data[j][0] = con.getFName();
 677:                                  data[j][1] = con.getLName();
 678:                                  data[j][2] = con.getNname();
 679:                                  data[j][3] = con.getEMail();
 680:                                  data[j][4] = con.getAddress();
 681:                                  data[j][5] = con.getPhoneNo();
 682:                                  data[j][6] = con.getWebpage();
 683:                                  data[j][7] = con.getBday();
 684:  
 685:                                  k++;
 686:  
 687:                             }
 688:                      k=0;
 689:  
 690:                      JTable abtable = new JTable(data,columnNames);
 691:                      JScrollPane scrollPane = new JScrollPane(abtable);
 692:                      abtable.setPreferredScrollableViewportSize(new
 693: Dimension(500, 370));
 694:  
 695:                      JPanel pane = new JPanel();
 696:                      JLabel label = new JLabel("Contacts Currently In 
 697: The
 698: Address Book");
 699:                      pane.add(label);
 700:  
 701:  
 702: newFrame.getContentPane().add(pane,BorderLayout.SOUTH);
 703:                      newFrame.getContentPane().add(scrollPane,
 704: BorderLayout.CENTER);
 705:                      newFrame.setLocation(screenWidth/4, 
 706: screenHeight/4);
 707:                      newFrame.show();
 708:  
 709:  
 710:  
 711:  
 712:            }
 713:  
 714:        public void showHelp() throws IOException
 715:            {
 716:  
 717:                 String title = "Help Contents";
 718:                 String data = "";
 719:                 FileInputStream fishelp = null;
 720:                 int i;
 721:  
 722:                 newFrame = new JFrame(title);
 723:                 newFrame.setSize(401, 400);
 724:                 newFrame.setResizable(false);
 725:                 newFrame.setLocation(screenWidth/4, screenHeight/4);
 726:                 newFrame.setIconImage(img);
 727:  
 728:                 JTextArea textArea = new JTextArea(5,20);
 729:                 textArea.setLineWrap(true);
 730:                 textArea.setEditable(false);
 731:  
 732:                 try
 733:                   {
 734:                         fishelp= new FileInputStream("help/help.txt");
 735:                   }
 736:                 catch(Exception e)
 737:                   {
 738:                         JOptionPane.showMessageDialog(newFrame, "Help File
 739: Not Found.", "Help File Not Found", JOptionPane.INFORMATION_MESSAGE);
 740:                   }
 741:  
 742:                 do
 743:                  {
 744:                       i = fishelp.read();
 745:                       if(i!=1)
 746:                           data = data + (char) i;
 747:                  } while(i!=-1);
 748:  
 749:                 fishelp.close();
 750:  
 751:                 textArea.setText(data);
 752:  
 753:                 JPanel bottomPane = new JPanel();
 754:                 JButton button = new JButton("Ok");
 755:                 bottomPane.add(button);
 756:                 button.addActionListener(this);
 757:  
 758:                 JPanel topPane = new JPanel();
 759:                 JLabel label = new JLabel(title);
 760:                 topPane.add(label);
 761:  
 762:                 JScrollPane scrollPane = new JScrollPane(textArea);
 763:  
 764:                 
 765: newFrame.getContentPane().add(topPane,BorderLayout.NORTH);
 766:                 newFrame.getContentPane().add(scrollPane);
 767:  
 768: newFrame.getContentPane().add(bottomPane,BorderLayout.SOUTH);
 769:  
 770:                 newFrame.show();
 771:  
 772:  
 773:            }
 774:  
 775:  
 776:        public void actionPerformed(ActionEvent ae)
 777:            {
 778:  
 779:  
 780:                  if(ae.getActionCommand() == "Add New!")
 781:                       {
 782:  
 783:                            if(txtFirstName.getText().equals("") &&
 784: txtLastName.getText().equals("") && txtNickname.getText().equals("") &&
 785: txtEMail.getText().equals("") && txtAddress.getText().equals("") &&
 786: txtPhoneNo.getText().equals("") && txtWebpage.getText().equals("") &&
 787: txtBDay.getText().equals(""))
 788:                                {
 789:                                     
 790: JOptionPane.showMessageDialog(newFrame,
 791: "Entries Empty! Fill in the required entries to save Contact.", 
 792: "Entries
 793: Empty", JOptionPane.INFORMATION_MESSAGE);
 794:  
 795:                                  }
 796:  
 797:                            else
 798:                               {
 799:                                   Contact contact = new Contact();
 800:  
 801: contact.setDetails(txtFirstName.getText(),txtLastName.getText(),txtNicknam
 802: e.getText(),txtEMail.getText(),txtAddress.getText(),txtPhoneNo.getText(),t
 803: xtWebpage.getText(),txtBDay.getText());
 804:                                   v.addElement(contact);
 805:                                   txtFirstName.setText("");
 806:                                   txtLastName.setText("");
 807:                                   txtNickname.setText("");
 808:                                   txtEMail.setText("");
 809:                                   txtAddress.setText("");
 810:                                   txtPhoneNo.setText("");
 811:                                   txtWebpage.setText("");
 812:                                   txtBDay.setText("");
 813:  
 814:                                   if(BttnSaveAdded.isEnabled() == false)
 815:                                        BttnSaveAdded.setEnabled(true);
 816:                              }
 817:  
 818:  
 819:  
 820:                       }
 821:                   else if(ae.getActionCommand() == "Save Added!")
 822:                       {
 823:  
 824:                            saveVector();
 825:                            newFrame.setVisible(false);
 826:  
 827:  
 828:                       }
 829:                    else if(ae.getActionCommand() == "Ok")
 830:                       {
 831:                            newFrame.setVisible(false);
 832:  
 833:                       }
 834:                    else if(ae.getActionCommand() == "Delete Selected")
 835:                       {
 836:  
 837:                            int index;
 838:                            try
 839:                              {
 840:  
 841:                                 index = list.getSelectedIndex();
 842:  
 843:                                 if(index==-1)
 844:                                   {
 845:  
 846: JOptionPane.showMessageDialog(newFrame, "Select a Contact first to 
 847: delete
 848: it.", "Select a Contact First", JOptionPane.INFORMATION_MESSAGE);
 849:                                }
 850:  
 851:                              else
 852:                               {
 853:  
 854:                                       int n =
 855: JOptionPane.showConfirmDialog(newFrame, "Are you sure you want to 
 856: delete
 857: the selected Contact?", "Are you sure?", JOptionPane.YES_NO_OPTION);
 858:  
 859:  
 860:                                     if (n == JOptionPane.YES_OPTION)
 861:                                         {
 862:                                                listModel.remove(index);
 863:                                               v.removeElementAt(index);
 864:                                                 saveVector();
 865:                                                 newFrame.show();
 866:  
 867:  
 868:                                         }
 869:                                      else if (n == 
 870: JOptionPane.NO_OPTION)
 871:                                         {
 872:  
 873:                                         }
 874:  
 875:                                  }
 876:  
 877:                               }
 878:                             catch(Exception e)
 879:                               {
 880:  
 881:                               }
 882:  
 883:                       }
 884:                    else if(ae.getActionCommand() == "Cancel")
 885:                       {
 886:  
 887:                            newFrame.setVisible(false);
 888:                       }
 889:                    else if(ae.getActionCommand() == "Search!")
 890:                       {
 891:                            String SearchStr;
 892:                            SearchStr = txtSearch.getText();
 893:                            boolean flag=false;
 894:                            Contact con = new Contact();
 895:                            int c=0;
 896:  
 897:                            for(int t=0;t<5;t++)
 898:                               {
 899:                                      data[t][0] = "";
 900:                                   data[t][1] = "";
 901:                                   data[t][2] = "";
 902:                                   data[t][3] = "";
 903:                                   data[t][4] = "";
 904:                                   data[t][5] = "";
 905:                                   data[t][6] = "";
 906:                                   data[t][7] = "";
 907:                               }
 908:  
 909:                         for(int t=0;t<v.size();t++)
 910:                             {
 911:  
 912:                                  con = (Contact) v.elementAt(t);
 913:  
 914:  
 915: if(SearchStr.equalsIgnoreCase(con.getFName()) ||
 916: SearchStr.equalsIgnoreCase(con.getLName()) ||
 917: SearchStr.equalsIgnoreCase(con.getFName() + " " + con.getLName()))
 918:                                     {
 919:                                         flag=true;
 920:  
 921:                                         data[c][0] = con.getFName();
 922:                                         data[c][1] = con.getLName();
 923:                                         data[c][2] = con.getNname();
 924:                                         data[c][3] = con.getEMail();
 925:                                         data[c][4] = con.getAddress();
 926:                                         data[c][5] = con.getPhoneNo();
 927:                                         data[c][6] = con.getWebpage();
 928:                                         data[c][7] = con.getBday();
 929:                                         searchTable = new
 930: JTable(data,columnNames);
 931:                                         newFrame.setSize(700,221);
 932:                                         newFrame.setSize(700,220);
 933:  
 934:                                         if(c<5)
 935:                                              c++;
 936:                                     }
 937:  
 938:                             }
 939:  
 940:  
 941:                             if(flag)
 942:                                {
 943:                                   JOptionPane.showMessageDialog(newFrame,
 944: "Contact Found!", "Search Result!", JOptionPane.INFORMATION_MESSAGE);
 945:                                }
 946:  
 947:                             else
 948:                                {
 949:                                   JOptionPane.showMessageDialog(newFrame, 
 950: "No
 951: Such Contact Found!", "Search Result!", 
 952: JOptionPane.INFORMATION_MESSAGE);
 953:                                }
 954:  
 955:  
 956:                       }
 957:                    else if(ae.getActionCommand() == "Sort Contacts")
 958:                       {
 959:  
 960:                      if(byfname.isSelected())
 961:                        {
 962:                            Contact contact1 = new Contact();
 963:                            Contact contact2 = new Contact();
 964:                            Contact temp = new Contact();
 965:                            int l,m;
 966:  
 967:                            for(l=0;l<v.size()-1;l++)
 968:                              {
 969:                                for(m=l+1;m<v.size();m++)
 970:                                  {
 971:                                      contact1 = (Contact) v.elementAt(l);
 972:                                       contact2 = (Contact) v.elementAt(m);
 973:  
 974:  
 975: if(contact1.getFName().compareTo(contact2.getFName()) > 0)
 976:                                          {
 977:                                             temp = (Contact) 
 978: v.elementAt(m);
 979:                                             
 980: v.setElementAt(v.elementAt(l),m);
 981:                                             v.setElementAt(temp,l);
 982:                                          }
 983:  
 984:                                  }
 985:                               }
 986:  
 987:                            saveVector();
 988:                            }
 989:                          else
 990:                            {
 991:  
 992:                            Contact contact1 = new Contact();
 993:                            Contact contact2 = new Contact();
 994:                            Contact temp = new Contact();
 995:                            int l,m;
 996:  
 997:                            for(l=0;l<v.size()-1;l++)
 998:                              {
 999:                                for(m=l+1;m<v.size();m++)
1000:                                  {
1001:                                      contact1 = (Contact) v.elementAt(l);
1002:                                       contact2 = (Contact) v.elementAt(m);
1003:  
1004:  
1005: if(contact1.getLName().compareTo(contact2.getLName()) > 0)
1006:                                          {
1007:                                             temp = (Contact) 
1008: v.elementAt(m);
1009:                                             
1010: v.setElementAt(v.elementAt(l),m);
1011:                                             v.setElementAt(temp,l);
1012:                                          }
1013:  
1014:                                  }
1015:                               }
1016:  
1017:                            saveVector();
1018:                            }
1019:  
1020:                          newFrame.setVisible(false);
1021:                      }
1022:  
1023:  
1024:            }
1025:  
1026:  
1027:        public void saveVector()
1028:            {
1029:                      t = new Thread(this, "Save Vector Thread");
1030:                   t.start();
1031:  
1032:            }
1033:  
1034:  
1035:        public void valueChanged(ListSelectionEvent lse)
1036:            {
1037:  
1038:  
1039:            }
1040:  
1041:     }

No comments

please write your mail id for contact: