Results 1 to 12 of 12
  1. #1
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default .txt file into JTable

    Hi, I'm new to Java. Just like most of the other people who post threads on here, lol.

    I know that somebody posted a similar thread, but I was unable to understand it all. I also posted this on another forum, but have received no reply.

    Basically I need to load the stockEnquiriesLog.txt file into the JTable here. Apparently, some of the code from ReadFile.java can be put it into loadInitialEnquiries(), then modified to fit to the JTable. For the life of me, I can't figure it out. I've provided my code and the code to ReadFile.

    Thank you in advance for any assistance! And please excuse and stupid mistakes you see in the code, I'm not too bright with Java...

    This is the format of the .txt file:

    <stock_enquiry>
    Pink, Polly
    020 8567 1234
    PC Planet, Greenwich
    13:45
    I'd like help finding the number of printers we have got.
    </stock_enquiry>
    <stock_enquiry>
    Red, Ryan
    020 7465 9984
    Digital Outlet, Gatwick
    12:50
    What is the top of the range digital camera we stock?
    </stock_enquiry>


    Java Code:
    package coursework;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    class Enquiry implements Comparable {
        private int number;
        private int time;
        private String name;
        private String branch;
        private String enquiry;
        public Enquiry(int m, int t, String n, String b, String q) {
            number = m;
            name = n;
            branch = b;
            time = t;
            enquiry = q;
        }
    
    
        public Enquiry() {
            this(0, "default");
        }
        public int getNumber() {
            return number;
        }
    
         public int getTime() {
            return time;
        }
    
        public String getName() {
            return name;
        }
        public String getBranch() {
            return branch;
        }
        public String getEnquiry() {
            return enquiry;
        }
    
        public int compareTo(Object o) {
            Enquiry temp = (Enquiry) o;
    
            return (name.compareTo(temp.name));
        }
    }
    public class GUI2 extends JFrame {
    
        ArrayList enquiries = new ArrayList();
    
        JPanel panInput = new JPanel(new GridLayout(4,1)),
                panAdd = new JPanel(),
                panDelSort = new JPanel();
    
        JTextField txtName = new JTextField(10);
        JTextField txtBranch = new JTextField(10);
        JTextField txtEnquiry = new JTextField(10);
        JTextField txtNumber = new JTextField(5);
        JTextField txtTime = new JTextField(5);
        JButton btnAdd = new JButton("Add"),
                btnDelete = new JButton("Delete"),
                btnSort = new JButton("Sort Products");
    
        JTable tab = new JTable();
    
    
        DefaultTableModel tabMod = new DefaultTableModel();
    
    
        public GUI2() {
            super("Product list");
            tab.setModel(tabMod);
            tabMod.addColumn("Name");
            tabMod.addColumn("Telephone number");
            tabMod.addColumn("Branch");
            tabMod.addColumn("Callback time");
            tabMod.addColumn("Enquiry");
    
            tab.setAutoCreateRowSorter(true);
    
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    addEnquiry();
                }
            });
    
            btnDelete.setToolTipText("Select the record(s) to delete first");
            btnDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    deleteEnquiry();
                }
            });
            btnSort.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    sortEnquiries();
                }
            });
    
            panAdd.add(btnAdd);
            panAdd.add(new JLabel("Name"));
            panAdd.add(txtName);
            panAdd.add(new JLabel("Telehpone number"));
            panAdd.add(txtNumber);
            panAdd.add(new JLabel("Branch"));
            panAdd.add(txtBranch);
            panAdd.add(new JLabel("Callback Time"));
            panAdd.add(txtTime);
            panAdd.add(new JLabel("Enquiry"));
            panAdd.add(txtEnquiry);
            panDelSort.add(btnDelete);
            panDelSort.add(btnSort);
            panInput.add(panAdd);
            panInput.add(panDelSort);
            getContentPane().add(panInput, BorderLayout.SOUTH);
            getContentPane().add(new JScrollPane(tab));
            
        }
    
        public void deleteEnquiry() {
            int [] rows = tab.getSelectedRows();
            for (int i = 0; i < rows.length; i++) {
                tabMod.removeRow(rows[i] - i); 
                enquiries.remove(rows[i] - i);
            }
        }
    
        public void sortEnquiries() {
            Collections.sort(enquiries);
            loadProductsIntoTable();
        }
    
        public void addEnquiry() {
            String [] row = new String [5];
            Enquiry newEnq = new Enquiry
                    (Integer.parseInt(txtNumber.getText()), (Integer.parseInt(txtTime.getText())),
                    txtName.getText(),
                    txtBranch.getText(), txtEnquiry.getText()
                    );
    
            enquiries.add(newEnq);
            row[0] = newEnq.getName();
            row[1] = Integer.toString(newEnq.getNumber());
            row[2] = newEnq.getBranch();
            row[3] = Integer.toString(newEnq.getTime());
            row[4] = newEnq.getEnquiry();
            tabMod.addRow(row);
    
    
        }
        public void loadInitialEnquiries() throws IOException {
    
            BufferedReader br = new BufferedReader(
                    new FileReader("stockEnquiryLog.txt"));
    
    
                    String s = new String ();
                    String name = "";
                    String phone = "";
                    String branch = "";
                    String time = "";
                    String enquiry = "";
                    
                    String [] row = new String [4];
    
    
           while ((s = br.readLine()) != null) {
    
                if (s.equals("<stock_enquiry>")) {
                    name = br.readLine();
                    phone = br.readLine();
                    branch = br.readLine();
                    time = br.readLine();
                    enquiry = br.readLine();
    
    
                   
                    row[0] = name;
                    row[1] = phone;
                    row[2] = branch;
                    row[3] = time;
                    row[4] = enquiry;
                    tabMod.addRow(row);
              
                   
                 
        }
            br.close();
        }
                    {
            loadProductsIntoTable();
                    }
        }
        public void loadProductsIntoTable() {
            tabMod.setRowCount(0);
    
            String [] row = new String [4];
            for (Iterator i = enquiries.iterator(); i.hasNext();) {
                Enquiry temp = (Enquiry) i.next();
                row[0] = temp.getName();
                row[1] = Integer.toString(temp.getNumber());
                row[2] = temp.getBranch();
                row[3] = Integer.toString(temp.getTime());
                row[4] = temp.getEnquiry();
                tabMod.addRow(row);
            }
        }
        public static void main(String [] a){
            GUI2 me = new GUI2();
            me.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0) ;
                }
            });
            me.pack();
            me.setVisible(true);
        }
    }
    Java Code:
    package cwhints;
    
    import java.io.*;
    
    // reading text from a file
    public class ReadFile {
    
        public static void main(String[] args)
                throws IOException {
    
    
            BufferedReader br = new BufferedReader(
                    new FileReader("stockEnquiryLog.txt"));
    
            String s;
            String name = "";
            String phone = "";
            String branch = "";
            while ((s = br.readLine()) != null) {
                if (s.equals("<stock_enquiry>")) {
                    name = br.readLine();
                    phone = br.readLine();
                    branch = br.readLine();
                    System.out.println("name is " + name + " phone is " + phone + " branch is " + branch);
                }
            }
            br.close();
        }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The enquiry class won't compile. Consider the constructor:
    Java Code:
        public Enquiry() {
            this(0, "default"); // calls another Enquiry constructor
        }
    For this constructor to compile there must be another constructor in Enquiry with a matching signature, viz,
    Java Code:
        public Enquiry(int n, String s) {
    Java Code:
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class GUI2Rx extends JFrame {
    
        ArrayList enquiries = new ArrayList();
    
        JPanel panInput = new JPanel(new GridLayout(4,1)),
               panAdd = new JPanel(),
               panDelSort = new JPanel();
    
        JTextField txtName = new JTextField(10);
        JTextField txtBranch = new JTextField(10);
        JTextField txtEnquiry = new JTextField(10);
        JTextField txtNumber = new JTextField(5);
        JTextField txtTime = new JTextField(5);
        JButton btnAdd = new JButton("Add"),
                btnDelete = new JButton("Delete"),
                btnSort = new JButton("Sort Products");
    
        JTable tab = new JTable();
    
        DefaultTableModel tabMod = new DefaultTableModel();
    
    
        public GUI2Rx() {
            super("Product list");
            tab.setModel(tabMod);
            tabMod.addColumn("Name");
            tabMod.addColumn("Telephone number");
            tabMod.addColumn("Branch");
            tabMod.addColumn("Callback time");
            tabMod.addColumn("Enquiry");
    
            tab.setAutoCreateRowSorter(true);
    
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    addEnquiry();
                }
            });
    
            btnDelete.setToolTipText("Select the record(s) to delete first");
            btnDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    deleteEnquiry();
                }
            });
            btnSort.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    sortEnquiries();
                }
            });
    
            panAdd.add(btnAdd);
            panAdd.add(new JLabel("Name"));
            panAdd.add(txtName);
            panAdd.add(new JLabel("Telehpone number"));
            panAdd.add(txtNumber);
            panAdd.add(new JLabel("Branch"));
            panAdd.add(txtBranch);
            panAdd.add(new JLabel("Callback Time"));
            panAdd.add(txtTime);
            panAdd.add(new JLabel("Enquiry"));
            panAdd.add(txtEnquiry);
            panDelSort.add(btnDelete);
            panDelSort.add(btnSort);
            panInput.add(panAdd);
            panInput.add(panDelSort);
            getContentPane().add(panInput, BorderLayout.SOUTH);
            getContentPane().add(new JScrollPane(tab));
            
        }
    
        public void deleteEnquiry() {
            int [] rows = tab.getSelectedRows();
            for (int i = 0; i < rows.length; i++) {
                tabMod.removeRow(rows[i] - i); 
                enquiries.remove(rows[i] - i);
            }
        }
    
        public void sortEnquiries() {
            Collections.sort(enquiries);
            loadProductsIntoTable();
        }
    
        public void addEnquiry() {
            String[] row = new String[5];
            int number = Integer.parseInt(txtNumber.getText());
            int time = Integer.parseInt(txtTime.getText());
            String nameStr = txtName.getText();
            String branchStr = txtBranch.getText();
            String enquiryStr = txtEnquiry.getText();
            EnquiryRx newEnq = new EnquiryRx(number, time, nameStr,
                                             branchStr, enquiryStr);
    
            enquiries.add(newEnq);
            row[0] = newEnq.getName();
            row[1] = Integer.toString(newEnq.getNumber());
            row[2] = newEnq.getBranch();
            row[3] = Integer.toString(newEnq.getTime());
            row[4] = newEnq.getEnquiry();
            tabMod.addRow(row);
        }
    
        public void loadInitialEnquiries() throws IOException {
    
            BufferedReader br = new BufferedReader(
                    new FileReader("stockEnquiryLog.txt"));
    
            String s = new String ();
            String name = "";
            String phone = "";
            String branch = "";
            String time = "";
            String enquiry = "";
                    
            String [] row = new String [4];
    
            while ((s = br.readLine()) != null) {
                if (s.equals("<stock_enquiry>")) {
                    name = br.readLine();
                    phone = br.readLine();
                    branch = br.readLine();
                    time = br.readLine();
                    enquiry = br.readLine();
    
                    row[0] = name;
                    row[1] = phone;
                    row[2] = branch;
                    row[3] = time;
                    row[4] = enquiry;
                    tabMod.addRow(row);
                }
                br.close();
            }
            loadProductsIntoTable();
        }
    
        public void loadProductsIntoTable() {
            tabMod.setRowCount(0);
    
            String [] row = new String [4];
            for (Iterator i = enquiries.iterator(); i.hasNext();) {
                EnquiryRx temp = (EnquiryRx) i.next();
                row[0] = temp.getName();
                row[1] = Integer.toString(temp.getNumber());
                row[2] = temp.getBranch();
                row[3] = Integer.toString(temp.getTime());
                row[4] = temp.getEnquiry();
                tabMod.addRow(row);
            }
        }
    
        public static void main(String [] a){
            GUI2Rx me = new GUI2Rx();
    //        me.setDefaultCloseOperation(JFrame.EIXT_ON_CLOSE);
            me.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0) ;
                }
            });
            me.pack();
            me.setVisible(true);
        }
    }
    
    class EnquiryRx implements Comparable {
        private int number;
        private int time;
        private String name;
        private String branch;
        private String enquiry;
    
        public EnquiryRx(int m, int t, String n, String b, String q) {
            number = m;
            name = n;
            branch = b;
            time = t;
            enquiry = q;
        }
    
        public EnquiryRx() {
            this(0, 0, "", "", "");
        }
    
        public int getNumber() {
            return number;
        }
    
        public int getTime() {
            return time;
        }
    
        public String getName() {
            return name;
        }
    
        public String getBranch() {
            return branch;
        }
    
        public String getEnquiry() {
            return enquiry;
        }
    
        public int compareTo(Object o) {
            EnquiryRx temp = (EnquiryRx) o;
    
            return (name.compareTo(temp.name));
        }
    }

  3. #3
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Thank you, I was trying to figure that one too. :D Any ideas for reading the txt file? Thats what I'm having real difficulty on...:mad:

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Any ideas for reading the txt file?
    Read in the file data:
    Java Code:
        public GUI2Rx() {
            ...
            getContentPane().add(new JScrollPane(tab));
            // After the class has been constructed, read/load data:
            loadInitialEnquiries();
        }
    The compiler tells what needs work in your code:
    Java Code:
    C:\jexp>java GUI2Rx
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
            at GUI2Rx.loadInitialEnquiries(gui2rx.java:126)
            at GUI2Rx.<init>(gui2rx.java:76)
            at GUI2Rx.main(gui2rx.java:154)
    Java Code:
        public void loadInitialEnquiries() throws IOException {
            ...
            // This next line instantiates an array of 4 elements.
            // All 4 elements are null, of course.
            String [] row = new String [4];
                ...
                    // This next line is attempting to access
                    // element 5 at index 4 in the array. The array
                    // only has 4 elements. This will result in an
                    // ArrayIndexOutOfBoundsException.
                    row[4] = enquiry;
    Try using the number of columns in the JTable for the array size:
    Java Code:
        public void loadInitialEnquiries() {
            BufferedReader br = null;
            try {
                br = new BufferedReader(
                     new FileReader("stockEnquiryLog.txt"));
    
                String s;
                int colCount = tab.getColumnCount();
                String[] row = new String[colCount];
    
                while ((s = br.readLine()) != null) {
                    if(s.equals("<stock_enquiry>")) {
                        row[0] = br.readLine();  // name
                        row[1] = br.readLine();  // phone
                        row[2] = br.readLine();  // branch
                        row[3] = br.readLine();  // time
                        row[4] = br.readLine();  // enquiry
                        tabMod.addRow(row);
                    }
                }
                br.close();
            } catch(IOException e) {
                System.out.println("loadInitialEnquiries i/o error: " +
                                    e.getMessage());
            }
            // When you call this next method the first line inside
            // it dumps the table data. How will you then get access
            // to the data you read and loaded into the table in
            // this method?
    //        loadProductsIntoTable();
        }

  5. #5
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    I'm not sure... could you give me another pointer please?

    And thanks so much for the help so far! :)

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    It's easy enough to read the text file data into the DefaultTableModel and show it in a JTable.
    Java Code:
    import java.io.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TextToTable {
        JTable table = new JTable();
        DefaultTableModel model = new DefaultTableModel();
    
        public TextToTable() {
            model.addColumn("Name");
            model.addColumn("Telephone number");
            model.addColumn("Branch");
            model.addColumn("Callback time");
            model.addColumn("Enquiry");
            table.setModel(model);
            table.setAutoCreateRowSorter(true);
            loadInitialEnquiries();
        }
    
        private void loadInitialEnquiries() {
            BufferedReader br = null;
            try {
                br = new BufferedReader(
                     new FileReader("stockEnquiryLog.txt"));
    
                String s;
                int colCount = table.getColumnCount();
                String[] row = new String[colCount];
    
                while ((s = br.readLine()) != null) {
                    if(s.equals("<stock_enquiry>")) {
                        row[0] = br.readLine();  // name
                        row[1] = br.readLine();  // phone
                        row[2] = br.readLine();  // branch
                        row[3] = br.readLine();  // time
                        row[4] = br.readLine();  // enquiry
                        model.addRow(row);
                    }
                }
                br.close();
            } catch(IOException e) {
                System.out.println("loadInitialEnquiries i/o error: " +
                                    e.getMessage());
            }
        }
    
        private JScrollPane getContent() {
            return new JScrollPane(table);
        }
    
        public static void main(String[] args) {
            TextToTable demo = new TextToTable();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(demo.getContent());
            f.setSize(400,300);
            f.setVisible(true);
        }
    }
    Some other things to think about in your class design, ie, in the way you build your class to do what you want it to do:
    1 — The only way you can get to or access the data you start with is in the DefaultTableModel after startup or by reading the text file again.
    2 — At any time during the life of you application, ie, as user is adding and deleting data, where will you have complete access to all of the data? You could let the TableModel hold it all for you. Then you would access the data by querying the tableModel. Or, you could store the data in your ArrayList of Enquiry instances/objects. Then you could access the data via the list of Enquiry objects, or both list and tableModel. One additional thing you'll have to deal with in the Enquiry class/collection is generics (j2se 1.5+).
    3 — How you manage, search–for and alter (adding/deleting/editing) the data in the TableModel may affect your choice in #2 above and vice–versa.
    4 — Since this is your class you can change/redesign anything you want. As long as it makes sense to you, is easy to read/follow/understand and does what you want it will be okay.

  7. #7
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Thank you, I'm currently trying to get it to work. It just doesn't want to do it, ugh.

    Will get back to you asap, thanks again~

  8. #8
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Alright, sorry to bug you again. But I'm still not getting the data in the table! Here's my code, and sorry if its any dumb mistakes...

    Also, about being able to access the data, I was thinking of asking you next (after this problem is solved!) about any possibilities of being able to update the original log, so to speak. That's apparently what I'm supposed to do, but again, clueless.

    Java Code:
    package coursework;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.awt.Dialog.*;
    import javax.swing.JOptionPane.*;
    
    class Enquiry implements Comparable {
        private int number;
        private int time;
        private String name;
        private String branch;
        private String enquiry;
        public Enquiry(int m, int t, String n, String b, String q) {
            number = m;
            name = n;
            branch = b;
            time = t;
            enquiry = q;
        }
    
        public Enquiry() {
            this(0, 0, "", "", "");
        }
    
    
        public int getNumber() {
            return number;
        }
    
         public int getTime()  {
            return time;
        }
    
        public String getName() {
            return name;
        }
        public String getBranch() {
            return branch;
        }
        public String getEnquiry() {
            return enquiry;
        }
    
        public int compareTo(Object o) {
            Enquiry temp = (Enquiry) o;
    
            return (name.compareTo(temp.name));
           
        }
    }
    public class GUI2 extends JFrame {
    
        ArrayList enquiries = new ArrayList();
    
        JPanel panInput = new JPanel(new GridLayout(4,1)),
                panAdd = new JPanel(),
                panDelSort = new JPanel();
    
        JTextField txtName = new JTextField(10);
        JTextField txtBranch = new JTextField(10);
        JTextField txtEnquiry = new JTextField(10);
        JTextField txtNumber = new JTextField(10);
        JTextField txtTime = new JTextField(5);
        JButton btnAdd = new JButton("Add"),
                btnDelete = new JButton("Delete"),
                btnSort = new JButton("Sort by Name");
       JButton btnSortTime = new JButton("Sort by plus time");
    
        JTable tab = new JTable();
    
        DefaultTableModel tabMod = new DefaultTableModel();
    
    
        public GUI2() {
            super("Product list");
            tab.setModel(tabMod);
            tabMod.addColumn("Name");
            tabMod.addColumn("Telephone number");
            tabMod.addColumn("Branch");
            tabMod.addColumn("Callback time");
            tabMod.addColumn("Enquiry");
            tab.setAutoCreateRowSorter(true);
            loadInitialEnquiries();
    
    
            btnAdd.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                  
                  addEnquiry();
                
                    if (txtNumber.getText() != null) {
                        JOptionPane.showMessageDialog(null, "Please input a correct phone number and/or a time form");
                    }
                    else addEnquiry();
    
    
                
                }
    
            });
    
            btnDelete.setToolTipText("Select the record(s) to delete first");
            btnDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    deleteEnquiry();
                }
            });
            btnSort.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    sortEnquiriesName();
                }
            });
    
              btnSortTime.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    sortEnquiriesTimeAsc();
                }
            });
    
            panAdd.add(btnAdd);
            panAdd.add(new JLabel("Name"));
            panAdd.add(txtName);
            panAdd.add(new JLabel("Telehpone number"));
            panAdd.add(txtNumber);
            panAdd.add(new JLabel("Branch"));
            panAdd.add(txtBranch);
            panAdd.add(new JLabel("Callback Time"));
            panAdd.add(txtTime);
            panAdd.add(new JLabel("Enquiry"));
            panAdd.add(txtEnquiry);
            panDelSort.add(btnDelete);
            panDelSort.add(btnSort);
            panDelSort.add(btnSortTime);
            panInput.add(panAdd);
            panInput.add(panDelSort);
            getContentPane().add(panInput, BorderLayout.SOUTH);
            getContentPane().add(new JScrollPane(tab));
         
        }
        public void deleteEnquiry() {
    
            int [] rows = tab.getSelectedRows();
            for (int i = 0; i < rows.length; i++) {
                tabMod.removeRow(rows[i] - i); 
                enquiries.remove(rows[i] - i);
            
            }
    
        }
    
        public void sortEnquiriesName() {
            Collections.sort(enquiries);
            loadProductsIntoTable();
        }
    
        public void sortEnquiriesTimeAsc() {
            Collections.sort(enquiries);
            loadProductsIntoTable();
        }
    
        public void addEnquiry() {
            String [] row = new String [5];
            Enquiry newEnq = new Enquiry
                    (Integer.parseInt(txtNumber.getText()), (Integer.parseInt(txtTime.getText())),
                    txtName.getText(),
                    txtBranch.getText(), txtEnquiry.getText()
                    );
    
            enquiries.add(newEnq);
            row[0] = newEnq.getName();
            row[1] = Integer.toString(newEnq.getNumber());
            row[2] = newEnq.getBranch();
            row[3] = Integer.toString(newEnq.getTime());
            row[4] = newEnq.getEnquiry();
            tabMod.addRow(row);
    
    
        }
        public void loadInitialEnquiries() {
            BufferedReader br = null;
            try {
                    br = new BufferedReader(
                     new FileReader("stockEnquiryLog.txt"));
    
                    String s;
                    int colCount = tab.getColumnCount();
                    String[] row = new String[colCount];
    
                    while ((s = br.readLine()) != null) {
                    if(s.equals("<stock_enquiry>")) {
                        row[0] = br.readLine();  // name
                        row[1] = br.readLine();  // phone
                        row[2] = br.readLine();  // branch
                        row[3] = br.readLine();  // time
                        row[4] = br.readLine();  // enquiry
                        tabMod.addRow(row);
                    }
                br.close();
                    }
            }
                    
                    catch(IOException e) {
                    System.out.println("loadInitialEnquiries i/o error: " +
                                    e.getMessage());
                        }
        }
           
        public void loadProductsIntoTable() {
            tabMod.setRowCount(0);
    
            String [] row = new String [5];
            for (Iterator i = enquiries.iterator(); i.hasNext();) {
                Enquiry temp = (Enquiry) i.next();
                row[0] = temp.getName();
                row[1] = Integer.toString(temp.getNumber());
                row[2] = temp.getBranch();
                row[3] = Integer.toString(temp.getTime());
                row[4] = temp.getEnquiry();
                tabMod.addRow(row);
            }
        }
         private JScrollPane getContent() {
            return new JScrollPane(tab);
        }
    
        public static void main(String [] a){
            GUI2 me = new GUI2();
            JFrame j = new JFrame();
           j.add(me.getContent());
           
            me.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0) ;
                }
            });
            me.pack();
            me.setVisible(true);
        }
    }
    Last edited by Hikari; 01-01-2009 at 11:21 PM.

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Copying, pasting, compiling and running your last–posted code gets this in the console:
    Java Code:
    C:\jexp\GUI2>java GUI2
    loadInitialEnquiries i/o error: Stream closed
    Correcting the formatting of the loadProductsIntoTable method reveals that the br.close() line is misplaced, viz, it is inside the while loop causing the "closed InputStream" exception.
    Java Code:
        public void loadInitialEnquiries() {
            BufferedReader br = null;
            try {
                br = new BufferedReader(
                     new FileReader("stockEnquiryLog.txt"));
    
                String s;
                int colCount = tab.getColumnCount();
                String[] row = new String[colCount];
    
                while ((s = br.readLine()) != null) {
                    if(s.equals("<stock_enquiry>")) {
                        row[0] = br.readLine();  // name
                        row[1] = br.readLine();  // phone
                        row[2] = br.readLine();  // branch
                        row[3] = br.readLine();  // time
                        row[4] = br.readLine();  // enquiry
                        tabMod.addRow(row);
                    }
                    // Premature closing of InputStream by
                    // this misplaced line:
    //                br.close();
                }
                // Close the Reader after we are done with it.
                br.close();
            } catch(IOException e) {
                System.out.println("loadInitialEnquiries i/o error: " +
                                    e.getMessage());
            }
        }
    The kind of formatting you use doesn't really matter so much. Consistency matters a lot.

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Another thing to correct:
    Java Code:
        public static void main(String [] a){
            GUI2 me = new GUI2();
            // Your JFrame class takes care of this
            // so you can eliminate these two lines.
    //        JFrame j = new JFrame();
    //       j.add(me.getContent());
            ...

  11. #11
    Hikari is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Wow, you're the best! Thank you ever so much, I would never have figured this on my own!

    Is it okay to post here again if I can't figure something else out?

    Again, thanks. You've really got me out of a right fix!

  12. #12
    kuaq11 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi Hardwired,

    I tried to combine all the code, but still it cant load the txt file and there is no error show

Similar Threads

  1. JTable to txt File
    By Tzaphiel in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 11:47 AM
  2. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  3. Row Name In Jtable
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 08-16-2008, 12:23 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Posting Permissions

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