Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

phone directory

Thu Mar 05, 2009 11:16 pm

hi,
iam trying to run program a simulation to cellphone with addressbook from database. my idea is that i try to call names and numbers from a database (Access) to show them on a JList then from this list i can press any name to be displayed on a JTextField.but this program doesnot run ??? pls help.
Code:
/**
   This program simulates a cellphone with a phone directory.

     */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ListSelectionModel;
import java.sql.*;

public class MobileDB extends JFrame  {

        private final String WINDOW_TITLE = "CellPhone Simulation";
   private final int WINDOW_WIDTH = 270;
   private final int WINDOW_HEIGHT = 300;
        String input="";
   private DefaultListModel listmodel;
        private JList list= new JList();
       
     
   
        private final int MAX_CONTACTS=20;
        ContactsInfo[] contacts= new ContactsInfo [MAX_CONTACTS];
       
        private int contactsInitial=0;
     

    private JPanel textPanel = new JPanel();
    private JPanel digitPanel = new JPanel();
    private JPanel buttonPanel1 = new JPanel();
    private JPanel buttonPanel2 = new JPanel();
    private JPanel listPanel = new JPanel();
   
   

    private JLabel displayLabel = new JLabel ("                                                                                NOKIA ");
   
    private JTextField displayTextField = new JTextField(20);
   
   
    private JButton sendButton = new JButton("Send");
    private JButton clearButton = new JButton("Clear");
    private JButton endButton = new JButton(" End ");
    private JButton redialButton = new JButton("Redial");
   
    private JButton Button1 = new JButton("1");
    private JButton Button2 = new JButton("2");
    private JButton Button3 = new JButton("3");
    private JButton Button4 = new JButton("4");
    private JButton Button5 = new JButton("5");
    private JButton Button6 = new JButton("6");
    private JButton Button7 = new JButton("7");
    private JButton Button8 = new JButton("8");
    private JButton Button9 = new JButton("9");
    private JButton Button0 = new JButton("0");
    private JButton astrButton = new JButton("*");
    private JButton boundButton = new JButton("#");
   


   

    public static void main(String[] args) {
           new MobileDB();
      
      
    }


/**
   Build the GUI
*/

   public MobileDB() {

      setTitle(WINDOW_TITLE);
                setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                buildTextPanel();
                buildButtonPanel1();
                buildButtonPanel2();
                buildDigitPanel();
                buildListPanel();
                setLayout(new BorderLayout());
                add(textPanel,BorderLayout.NORTH);
                add(buttonPanel1,BorderLayout.EAST);
                add(buttonPanel2,BorderLayout.WEST);
                add(listPanel,BorderLayout.CENTER);
                add(digitPanel,BorderLayout.SOUTH);
               
                clearDisplay();
                setVisible(true);

   }

/**
    The buildTextPanel method creates a panel to hold the label and the textfield.
*/
         public void buildTextPanel () {
      textPanel.setLayout(new BorderLayout());
      textPanel.add(displayLabel,BorderLayout.NORTH);
      textPanel.add(displayTextField,BorderLayout.CENTER);
                textPanel.setBackground(Color.RED);
   }
/**
      The buildButtonPanel1 method creates a panel to hold the send and clear buttons.
*/
       
         public void buildButtonPanel1 () {
      buttonPanel1.setLayout(new GridLayout(2,1));
      buttonPanel1.add(sendButton);
      buttonPanel1.add(clearButton);
                sendButton.addActionListener(new SendButtonListener());
      clearButton.addActionListener(new ClearButtonListener());
                sendButton.setForeground(Color.GREEN);
                clearButton.setForeground(Color.RED);
                buttonPanel1.setBorder(BorderFactory.createRaisedBevelBorder());
                buttonPanel1.setBackground(Color.RED);
        }
/**
      The buildButtonPanel1 method creates a panel to hold the end and redial buttons.
*/
         public void buildButtonPanel2 () {
                buttonPanel2.setLayout(new GridLayout(2,1));
      buttonPanel2.add(endButton);
                buttonPanel2.add(redialButton);
                endButton.addActionListener(new EndButtonListener());
                redialButton.addActionListener(new RedialButtonListener());
                endButton.setForeground(Color.RED);
                redialButton.setForeground(Color.GREEN);
                buttonPanel2.setBorder(BorderFactory.createRaisedBevelBorder());
                buttonPanel2.setBackground(Color.RED);
        }       
/**
      The buildListPanel1 method creates a panel to hold the list and its elements.
*/
      
   public void buildListPanel() {
              listPanel.add(list);
              list.setSelectedIndex(0);
              list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              list.addListSelectionListener(new ListListener());   
              listPanel.setBorder(BorderFactory.createRaisedBevelBorder());
              listPanel.setBackground(Color.RED);
      }   
         

/**
      The buildDigitPanel method creates a panel to hold the digit buttons.
*/
         public void buildDigitPanel() {
      digitPanel.setLayout(new GridLayout(4,3));
      digitPanel.add(Button1);
      digitPanel.add(Button2);
                digitPanel.add(Button3);
                digitPanel.add(Button4);
                digitPanel.add(Button5);
                digitPanel.add(Button6);
                digitPanel.add(Button7);
                digitPanel.add(Button8);
                digitPanel.add(Button9);
                digitPanel.add(astrButton);
                digitPanel.add(Button0);
                digitPanel.add(boundButton);
                digitPanel.setBorder(BorderFactory.createRaisedBevelBorder());
                digitPanel.setBackground(Color.RED);

/** create innerlistener to all buttons, then add all buttons to
    this listener.
*/


        InnerListener listener= new InnerListener();
                Button1.addActionListener(listener);
                Button2.addActionListener(listener);
                Button3.addActionListener(listener);
                Button4.addActionListener(listener);
                Button5.addActionListener(listener);
                Button6.addActionListener(listener);
                Button7.addActionListener(listener);
                Button8.addActionListener(listener);
                Button9.addActionListener(listener);
                Button0.addActionListener(listener);
                astrButton.addActionListener(listener);
                boundButton.addActionListener(listener);
   }







/**
     SendButtonListener is an action listener class for
      the Send button.
*/

      private class SendButtonListener implements ActionListener {

/**
      The actionPerformed method executes when the user clicks on the send button.
      @param e The event object.
*/
       public void actionPerformed(ActionEvent ev) {

              input= displayTextField.getText();
             sendCalling();

      }
   }

/**
     ClearButtonListener is an action listener class for
      the Clear button.
*/     
      private class ClearButtonListener implements ActionListener {

/**
     The actionPerformed method executes when the user clicks on the clear button.
      @param e The event object.
*/

      public void actionPerformed(ActionEvent ev) {

             clearDisplay();
      }
   }

/**
    EndButtonListener is an action listener class for the Exit button.
*/
     private class EndButtonListener implements ActionListener {


/**
    The actionPerformed method executes when the user clicks on the end button.
     @param e The event object.
*/
     public void actionPerformed(ActionEvent ev) {

             endCalling();
      }
   }

/**
    RedialButtonListener is an action listener class for the Redial button.
*/
     private class RedialButtonListener implements ActionListener {

/**
    The actionPerformed method executes when the user clicks on the redial button.
     @param e The event object.
*/
     public void actionPerformed(ActionEvent ev) {
                 
                    redialCalling();
                 }
        }


     
/**
    The clearDisplay method will clear the digit,send,end and redial fields.
*/

      public void clearDisplay() {
                input="";
      displayTextField.setText(" ");

   }

/**
    The sendCalling method will dislay a message when calling.
*/
        public void sendCalling() {
      displayTextField.setText("Calling " + input);

   }
/**
    The endCalling method will dislay a message when call is ended.
*/
        public void endCalling() {
      displayTextField.setText("Call Ended");

   }
/**
    The redialCalling method will dislay a message when number is redialing.
*/
        public void redialCalling() {
      displayTextField.setText("Redialing " + input);

   }

        public class ContactsInfo {
           private String Names;
           private String Numbers;
       
        public ContactsInfo() {
            Names=("");
            Numbers=("");
        }
       
       
         public void setNames(String newNames)  {       
                this.Names=newNames;
            }
           
            public void setNumbers(String newNumbers)  {         
                this.Numbers=newNumbers;
           }
       }
         

       
     private class ListListener implements ListSelectionListener{           
       public void valueChanged(ListSelectionEvent ev) {
     
        String selection= (String)list.getSelectedValue();
        displayTextField.setText(selection);
     }
   }
       

        private class InnerListener implements ActionListener {

        public void actionPerformed(ActionEvent ev) {
                 
                  if(ev.getSource()==Button1) {
                    displayTextField.setText(input +"1");
                      input= input+"1";
                     
                  }
                   else if (ev.getSource()==Button2) {
                     displayTextField.setText(input +"2");
                      input= input+"2";
                     
                   }
                   else if (ev.getSource()==Button3) {
                     displayTextField.setText(input +"3");
                      input=input +"3";
                     
                   }
                   else if (ev.getSource()==Button4) {
                     displayTextField.setText(input +"4");
                       input=input +"4";
                     
                   }
                   else if (ev.getSource()==Button5) {
                     displayTextField.setText(input +"5");
                      input=input +"5";
                     
                   }
                   else if (ev.getSource()==Button6) {
                     displayTextField.setText(input +"6");
                      input=input +"6";
                     
                   }
                   else if (ev.getSource()==Button7) {
                     displayTextField.setText(input +"7");
                      input=input +"7";
                     
                   }
                   else if (ev.getSource()==Button8) {
                     displayTextField.setText(input +"8");
                      input=input +"8";
                     
                   }
                   else if (ev.getSource()==Button9) {
                     displayTextField.setText(input +"9");
                      input=input +"9";
                     
                   }
                   else if (ev.getSource()==Button0) {
                     displayTextField.setText(input +"0");
                       input=input +"0";
                     
                   }
                   else if (ev.getSource()==astrButton) {
                     displayTextField.setText(input +"*");
                       input=input +"*";
                     
                   }
                   else if (ev.getSource()==boundButton) {
                     displayTextField.setText(input +"#");
                      input=input +"#";
                     
                   }
                 
         
              for (int x=0; x<=MAX_CONTACTS-1; x++) {
                  contacts[x]=new ContactsInfo();
              }
             
              try {     
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              }
              catch (ClassNotFoundException e) {
           System.err.println("Can't find JDBC to ODBC Bridge");
              }
           
              try {
                  Connection conn = DriverManager.getConnection
                                    ("jdbc:odbc:addressesdb");
                  Statement stmt=conn.createStatement(); 
                  ResultSet rset = stmt.executeQuery("SELECT * from info");

           

                    while (rset.next ()==true)
                    contacts[contactsInitial].setNames(rset.getString("Names"));
                    contacts[contactsInitial].setNumbers(rset.getString("Numbers"));
                    contactsInitial++;
                   
                   
                         
               rset.close();
               stmt.close();
               conn.close();
              }
               catch(SQLException e) {
                System.err.println("An SQL exception occurred while trying to                                          connect to the server: " +e.getMessage());
           }

          }                 

       }       
         
     }
   





Post a reply
  Related Posts  to : phone directory
 Creating a Directory in php     -  
 Delete directory in php     -  
 php Directory lister     -  
 Listing the Contents of a Directory     -  
 Print all files in a directory     -  
 invalid drive or directory     -  
 Read the content from directory     -  
 Get Directory size in java     -  
 Upload File to a new directory     -  
 PHP Phone Book     -