Contacts Project : Main Window




package conpack;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;



import javax.swing.DefaultListModel;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class windows extends JFrame implements ActionListener,ListSelectionListener{
     
      JTextField name,number;
      JButton find;
      JLabel lname,lnumber;
      JList list;
    DefaultListModel model ;

     
    public windows() {
      setLayout(null);
     
      model = new DefaultListModel();   
      list = new JList(model);
      list.setBounds(100,70,100,100);
      add(list);
     
      // Initialize the list with items
      list.addListSelectionListener(this);
     
        setTitle("Contacts");
        setSize(300, 300);
            
        name = new JTextField(5);
        name.setBounds(100,20,100,20);
        add(name);
       
        number = new JTextField(5);
        number.setBounds(180,200,100,20);
        add(number);
       
        find = new JButton("Find");
        find.setBounds(20,200,100,20);
        find.addActionListener(this);
        add(find);
       

        // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
       
        // Add the menubar to the frame
        setJMenuBar(menuBar);
       
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("Options");
      
        menuBar.add(fileMenu);
              
        // Create and add simple menu item to one of the drop down menu
        JMenuItem newAction = new JMenuItem("Add Contact");

        JMenuItem editAction = new JMenuItem("Edit");
        JMenuItem deleteAction = new JMenuItem("Delete");
        JMenuItem copyAction = new JMenuItem("Copy");
        // JMenuItem moveAction = new JMenuItem("Move");
        JMenuItem exitAction = new JMenuItem("Exit");
       

        fileMenu.add(newAction);
        fileMenu.add(editAction);
        fileMenu.add(deleteAction);
  
        fileMenu.add(copyAction);
        //  fileMenu.add(moveAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
     

        // Add a listener to the New menu item. actionPerformed() method will
        // invoked, if user triggred this menu item
       
        try{
                   
                  Class.forName("oracle.jdbc.driver.OracleDriver");
                  Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                 
                 
                  Statement st=con.createStatement();
                  String str = name.getText();
                  //ResultSet rs=st.executeQuery("select num from contacts where nam = '"+str+"'");
                  ResultSet rs=st.executeQuery("select * from contacts");
                int i = 0;
                  while(rs.next()){

                model.add(i,rs.getString("nam"));
                i++;

                  }
                 
              }
            catch(Exception e1)
            {
                  System.out.println(e1);
            }
       
        newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the new action");
                dispose();
                AddContact c = new AddContact();
                c.setSize(300,300);
                c.setVisible(true);
                c.setLocation(500, 400);
            }
        });
       
        editAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the edit action");
                dispose();
                EditContact c = new EditContact();
                c.setSize(300,300);
                c.setVisible(true);c.setLocation(500, 400);
            }
        });
       
        deleteAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the delete action");
                dispose();
               DeleteContact c = new DeleteContact();
                c.setSize(300,300);
                c.setVisible(true);c.setLocation(500, 400);
               
            }
        });
       
        copyAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the copy action");
                dispose();
               CopyContact c = new CopyContact();
                c.setSize(300,300);
                c.setVisible(true);c.setLocation(500, 400);
            }
        });
       
        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               
                dispose(); // close
            
            }
        });
           
    }
   
        public void valueChanged(ListSelectionEvent e) {

                   try{
                         
                              Class.forName("oracle.jdbc.driver.OracleDriver");
                              Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                             
                             
                       Statement st=con.createStatement();
                       String str = list.getSelectedValue().toString();
ResultSet rs=st.executeQuery("select num from contacts where nam = '"+str+"'");
                              // may table name is contacts
                              // column names are num , nam and grp
                           
                              while(rs.next()){
                       
                                    number.setText(rs.getString(1));
                              }

                          }
                        catch(Exception e1)
                        {
                              System.out.println(e1);
                        }
                 
            }
   
   public void actionPerformed(ActionEvent e) {
             if(e.getSource()== find )
             {

              try{
                   
                  Class.forName("oracle.jdbc.driver.OracleDriver");
                  Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
                 
                 
                  Statement st=con.createStatement();
                  String str = name.getText();
ResultSet rs=st.executeQuery("select num from contacts where nam = '"+str+"'");
                 
           
                  while(rs.next()){
                       
                        number.setText(rs.getString(1).toString());
                       
                  }
                 
           

              }
            catch(Exception e1)
                {
                  System.out.println(e1);
                }
             }
       
           
    }
  
   
    public static void main (String[] args)
    {
        windows me = new windows();
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.setVisible(true);
        me.setLocation(500, 400);

    }
}







Post a Comment

Copyright © Rough Record. Designed by OddThemes