473,416 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Help regarding JList Netbeans6.9 IDE

good day guys,

please help:

i'm having trouble of displaying my data from ms access database to JList. if there's someone could help me, i would greatly appreciate it. i'm using netbeans 6.0 for developing an application. here is my code so far:

note: some of my code is auto generated by netbeans JDK.

Expand|Select|Wrap|Line Numbers
  1. /*TO GOD BE THE GLORY! I CAN DO ALL THINGS THROUGH CHRIST WHO STRENGHTENS ME!
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * MainForm.java
  8.  *
  9.  * Created on Feb 4, 2011, 8:41:45 AM
  10.  */
  11.  
  12. package UsernamePasswordPackage;
  13. import java.awt.Color;
  14. import java.sql.*;
  15. import javax.swing.*;
  16. /**
  17.  *
  18.  * @author jmoreno
  19.  */
  20. public class MainForm extends javax.swing.JFrame {
  21.     /** Creates new form MainForm */
  22.         DefaultListModel list = new DefaultListModel();
  23.         public MainForm() {
  24.         this.setTitle("Username & Password Manager");
  25.  
  26.         initComponents();
  27.  
  28.         jButton1.requestFocus();
  29.         jButton1.setEnabled(true);
  30.         jButton2.setEnabled(true);
  31.         jButton3.setEnabled(false);
  32.         jButton4.setEnabled(false);
  33.         jButton5.setEnabled(true);
  34.         jTextField1.setEnabled(false);
  35.         jTextField2.setEnabled(false);
  36.         jTextField3.setEnabled(false);
  37.         jList1.clearSelection();
  38.         jButton5.setBackground(Color.getHSBColor(0, 217, 114));
  39.         //PopulatejList();
  40.     }
  41.  
  42.     /** This method is called from within the constructor to
  43.      * initialize the form.
  44.      * WARNING: Do NOT modify this code. The content of this method is
  45.      * always regenerated by the Form Editor.
  46.      */
  47.     @SuppressWarnings("unchecked")
  48.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  49.     private void initComponents() {
  50.  
  51.         jPanel1 = new javax.swing.JPanel();
  52.         jScrollPane1 = new javax.swing.JScrollPane();
  53.         jList1 = new javax.swing.JList();
  54.         jLabel1 = new javax.swing.JLabel();
  55.         jTextField1 = new javax.swing.JTextField();
  56.         jTextField2 = new javax.swing.JTextField();
  57.         jLabel2 = new javax.swing.JLabel();
  58.         jLabel3 = new javax.swing.JLabel();
  59.         jTextField3 = new javax.swing.JTextField();
  60.         jButton1 = new javax.swing.JButton();
  61.         jButton2 = new javax.swing.JButton();
  62.         jButton3 = new javax.swing.JButton();
  63.         jButton4 = new javax.swing.JButton();
  64.         jButton5 = new javax.swing.JButton();
  65.  
  66.         setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  67.         setResizable(false);
  68.  
  69.         jList1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
  70.         jList1.addMouseListener(new java.awt.event.MouseAdapter() {
  71.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  72.                 jList1MouseClicked(evt);
  73.             }
  74.         });
  75.         jScrollPane1.setViewportView(jList1);
  76.  
  77.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 16));
  78.         jLabel1.setText("Username :");
  79.  
  80.         jTextField1.setFont(new java.awt.Font("Tahoma", 0, 16));
  81.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  82.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  83.                 jTextField1ActionPerformed(evt);
  84.             }
  85.         });
  86.         jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
  87.             public void keyPressed(java.awt.event.KeyEvent evt) {
  88.                 jTextField1KeyPressed(evt);
  89.             }
  90.         });
  91.  
  92.         jTextField2.setFont(new java.awt.Font("Tahoma", 0, 16));
  93.         jTextField2.addKeyListener(new java.awt.event.KeyAdapter() {
  94.             public void keyPressed(java.awt.event.KeyEvent evt) {
  95.                 jTextField2KeyPressed(evt);
  96.             }
  97.         });
  98.  
  99.         jLabel2.setFont(new java.awt.Font("Tahoma", 1, 16));
  100.         jLabel2.setText("Password :");
  101.  
  102.         jLabel3.setFont(new java.awt.Font("Tahoma", 1, 16));
  103.         jLabel3.setText("Website :");
  104.  
  105.         jTextField3.setFont(new java.awt.Font("Tahoma", 0, 16));
  106.         jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
  107.             public void keyPressed(java.awt.event.KeyEvent evt) {
  108.                 jTextField3KeyPressed(evt);
  109.             }
  110.         });
  111.  
  112.         jButton1.setText("Add");
  113.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  114.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  115.                 jButton1ActionPerformed(evt);
  116.             }
  117.         });
  118.         jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
  119.             public void keyPressed(java.awt.event.KeyEvent evt) {
  120.                 jButton1KeyPressed(evt);
  121.             }
  122.         });
  123.  
  124.         jButton2.setText("Edit");
  125.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  126.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  127.                 jButton2ActionPerformed(evt);
  128.             }
  129.         });
  130.         jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
  131.             public void keyPressed(java.awt.event.KeyEvent evt) {
  132.                 jButton2KeyPressed(evt);
  133.             }
  134.         });
  135.  
  136.         jButton3.setText("Save");
  137.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  138.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  139.                 jButton3ActionPerformed(evt);
  140.             }
  141.         });
  142.         jButton3.addKeyListener(new java.awt.event.KeyAdapter() {
  143.             public void keyPressed(java.awt.event.KeyEvent evt) {
  144.                 jButton3KeyPressed(evt);
  145.             }
  146.         });
  147.  
  148.         jButton4.setText("Cancel");
  149.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  150.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  151.                 jButton4ActionPerformed(evt);
  152.             }
  153.         });
  154.         jButton4.addKeyListener(new java.awt.event.KeyAdapter() {
  155.             public void keyPressed(java.awt.event.KeyEvent evt) {
  156.                 jButton4KeyPressed(evt);
  157.             }
  158.         });
  159.  
  160.         jButton5.setText("Exit");
  161.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  162.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  163.                 jButton5ActionPerformed(evt);
  164.             }
  165.         });
  166.         jButton5.addKeyListener(new java.awt.event.KeyAdapter() {
  167.             public void keyPressed(java.awt.event.KeyEvent evt) {
  168.                 jButton5KeyPressed(evt);
  169.             }
  170.         });
  171.  
  172.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  173.         jPanel1.setLayout(jPanel1Layout);
  174.         jPanel1Layout.setHorizontalGroup(
  175.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  176.             .addGroup(jPanel1Layout.createSequentialGroup()
  177.                 .addContainerGap()
  178.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  179.                     .addGroup(jPanel1Layout.createSequentialGroup()
  180.                         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
  181.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  182.                         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  184.                         .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  185.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  186.                         .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
  187.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  188.                         .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
  189.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  190.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
  191.                         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
  192.                             .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.                                 .addComponent(jLabel2)
  194.                                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
  195.                                 .addComponent(jLabel3))
  196.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  197.                             .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  198.                                 .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  199.                                 .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
  200.                                 .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
  201.                 .addContainerGap(20, Short.MAX_VALUE))
  202.         );
  203.  
  204.         jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField1, jTextField2, jTextField3});
  205.  
  206.         jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3});
  207.  
  208.         jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jButton3, jButton4, jButton5});
  209.  
  210.         jPanel1Layout.setVerticalGroup(
  211.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  212.             .addGroup(jPanel1Layout.createSequentialGroup()
  213.                 .addContainerGap()
  214.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
  215.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  216.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  217.                     .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  218.                     .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
  219.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  220.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  221.                     .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  222.                     .addComponent(jLabel2))
  223.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  224.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  225.                     .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  226.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
  227.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  228.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  229.                     .addComponent(jButton1)
  230.                     .addComponent(jButton2)
  231.                     .addComponent(jButton3)
  232.                     .addComponent(jButton4)
  233.                     .addComponent(jButton5))
  234.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  235.         );
  236.  
  237.         jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jTextField1, jTextField2, jTextField3});
  238.  
  239.         jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3});
  240.  
  241.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  242.         getContentPane().setLayout(layout);
  243.         layout.setHorizontalGroup(
  244.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  245.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  246.         );
  247.         layout.setVerticalGroup(
  248.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  250.         );
  251.  
  252.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  253.         setBounds((screenSize.width-405)/2, (screenSize.height-396)/2, 405, 396);
  254.     }// </editor-fold>
  255.  
  256.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  257.         // TODO add your handling code here:
  258.         int choose = JOptionPane.showConfirmDialog(null,"Continue to add new record?","Add New Record",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  259.         if(choose == JOptionPane.OK_OPTION){
  260.            jButton1.setEnabled(false);
  261.            jButton2.setEnabled(false);
  262.            jButton3.setEnabled(true);
  263.            jButton4.setEnabled(true);
  264.            jButton5.setEnabled(false);
  265.            jTextField1.setEnabled(true);
  266.            jTextField2.setEnabled(true);
  267.            jTextField3.setEnabled(true);
  268.            jTextField1.requestFocus();
  269.         }else if(choose == JOptionPane.CANCEL_OPTION){
  270.            jButton1.setEnabled(true);
  271.            jButton2.setEnabled(true);
  272.            jButton3.setEnabled(false);
  273.            jButton4.setEnabled(false);
  274.            jButton5.setEnabled(true);
  275.            jTextField1.setEnabled(false);
  276.            jTextField2.setEnabled(false);
  277.            jTextField3.setEnabled(false);
  278.            jButton1.requestFocus();
  279.         }
  280.     }                                        
  281.  
  282.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  283.         // TODO add your handling code here:
  284.         JOptionPane.showMessageDialog(null,"You have been cancelled the adding of new record/s.");
  285.         jButton1.setEnabled(true);
  286.         jButton2.setEnabled(true);
  287.         jButton3.setEnabled(false);
  288.         jButton4.setEnabled(false);
  289.         jButton5.setEnabled(true);
  290.         jTextField1.setText("");
  291.         jTextField2.setText("");
  292.         jTextField3.setText("");
  293.         jTextField1.setEnabled(false);
  294.         jTextField2.setEnabled(false);
  295.         jTextField3.setEnabled(false);
  296.     }                                        
  297.  
  298.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  299.         // TODO add your handling code here:
  300.         int choose = JOptionPane.showConfirmDialog(null,"Continue to exit?.","Exit",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  301.         if(choose == JOptionPane.YES_OPTION){
  302.             this.dispose();
  303.         }else if(choose == JOptionPane.NO_OPTION){
  304.             jButton1.requestFocus();
  305.             return;
  306.         }
  307.     }                                        
  308.  
  309.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  310.         // TODO add your handling code here:
  311.         if(jTextField1.getText().isEmpty()){
  312.            JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  313.            if(jTextField1.getText().isEmpty()){
  314.               jTextField1.requestFocus();
  315.            }
  316.            return;
  317.         }
  318.         else if(jTextField2.getText().isEmpty()){
  319.            JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  320.            if(jTextField2.getText().isEmpty()){
  321.               jTextField2.requestFocus();
  322.            }
  323.            return;
  324.         }
  325.         else if(jTextField3.getText().isEmpty()){
  326.            JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  327.            if(jTextField3.getText().isEmpty()){
  328.               jTextField3.requestFocus();
  329.            }
  330.            return;
  331.         }
  332.  
  333.         int choose = JOptionPane.showConfirmDialog(null,"Continue to save new record?","Confirmation",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  334.         if(choose == JOptionPane.YES_OPTION){
  335.  
  336.            String text1 = jTextField1.getText();
  337.            String text2 = jTextField2.getText();
  338.            String text3 = jTextField3.getText();
  339.  
  340.            String dbuser = "";
  341.            String dbpasswd = "";
  342.            String DriverPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb";
  343.  
  344.            String SQLString = "INSERT INTO Table1(Username,Password,Website)VALUES('"+text1+"','"+text2+"','"+text3+"')";
  345.  
  346.            try{
  347.               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  348.            }
  349.            catch(Exception e){
  350.               JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC DRIVER Error",JOptionPane.WARNING_MESSAGE);
  351.               return;
  352.            }
  353.            Statement stmt = null;
  354.            Connection con = null;
  355.  
  356.            try{
  357.               //con = DriverManager.getConnection("jdbc:odbc:db1.mdb");
  358.               //con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
  359.               con = DriverManager.getConnection(DriverPrefix,dbuser,dbpasswd);
  360.               stmt = con.createStatement();
  361.            }
  362.            catch(Exception e){
  363.               JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
  364.            }
  365.  
  366.            try{
  367.               stmt.executeUpdate(SQLString);
  368.               con.close();
  369.               jTextField1.setEnabled(false);
  370.               jTextField2.setEnabled(false);
  371.               jTextField3.setEnabled(false);
  372.               jButton1.setEnabled(true);
  373.               jButton2.setEnabled(true);
  374.               jButton3.setEnabled(false);
  375.               jButton4.setEnabled(false);
  376.               jButton5.setEnabled(true);
  377.               jButton1.requestFocus();
  378.            }
  379.            catch(SQLException e){
  380.               JOptionPane.showMessageDialog(null,"Check that all TextFields have been completed.\n"+e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
  381.            }
  382.         }
  383.         else{
  384.            jTextField1.setText("");
  385.            jTextField1.setEnabled(false);
  386.            jTextField2.setText("");
  387.            jTextField2.setEnabled(false);
  388.            jTextField3.setText("");
  389.            jTextField3.setEnabled(false);
  390.            jButton1.setEnabled(true);
  391.            jButton2.setEnabled(true);
  392.            jButton3.setEnabled(false);
  393.            jButton4.setEnabled(false);
  394.            jButton5.setEnabled(true);
  395.            jButton1.requestFocus();
  396.         }
  397.     }                                        
  398.  
  399.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  400.         // TODO add your handling code here:
  401.     }                                           
  402.  
  403.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  404.         // TODO add your handling code here:
  405.         jButton1.setEnabled(false);
  406.         jButton2.setEnabled(false);
  407.         jButton3.setEnabled(true);
  408.         jButton4.setEnabled(true);
  409.         jButton5.setEnabled(false);
  410.         jTextField1.setEnabled(true);
  411.         jTextField2.setEnabled(true);
  412.         jTextField3.setEnabled(true);
  413.         jTextField1.requestFocus();
  414.     }                                        
  415.  
  416.     private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
  417.         // TODO add your handling code here:
  418.         if(evt.getKeyCode()==10){
  419.             jTextField2.requestFocus();
  420.         }
  421.     }                                      
  422.  
  423.     private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {                                       
  424.         // TODO add your handling code here:
  425.         if(evt.getKeyCode()==10){
  426.             jTextField3.requestFocus();
  427.         }
  428.     }                                      
  429.  
  430.     private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {                                       
  431.         // TODO add your handling code here:
  432.         if(evt.getKeyCode()==10){
  433.             jButton3.requestFocus();
  434.         }
  435.     }                                      
  436.  
  437.     private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {                                    
  438.         // TODO add your handling code here:
  439.         if(evt.getKeyCode()==10){
  440.             int choose = JOptionPane.showConfirmDialog(null,"Continue to add new record?","Add New Record",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  441.             if(choose == JOptionPane.OK_OPTION){
  442.                jButton1.setEnabled(false);
  443.                jButton2.setEnabled(false);
  444.                jButton3.setEnabled(true);
  445.                jButton4.setEnabled(true);
  446.                jButton5.setEnabled(false);
  447.                jTextField1.setEnabled(true);
  448.                jTextField2.setEnabled(true);
  449.                jTextField3.setEnabled(true);
  450.                jTextField1.requestFocus();
  451.             }else if(choose == JOptionPane.CANCEL_OPTION){
  452.                jButton1.setEnabled(true);
  453.                jButton2.setEnabled(true);
  454.                jButton3.setEnabled(false);
  455.                jButton4.setEnabled(false);
  456.                jButton5.setEnabled(true);
  457.                jTextField1.setEnabled(false);
  458.                jTextField2.setEnabled(false);
  459.                jTextField3.setEnabled(false);
  460.                jButton1.requestFocus();
  461.            }
  462.         }
  463.     }                                   
  464.  
  465.     private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {                                    
  466.         // TODO add your handling code here:
  467.         if(evt.getKeyCode()==10){
  468.             jButton1.setEnabled(false);
  469.             jButton2.setEnabled(false);
  470.             jButton3.setEnabled(true);
  471.             jButton4.setEnabled(true);
  472.             jButton5.setEnabled(false);
  473.             jTextField1.setEnabled(true);
  474.             jTextField2.setEnabled(true);
  475.             jTextField3.setEnabled(true);
  476.             jTextField1.requestFocus();
  477.         }
  478.     }                                   
  479.  
  480.     private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {                                    
  481.         // TODO add your handling code here:
  482.         if(evt.getKeyCode()==10){
  483.             if(jTextField1.getText().isEmpty()){
  484.                 JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  485.                 if(jTextField1.getText().isEmpty()){
  486.                    jTextField1.requestFocus();
  487.                 }
  488.                 return;
  489.              }
  490.              else if(jTextField2.getText().isEmpty()){
  491.                  JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  492.                  if(jTextField2.getText().isEmpty()){
  493.                      jTextField2.requestFocus();
  494.                  }
  495.                  return;
  496.              }
  497.              else if(jTextField3.getText().isEmpty()){
  498.                  JOptionPane.showMessageDialog(null,"Please complete all necessary fields.","Required Fields",JOptionPane.OK_OPTION);
  499.                  if(jTextField3.getText().isEmpty()){
  500.                      jTextField3.requestFocus();
  501.                  }
  502.                  return;
  503.              }
  504.  
  505.              int choose = JOptionPane.showConfirmDialog(null,"Continue to save new record?","Confirmation",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  506.              if(choose == JOptionPane.YES_OPTION){
  507.  
  508.              String text1 = jTextField1.getText();
  509.              String text2 = jTextField2.getText();
  510.              String text3 = jTextField3.getText();
  511.  
  512.              String dbuser = "";
  513.              String dbpasswd = "";
  514.              String DriverPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb";
  515.  
  516.              String SQLString = "INSERT INTO Table1(Username,Password,Website)VALUES('"+text1+"','"+text2+"','"+text3+"')";
  517.  
  518.              try{
  519.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  520.              }
  521.              catch(Exception e){
  522.                 JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC DRIVER Error",JOptionPane.WARNING_MESSAGE);
  523.                 return;
  524.              }
  525.              Statement stmt = null;
  526.              Connection con = null;
  527.  
  528.              try{
  529.                 //con = DriverManager.getConnection("jdbc:odbc:db1.mdb");
  530.                 //con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
  531.                 con = DriverManager.getConnection(DriverPrefix,dbuser,dbpasswd);
  532.                 stmt = con.createStatement();
  533.              }
  534.              catch(Exception e){
  535.                 JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
  536.              }
  537.  
  538.              try{
  539.                 stmt.executeUpdate(SQLString);
  540.                 con.close();
  541.                 jTextField1.setEnabled(false);
  542.                 jTextField2.setEnabled(false);
  543.                 jTextField3.setEnabled(false);
  544.                 jButton1.setEnabled(true);
  545.                 jButton2.setEnabled(true);
  546.                 jButton3.setEnabled(false);
  547.                 jButton4.setEnabled(false);
  548.                 jButton5.setEnabled(true);
  549.                 jButton1.requestFocus();
  550.              }
  551.              catch(SQLException e){
  552.                 JOptionPane.showMessageDialog(null,"Check that all TextFields have been completed.\n"+e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
  553.              }
  554.           }
  555.           else{
  556.              jTextField1.setText("");
  557.              jTextField1.setEnabled(false);
  558.              jTextField2.setText("");
  559.              jTextField2.setEnabled(false);
  560.              jTextField3.setText("");
  561.              jTextField3.setEnabled(false);
  562.              jButton1.setEnabled(true);
  563.              jButton2.setEnabled(true);
  564.              jButton3.setEnabled(false);
  565.              jButton4.setEnabled(false);
  566.              jButton5.setEnabled(true);
  567.              jButton1.requestFocus();
  568.           }
  569.         }
  570.     }                                   
  571.  
  572.     private void jButton4KeyPressed(java.awt.event.KeyEvent evt) {                                    
  573.         // TODO add your handling code here:
  574.         if(evt.getKeyCode()==10){
  575.             JOptionPane.showMessageDialog(null,"You have been cancelled the adding of new record/s.");
  576.             jButton1.setEnabled(true);
  577.             jButton2.setEnabled(true);
  578.             jButton3.setEnabled(false);
  579.             jButton4.setEnabled(false);
  580.             jButton5.setEnabled(true);
  581.             jTextField1.setText("");
  582.             jTextField2.setText("");
  583.             jTextField3.setText("");
  584.             jTextField1.setEnabled(false);
  585.             jTextField2.setEnabled(false);
  586.             jTextField3.setEnabled(false);
  587.         }
  588.     }                                   
  589.  
  590.     private void jButton5KeyPressed(java.awt.event.KeyEvent evt) {                                    
  591.         // TODO add your handling code here:
  592.         if(evt.getKeyCode()==10){
  593.             int choose = JOptionPane.showConfirmDialog(null,"Continue to exit?.","Exit",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  594.             if(choose == JOptionPane.YES_OPTION){
  595.                 this.dispose();
  596.             }else if(choose == JOptionPane.NO_OPTION){
  597.                 jButton1.requestFocus();
  598.                 return;
  599.             }
  600.         }
  601.     }                                   
  602.  
  603.     private void jList1MouseClicked(java.awt.event.MouseEvent evt) {                                    
  604.         // TODO add your handling code here:
  605.     }                                   
  606.  
  607.     public void PopulatejList(){
  608.            String dbuser = "";
  609.            String dbpasswd = "";
  610.            String DriverPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb";
  611.  
  612.            String SQLString = ("select * from Table1");
  613.  
  614.            try{
  615.               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  616.            }
  617.            catch(Exception e){
  618.               JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC DRIVER Error",JOptionPane.WARNING_MESSAGE);
  619.               return;
  620.            }
  621.            Statement stmt = null;
  622.            Connection con = null;
  623.            ResultSet rset = null;
  624.  
  625.            try{
  626.               con = DriverManager.getConnection(DriverPrefix,dbuser,dbpasswd);
  627.               stmt = con.createStatement();
  628.            }
  629.            catch(Exception e){
  630.               JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
  631.            }
  632.  
  633.            try{
  634.               rset = stmt.executeQuery(SQLString);
  635.               String Add;
  636.               while(rset.next()){
  637.                   Add = rset.getString("ID")+rset.getString("Username")+rset.getString("Password")+
  638.                           rset.getString("Website");
  639.                   list.addElement(Add);
  640.               }
  641.               con.close();
  642.            }
  643.            catch(SQLException e){
  644.                JOptionPane.showMessageDialog(null,e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
  645.            }
  646.            jList1 = new JList(list);
  647.     }
  648.     /**
  649.     * @param args the command line arguments
  650.     */
  651.     public static void main(String args[]) throws SQLException {
  652.         java.awt.EventQueue.invokeLater(new Runnable() {
  653.             public void run() {
  654.                 new MainForm().setVisible(true);
  655.                 MainForm MF = new MainForm();
  656.                 MF.PopulatejList();
  657.             }
  658.         });
  659.     }
  660.  
  661.     // Variables declaration - do not modify
  662.     private javax.swing.JButton jButton1;
  663.     private javax.swing.JButton jButton2;
  664.     private javax.swing.JButton jButton3;
  665.     private javax.swing.JButton jButton4;
  666.     private javax.swing.JButton jButton5;
  667.     private javax.swing.JLabel jLabel1;
  668.     private javax.swing.JLabel jLabel2;
  669.     private javax.swing.JLabel jLabel3;
  670.     private javax.swing.JList jList1;
  671.     private javax.swing.JPanel jPanel1;
  672.     private javax.swing.JScrollPane jScrollPane1;
  673.     private javax.swing.JTextField jTextField1;
  674.     private javax.swing.JTextField jTextField2;
  675.     private javax.swing.JTextField jTextField3;
  676.     // End of variables declaration
  677.  
  678. }
  679.  
thanks guys for helping me.

2 days have been passed and still i can't get pass with this. thanks again...
Feb 11 '11 #1
0 1630

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ratnakar Pedagani | last post by:
Hi, I'm new to c# programming. I need help regarding a form which contains a help in the main menu. upon clicking the sub items of help menu, i should be able to display a pop up displaying the...
2
by: sachin | last post by:
Hello Everybody I need some help regarding Natural Language Processing. I am designing a MT system from a SOV language to a SOV language. I need a parser which can find the root word...
4
by: trialproduct2004 | last post by:
Hi all i am having application in C#. here what i want it to update one datagrid depending on particular value. I want to start minimum of 5 threads at a time and all these threads are updating...
2
by: xyzbindu | last post by:
hello, iam a student & need help regarding UPDATE method of dataadapter when i update the database using dataadpter.update(dataset,tablename) i get the following error an unhandled exception of...
1
by: chandrapsg | last post by:
Hi, i am working with jsp .. i wanna help regarding how to import or how to call python modules to jsp if a piece of code is availabe will be very helpful for me chandra
1
by: pdesh3 | last post by:
Hi, I have a form which is used to search the patient details. When I search for a patient it displays a list of patients' details in the subform. I want to implement the functionality...
2
by: sindhu sweet | last post by:
Hi friends, I am developing an IDE using java...as part of my project...i am half the way(hav created menus FILE, FONT,TEXTAREA,SCROLLBARS on both sides...) i need some help....can anyone refer...
5
by: dannynnad | last post by:
Hi, SuperAdministrator when trying to edit the frontend content the following error is comingup: "You are not authorized to view this resource." I checked from the backend whether the...
4
by: bsonline | last post by:
I hv a datetime format like '24/01/2008 15:56:01' . Now I have to select dates from a date range, but input datetime format like 24/1/2008 15:56:01'. I hv written the query like : "select...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.