Results 1 to 3 of 3
  1. #1
    picuser is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Help on Basic table entries....

    Hi, I'm new to JAVA, I did some basic program exercises using the output window. I'm interested to create a simple form like the phonebook program using JFrame. I created 4 buttons.
    1. Connect button-connects to database..done
    2. Save button - add entries-don't know how here is where I need help.
    3. Cancel button - cancel entries.
    4. Close button - close form.
    To be honest I don't know how to start writing into jButton1ActionPerformed() to add my entries to the fields.

    Here is my incomplete codes, hope anybody here shades some light..
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * MainJFrame.java
     *
     * Created on Dec 25, 2010, 8:05:48 PM
     */
    
    
    import java.sql.*;
    import javax.swing.JOptionPane;
    
    public class MainJFrame extends javax.swing.JFrame {
    
        /** Creates new form MainJFrame */
        public MainJFrame() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jTextField2 = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jTextField3 = new javax.swing.JTextField();
            jLabel4 = new javax.swing.JLabel();
            jTextField4 = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            jTextField5 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            Connect = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("LastName:");
            jLabel1.setName("jLabel1"); // NOI18N
    
            jLabel2.setText("FirstName:");
            jLabel2.setName("jLabel2"); // NOI18N
    
            jTextField1.setName("jTextField1"); // NOI18N
    
            jTextField2.setName("jTextField2"); // NOI18N
    
            jLabel3.setText("Address:");
            jLabel3.setName("jLabel3"); // NOI18N
    
            jTextField3.setName("jTextField3"); // NOI18N
    
            jLabel4.setText("TelNo:");
            jLabel4.setName("jLabel4"); // NOI18N
    
            jTextField4.setName("jTextField4"); // NOI18N
    
            jLabel5.setText("Age:");
            jLabel5.setName("jLabel5"); // NOI18N
    
            jTextField5.setName("jTextField5"); // NOI18N
    
            jButton1.setText("Save");
            jButton1.setName("jButton1"); // NOI18N
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Cancel");
            jButton2.setName("jButton2"); // NOI18N
    
            jButton3.setText("Close");
            jButton3.setName("jButton3"); // NOI18N
    
            Connect.setText("Connect");
            Connect.setName("Connect"); // NOI18N
            Connect.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    ConnectActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(Connect)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton1)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton3))
                                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addContainerGap(26, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(Connect)
                    .addGap(7, 7, 7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(28, 28, 28)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3))
                    .addContainerGap(41, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void ConnectActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
           ResultSet rs;
           Statement stmt;
           String sql;
           Connection con; // The connection to the database.
           try{
    
    // First, tell Java what driver to use and where to find it.
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    // Next, create a connection to your data source.
    // Specify that you are using the ODBC-JDBC Bridge.
    // And specify the data source from ODBC.
    con = DriverManager.getConnection("jdbc:odbc:phonebook");
    JOptionPane.showMessageDialog(null,"Test Okay");
    }
    // Catch any exceptions that are thrown.
    catch(ClassNotFoundException e){
    
    System.out.println(e.toString());
    }
    catch(SQLException e){
    
    System.out.println(e.toString());
    }
    
    
    
    
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
    
    
            String lastname = jTextField1.getText();
            String firstname = jTextField2.getText();
            String address = jTextField3.getText();
            String telno = jTextField4.getText();
            String age = jTextField5.getText();
    
    
           
    
    
        }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
    
                    new MainJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton Connect;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        // End of variables declaration
    
    }
    regards,
    picuser
    Attached Thumbnails Attached Thumbnails Help on Basic table entries....-form1.jpg  
    Last edited by picuser; 12-25-2010 at 01:59 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to Google for and read the Oracle Swing tutorials (search on Java Tutorials Big Index), especially the tutorials on use of buttons. It will show you how to do some of what you're looking at. Otherwise if you need more specific help, you'll want to ask a more specific question.

  3. #3
    picuser is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Modify and delete certain xml entries
    By scheilaad in forum XML
    Replies: 2
    Last Post: 02-28-2011, 11:18 PM
  2. JNDI - Getting the child entries of base DN
    By natu in forum Advanced Java
    Replies: 7
    Last Post: 08-06-2010, 11:01 AM
  3. Basic high score table
    By Thurizdan in forum New To Java
    Replies: 5
    Last Post: 05-19-2010, 07:10 PM
  4. Printing multiple user entries.
    By mConfused in forum Advanced Java
    Replies: 2
    Last Post: 04-09-2010, 12:44 PM
  5. Not able to delete MSSql database table entries
    By wickedrahul9 in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •