Jump to content

Loading a .txt file into a JTable.

- - - - -

  • Please log in to reply
No replies to this topic

#1
Hikari

Hikari

    Newbie

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

This is an assignment I have to do, basically I need to load the stockEnquiriesLog.txt file into the JTable here. My lecturer has given some hints, saying to just use some of the code from ReadFile.java and put it into loadInitialEnquiries(), then modifying 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.

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>

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


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);
    }
}

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();
    }
} 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users