5 Replies - 663 Views - Last Post: 26 November 2012 - 10:23 AM Rate Topic: -----

#1 theinarasu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 12

how to add data frm another file to jtable

Posted 26 November 2012 - 08:05 AM

this is my jtable class



import java.awt.*;
import javax.swing.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author theinarasu
 */
public class table extends JFrame  {
 	
    public table(){
   }
    JTable table;
    
   public table(String name , String address , String account , String usage ,String total , String date){
   setLayout(new FlowLayout());
   

   
   String[] columnNames = {"Name" , "address" ,"account ","date" ,"usage" ,"totalpayment"};

   Object [][] data ={
       {name , address , account , date , usage , total}
          
   };
      
   
    DefaultTableModel model = new DefaultTableModel(data,columnNames);  
    table = new JTable(model);


    
    table .setPreferredScrollableViewportSize(new Dimension(500, 50 ));
    table.setFillsViewportHeight(true);
    
    JScrollPane sp = new JScrollPane(table);
    add(sp);
    
     
    
   }





    public static void main (String args []){
    
    table gui = new table();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(1200,200);
    gui.setVisible(true);
    gui.setTitle("DATABASE");
    
    
    }
    
   
   
   }
    
    
[code]



this is my main class
[code]

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author theinarasu
 */
public class Billing extends javax.swing.JFrame {

    /**
     * Creates new form Billing
     */
      double currentReading, previousReading, usage ,total  ;
       DecimalFormat df = new DecimalFormat("0.0");    
       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            java.util.Date date = new java.util.Date();
            String datetime=  dateFormat.format(date);
      
    public Billing() {
        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() {

        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenuBar3 = new javax.swing.JMenuBar();
        jMenu5 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuBar4 = new javax.swing.JMenuBar();
        jMenu7 = new javax.swing.JMenu();
        jMenu8 = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu10 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();

        jMenuItem3.setText("jMenuItem3");

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        jMenu5.setText("File");
        jMenuBar3.add(jMenu5);

        jMenu6.setText("Edit");
        jMenuBar3.add(jMenu6);

        jMenu7.setText("File");
        jMenuBar4.add(jMenu7);

        jMenu8.setText("Edit");
        jMenuBar4.add(jMenu8);

        jMenuItem9.setText("jMenuItem9");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Syarikat Air Darul Aman SDH.BHD.");
        setSize(new java.awt.Dimension(500, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Customer Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 14))); // NOI18N

        jLabel1.setText("Account Number :");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Name:");

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Address :");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel4.setText("previous Meter Reading :");

        jLabel5.setText("Current Meter Reading :");

        jLabel6.setText("Units");

        jLabel7.setText("Units");

        jLabel8.setText("Usage :");

        jLabel9.setText("Total Payment(RM) :");

        jButton1.setText("EXIT");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Reset");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Print");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Total");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jLabel8)
                                .add(18, 18, 18)
                                .add(jLabel10))
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(39, 39, 39)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(jTextField1)
                                    .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                                    .add(jScrollPane1))
                                .addContainerGap(12, Short.MAX_VALUE))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField4))
                                    .add(jLabel9))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel11)
                                    .add(jLabel6)
                                    .add(jLabel7))
                                .add(0, 0, Short.MAX_VALUE))))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(jButton4)
                        .add(18, 18, 18)
                        .add(jButton3)
                        .add(12, 12, 12)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(22, 22, 22)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel7))
                .add(29, 29, 29)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(jLabel9)
                    .add(jLabel10)
                    .add(jLabel11))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 25, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton4)
                    .add(jButton3)
                    .add(jButton2)
                    .add(jButton1))
                .add(27, 27, 27))
        );

        jMenu1.setText("File");

        jMenuItem1.setText("New");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Save");
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator1);

        jMenuItem4.setText("Exit");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem5.setText("Database");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem10.setText("Reset");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem10);

        jMenuItem6.setText("calculate total");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setText("calculate usage");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);
        jMenu2.add(jSeparator2);

        jMenuItem8.setText("print");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem8);

        jMenuBar1.add(jMenu2);

        jMenu10.setText("Help");

        jMenuItem12.setText("About");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu10.add(jMenuItem12);

        jMenuBar1.add(jMenu10);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
     System.exit(0);
    }                                          

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {                                            
     JOptionPane.showMessageDialog(null,"\n---------------------------"
            + "----------------------" + "\n Give Us A Call"
            + "\n Our main headquarters is located in sunny Southern California but our " 
            + "\n presence and reach is global."
            + "\n Click the markers below for our phone information." 
            + "\n If you are unsure give us a call at +1-818-766-2400 and we'll get you where you're going."
            + "\n\n http://www.sada.com.my" + "\n--------------------------------"
            + "-----------------"); 
    }                                           

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
   jTextField1.setText("");
    }                                           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  System.exit(0);
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
total();   
       String name =jTextField2.getText();  
     String address =jTextArea1.getText(); 
     String account =  jTextField1.getText();
     String usage =jLabel10.getText(); 
     String total =jLabel11.getText(); 
     
        table tb = new table(name ,address , account , usage ,total ,datetime);
      
    }                                        

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        jTextField2.setText("");
    }                                           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   jTextField1.setText("");
   jTextField2.setText("");
   jTextField3.setText("");
   jTextField4.setText("");
   jTextArea1.setText("");
   jLabel10.setText("");
   jLabel11.setText("");
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     String name =jTextField2.getText();  
     String address =jTextArea1.getText(); 
     String account =  jTextField1.getText();
     String usage =jLabel10.getText(); 
     String total =jLabel11.getText();
     
                 int confirm = JOptionPane.showConfirmDialog(null,   " date : " + 
             date + "\naccount number :" + account + "\nfor " + "name :" + name + "\n" +
             "\n\n\ttotal usage  is RM : " + total + "\n\nConfirm  ?",
             "Confirm", JOptionPane.YES_NO_OPTION);   
              
              
   if (JOptionPane.NO_OPTION == confirm) {

        
                          int confirm1 = JOptionPane.showConfirmDialog
                         (null,   "ARE YOU SURE WANT TO CANCEL"+"\n\nConfirm  ?",   
                         "Confirm", JOptionPane.YES_NO_OPTION);
            
                 
                    if (JOptionPane.NO_OPTION == confirm) {
             
                         int confirm2 = JOptionPane.showConfirmDialog(null,   " date : " + 
                         date + "\naccount number :" + account + "\nfor " + "name :" + name+ "\n" +
                         "\n\n\ttotal usage  is RM : " + total+ "\n\nConfirm  ?",
                         "Confirm", JOptionPane.YES_NO_OPTION);
	

                       }else{}
                    
                    
     }
              
                       else{    print(); }
                                      	
                          

    }                                        

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
previousReading = Float.parseFloat(jTextField3.getText());
        currentReading = Float.parseFloat(jTextField4.getText());
        usage = previousReading - currentReading ;
        jLabel10.setText(String.valueOf(usage));
      
    }                                          

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // for print menuitem code
        
     String name =jTextField2.getText();  
     String address =jTextArea1.getText(); 
     String account =  jTextField1.getText();
     String usage =jLabel10.getText(); 
     String total =jLabel11.getText();
     
                 int confirm = JOptionPane.showConfirmDialog(null,   " date : " + 
             date + "\naccount number :" + account + "\nfor " + "name :" + name + "\n" +
             "\n\n\ttotal usage  is RM : " + total + "\n\nConfirm  ?",
             "Confirm", JOptionPane.YES_NO_OPTION);   
              
              
   if (JOptionPane.NO_OPTION == confirm) {

        
                          int confirm1 = JOptionPane.showConfirmDialog
                         (null,   "ARE YOU SURE WANT TO CANCEL"+"\n\nConfirm  ?",   
                         "Confirm", JOptionPane.YES_NO_OPTION);
            
                 
                    if (JOptionPane.NO_OPTION == confirm) {
             
                         int confirm2 = JOptionPane.showConfirmDialog(null,   " date : " + 
                         date + "\naccount number :" + account + "\nfor " + "name :" + name+ "\n" +
                         "\n\n\ttotal usage  is RM : " + total+ "\n\nConfirm  ?",
                         "Confirm", JOptionPane.YES_NO_OPTION);
	

                       }else{}
                    
                    
     }
              
                       else{    print(); }
    }                                          

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
     total();
    }                                          

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
   jTextField1.setText("");
   jTextField2.setText("");
   jTextField3.setText("");
   jTextField4.setText("");
   jTextArea1.setText("");
   jLabel10.setText("");
   jLabel11.setText("");
    JOptionPane.showMessageDialog(null, "Data not save in database","reset" ,
        JOptionPane.INFORMATION_MESSAGE);
    }                                           

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
 String name =jTextField2.getText();  
     String address =jTextArea1.getText(); 
     String account =  jTextField1.getText();
     String usage =jLabel10.getText(); 
     String total =jLabel11.getText(); 
     
        table tb = new table(name ,address , account , usage ,total ,datetime);
     tb.setVisible(true);
     
    }                                          
public double total(){
   
    try{
        previousReading = Float.parseFloat(jTextField3.getText());
        currentReading = Float.parseFloat(jTextField4.getText());
        usage = previousReading - currentReading ;
        jLabel10.setText(String.valueOf(usage));

        if( usage <= 20)
        {
        total = 20 * 0.5 ;
        jLabel11.setText(String.valueOf(total));
        }
       
          else if (21 >=usage && usage<= 35)
        {
         total = (20 * 0.5) + ((usage -20) * 0.90);
         jLabel11.setText(String.valueOf(total));
               
        }
        
        else if (usage >35)
        {
         total = 20* 0.5 + (15 * 0.90) +((usage -35)*1.30);
         jLabel11.setText(String.valueOf(total));

        }
//    }catch (NullPointerException e ) {
//               
//                   JOptionPane.showMessageDialog(null,"Please enter correct data ");
//                }
    
    }catch (NumberFormatException e) {
               
                   JOptionPane.showMessageDialog(null,"Please enter data to system calculate ");
                }
             
   return total;
   }
public void print(){
        
  
     String name =jTextField2.getText();  
     String address =jTextArea1.getText(); 
     String account =  jTextField1.getText();
     String usage =jLabel10.getText(); 
     String total =jLabel11.getText(); 
     
        
              
     
 try { 

     FileWriter FW = new FileWriter(buildNumber() + ".txt", true);
     FileWriter FW1 = new FileWriter( "list.txt", true);
     
                      PrintWriter PW = new PrintWriter(FW);
                      PrintWriter PW1 = new PrintWriter(FW1);
                      
                         PW.println("Receipt no : " + buildNumber() + 1);
                         PW.println("Name :" + name);
                         PW.println("address :" + address);
                         PW.println("Date :" + date);
                         PW.println("account number :" + account);
                         PW.println("usage :" + usage);
                         PW.println("total :" + total);
                         PW.println("********************************************" + 
                               "***************************************************");
                         FW.close();
                    
                        PW1.println("Receipt no : " + buildNumber() + 1);
                        PW1.println("Name :" + name);
                        PW1.println("address :" + address);
                        PW1.println("Date :" + date);
                        PW1.println("account number :" + account);
                        PW1.println("usage :" + usage);
                        PW1.println("total :" + total);
                        PW1.println("********************************************" + 
                        "***************************************************");
                        FW1.close();
	
                         Report myR = new Report(name,datetime, address,account,usage , total);
                         myR.setTitle("Receipt Detail");
//                       myR.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                         myR.setSize(400, 576);
                         myR.setVisible(true);
                         myR.setResizable(true);
                         myR.setLocationRelativeTo(null);
                    
                       jTextField1.setText("");
                       jTextField2.setText("");
                       jTextField3.setText("");
                       jTextField4.setText("");
                       jTextArea1.setText("");
                       jLabel10.setText("");
                       jLabel11.setText("");
                       
                     } 
 catch(NumberFormatException e) {
                      JOptionPane.showMessageDialog(null,"Please insert the required data.");{

}
}catch (IOException f) {}




}
public  String buildNumber() {//generate 100 unique numbers
        int n[] = new int[100];
        int goodNumber = 1;
        Random randomGenerator = new Random();

        while (goodNumber < 5) {
            for (int i = 0; i < 4; i++) {
                n[i] = randomGenerator.nextInt(10);
                if (i > 0) {
                    for (int j = 0; j < i; j++) {
                        if (n[i] == n[j]) {
                            goodNumber = 1;
                            while (n[j] == n[i]) {
                                n[i] = randomGenerator.nextInt(10);
                            }
                            j = 0;
                        } else {
                            goodNumber++;
                        }
                    }
                }
            }
        }
        return String.valueOf(n[0]) + String.valueOf(n[1])
               + String.valueOf(n[2]) + String.valueOf(n[3]);
     }
        
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Billing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Billing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Billing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Billing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Billing().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu10;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuBar jMenuBar3;
    private javax.swing.JMenuBar jMenuBar4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration
}






i want to add data into jtable and also arraylist ...im stuck here ...im try to googled and youtube tutorial but still confusing and cant get the result
my problem :
1) cant add new data into arraylist
2) how to insert the data itable in arraylist

pls help me

This post has been edited by pbl: 26 November 2012 - 08:07 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how to add data frm another file to jtable

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to add data frm another file to jtable

Posted 26 November 2012 - 08:11 AM

Please just post the relevant code.

You will have to write your own TableModel rather than using the DefaultTableModel

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 2
  • +
  • -

#3 theinarasu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 12

Re: how to add data frm another file to jtable

Posted 26 November 2012 - 08:59 AM

ok sir ...thanks for the code demo..how to open the class if i press button in my main which mean from another class...
i try to do like this :


Csv2TableModel model = new Csv2TableModel();
setVisible(true);



Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: how to add data frm another file to jtable

Posted 26 November 2012 - 09:20 AM

The thing about a JTable; It simply represents the TableModel. You need to pass your TableModel as a constructor parameter:

JTable myTable = new JTable(model);

Was This Post Helpful? 0
  • +
  • -

#5 theinarasu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-October 12

Re: how to add data frm another file to jtable

Posted 26 November 2012 - 09:30 AM

Luckless i still dont get it ....pls help me ....!!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to add data frm another file to jtable

Posted 26 November 2012 - 10:23 AM

Where is your ArrayList defined ? Don't see it in your code.
Just pass it as argument to the Model

	
	// class that extends the AbstractTableModel
	class MyModel extends AbstractTableModel {

		ArrayList<Object[]> al;
		// the headers
		String[] header;
		
		// constructor 
		MyModel(ArrayList<Object[]> al, String[] header) {
			// save the header
			this.header = header;	
			// and the rows
			this.al = al;
		}
		public int getRowCount() {
			return al.size();
		}

		public int getColumnCount() {
			return header.length;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return al.get(rowIndex)[columnIndex];
		}
		
		public String getColumnName(int index) {
			return header[index];
		}
		
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1