Results 1 to 3 of 3

Thread: Need Help

  1. #1
    sagarvk is offline Member
    Join Date
    Jan 2015
    Posts
    25
    Rep Power
    0

    Default Need Help

    Hi
    As shown in the SS I show the data of the batch from the db.Now I want to edit the data of the jtable using the default Double editors and also show the data using a Date renderer in dd/MM/yyyy format.The problem is when the data is loaded from the db,I am not knowing how to implement and set the editors to double and date.Pls help me in it.I am new in java.The code of the above is shown as follows.







    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Billing;
    
    
    
    import java.awt.Component;
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.text.DateFormat;
    import java.text.DecimalFormat;
    import java.text.Format;
    import net.proteanit.sql.DbUtils;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JDesktopPane;
    import javax.swing.JFormattedTextField;
    import javax.swing.JInternalFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.text.MaskFormatter;
    import javax.swing.text.NumberFormatter;
    
    
    /**
     *
     * @author Sai Ram
     */
    public class IBatchOpening extends javax.swing.JInternalFrame {
    static Statement stmt;
    static Connection conn;  
    static ResultSet rs;
    PreparedStatement pstmt;
    public static int INO=Itemmaster.INO();
        public IBatchOpening() {
            initComponents();
            conn=DBConnect.Connect();
            
            
        try {
            stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }
        updatetable();
        Batch_Details.getColumnModel().getColumn(0).setHeaderValue("Batch No");
        Batch_Details.getColumnModel().getColumn(1).setHeaderValue("M.R.P");
        Batch_Details.getColumnModel().getColumn(2).setHeaderValue("Expiry Date");
        Batch_Details.getColumnModel().getColumn(3).setHeaderValue("Opng Stock");
        Batch_Details.getColumnModel().getColumn(4).setHeaderValue("Sales Rate");
        Batch_Details.getColumnModel().getColumn(5).setHeaderValue("Purch Rate");
        Batch_Details.getColumn("Batch No").setMinWidth(125);
        FormatDate();
        JFormattedTextField dt = new JFormattedTextField();
        MaskFormatter mask;
        try {
        mask = new MaskFormatter("##/##/####");
        mask.install(dt);
       } catch (ParseException e) {
        e.printStackTrace();
      }
       Batch_Details.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(dt));
        }
        private void FormatDate(){
            TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {
        SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
       public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            if( value instanceof Date) {
                value = f.format(value);
            }
            return super.getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);
        }
    };
         Batch_Details.getColumnModel().getColumn(2).setCellRenderer(tableCellRenderer);
         Batch_Details.repaint();
        }
        private void updatetable(){
        try {
            String sql="select BATCH,MRP_RATE,EXP_DT,OP_QTY,SALE_RATE,PUR_RATE,BTNO from BATCH where I_NO=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1, INO);
            rs=pstmt.executeQuery();
            Batch_Details.setModel(DbUtils.resultSetToTableModel(rs));
            Batch_Details.getColumnModel().getColumn(6).setMinWidth(0);
            Batch_Details.getColumnModel().getColumn(6).setMaxWidth(0);
            Batch_Details.getColumnModel().getColumn(6).setWidth(0);
            
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }
         
        
        }
        /**
         * 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() {
    
            lbliname = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            Batch_Details = new javax.swing.JTable();
            btnclose = new javax.swing.JButton();
            btnadd = new javax.swing.JButton();
            jLabel2 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
    
            setTitle("Batch-wise Opening Stock");
            addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
                public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                }
                public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                    formInternalFrameClosed(evt);
                }
                public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                }
                public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
                }
                public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
                }
                public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
                }
                public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                }
            });
    
            lbliname.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
            lbliname.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    
            Batch_Details.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null}
                },
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
                }
            ) {
                Class[] types = new Class [] {
                    java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
                };
    
                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }
            });
            Batch_Details.setRowHeight(20);
            Batch_Details.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
            Batch_Details.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    Batch_DetailsKeyPressed(evt);
                }
            });
            jScrollPane1.setViewportView(Batch_Details);
            Batch_Details.getColumnModel().getColumn(5).setCellEditor(null);
    
            btnclose.setText("OK");
            btnclose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btncloseActionPerformed(evt);
                }
            });
    
            btnadd.setText("Add");
            btnadd.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnaddActionPerformed(evt);
                }
            });
    
            jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel2.setText("Batch-wise Opening Stock");
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(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.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(137, 137, 137)
                                    .addComponent(jButton1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnadd, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnclose, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lbliname, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(151, 151, 151))))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(7, 7, 7)
                    .addComponent(lbliname, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(4, 4, 4)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(4, 4, 4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnadd)
                        .addComponent(btnclose)
                        .addComponent(jButton1))
                    .addContainerGap(31, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {                                         
           
        }                                        
    
        private void btncloseActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
        JDesktopPane desktopPane = getDesktopPane();
        JInternalFrame[] frames = desktopPane.getAllFrames();
        for (int i = 0 ; i < frames.length ; i ++)
        {
        if (frames[i].getTitle().equals("Item Master")){
            frames[i].setVisible(true);
        }
        }
        this.setVisible(false);
        }                                        
    
        private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {                                       
         DefaultTableModel model = (DefaultTableModel) Batch_Details.getModel();
         
         model.addRow(new Object[]{"","","","","",""}); 
        JFormattedTextField dt = new JFormattedTextField();
        MaskFormatter mask;
        try {
        mask = new MaskFormatter("##/##/####");
        mask.install(dt);
       } catch (ParseException e) {
        e.printStackTrace();
      }
       
       DecimalFormat dfmrp = new DecimalFormat("######0.00");
       NumberFormatter formattermrp = new NumberFormatter(dfmrp);
       formattermrp.setAllowsInvalid(false);
       JFormattedTextField MRP = new JFormattedTextField(formattermrp);
       
       DecimalFormat dfop = new DecimalFormat("######0.00");
       NumberFormatter formatterop = new NumberFormatter(dfop);
       formatterop.setAllowsInvalid(false);
       JFormattedTextField OP = new JFormattedTextField(formatterop);
       
       DecimalFormat dfsr = new DecimalFormat("######0.00");
       NumberFormatter formattersr = new NumberFormatter(dfsr);
       formattersr.setAllowsInvalid(false);
       JFormattedTextField SR = new JFormattedTextField(formattersr);
       
       DecimalFormat dfpr = new DecimalFormat("######0.00");
       NumberFormatter formatterpr = new NumberFormatter(dfpr);
       formatterpr.setAllowsInvalid(false);
       JFormattedTextField PR = new JFormattedTextField(formatterpr);
       
       dt.setBorder(null);
       MRP.setBorder(null);
       OP.setBorder(null);
       SR.setBorder(null);
       PR.setBorder(null);
       
        
        Batch_Details.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(dt));
        Batch_Details.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(OP));
        Batch_Details.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(SR));
        Batch_Details.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(PR));
        
        
        Batch_Details.setRowSelectionAllowed(true);
        int s=Batch_Details.getRowCount()-1,e=Batch_Details.getRowCount()-1;
        
        Batch_Details.getSelectionModel().setSelectionInterval(s,e);
        }                                      
        public static void SaveData() throws ParseException{
           String sql="Delete from Batch where I_NO=?";
             try{
                 PreparedStatement pstmt=conn.prepareStatement(sql);
                 pstmt.setInt(1, INO);
                 pstmt.executeUpdate();
                 pstmt.close();
             }catch (SQLException ex) {
                 ex.printStackTrace();
             }
            
            
            
            
            for (int i=0;i<Batch_Details.getRowCount();i++){
                if ((Batch_Details.getValueAt(i, 0)!=null||Batch_Details.getValueAt(i, 1) !=null||Batch_Details.getValueAt(i, 3)!=null||Batch_Details.getValueAt(i, 4) !=null||Batch_Details.getValueAt(i, 5) !=null)){
                int btno=0;    
                try {
                rs=stmt.executeQuery("select MAX(BTNO) as BATCHNO from BATCH");
                 } catch (SQLException ex) {
                 Logger.getLogger(IBatchOpening.class.getName()).log(Level.SEVERE, null, ex);
                 }
                 try {
                  if(rs.next()){
                      btno=rs.getInt("BATCHNO");
                     btno=btno+1;
                     }
                     else{
                    btno=1;
                  }
                 } catch (SQLException ex) {
                     Logger.getLogger(IBatchOpening.class.getName()).log(Level.SEVERE, null, ex);
                 }
          int batchid;
            if (Batch_Details.getValueAt(i, 6)==null){
               batchid=btno;
            }else{
                 batchid=Integer.parseInt(Batch_Details.getValueAt(i, 6).toString());
            }
            sql="INSERT INTO BATCH(BTNO,I_NO,ITEM,BATCH,MRP_RATE,EXP_DT,OP_QTY,SALE_RATE,PUR_RATE) VALUES(?,?,?,?,?,?,?,?,?)";
                    try {
                        PreparedStatement pstmt=conn.prepareStatement(sql);
                        pstmt.setInt(1, batchid);
                        pstmt.setInt(2, INO);
                        pstmt.setString(3, lbliname.getText());
                        pstmt.setString(4,(Batch_Details.getValueAt(i, 0).toString()) );
                        pstmt.setDouble(5,Double.parseDouble(Batch_Details.getValueAt(i, 1).toString()) );
                         DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                       String date=Batch_Details.getValueAt(i, 2).toString();
                       java.util.Date dtu=df.parse(date);
                       java.sql.Date dt= new java.sql.Date(dtu.getTime());
                        pstmt.setDate(6, dt);
                        pstmt.setDouble(7,Double.parseDouble(Batch_Details.getValueAt(i, 3).toString()) );
                        pstmt.setDouble(8,Double.parseDouble(Batch_Details.getValueAt(i, 4).toString()) );
                        pstmt.setDouble(9,Double.parseDouble(Batch_Details.getValueAt(i, 5).toString()) );
                        pstmt.executeUpdate();
                        pstmt.close();
                    } catch (SQLException ex) {
                       JOptionPane.showMessageDialog(null, ex);
                    } 
            
            }
            }
            
        }
     
        private void Batch_DetailsKeyPressed(java.awt.event.KeyEvent evt) {                                         
        
        }                                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        }                                        
    
        // Variables declaration - do not modify                     
        private static javax.swing.JTable Batch_Details;
        private javax.swing.JButton btnadd;
        private javax.swing.JButton btnclose;
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JScrollPane jScrollPane1;
        public static javax.swing.JLabel lbliname;
        // End of variables declaration                   
    
    
    
    
    
    }
    Last edited by sagarvk; 02-05-2015 at 03:45 PM.

  2. #2
    sagarvk is offline Member
    Join Date
    Jan 2015
    Posts
    25
    Rep Power
    0

    Default Re: Need Help


  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: Need Help

    Quote Originally Posted by sagarvk View Post
    Need Help
    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Posting Permissions

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