Results 1 to 4 of 4
  1. #1
    Knizz is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Exclamation Need help creating a table

    I need help creating a table that will read in data and the number of rows and columns will adjust to the size of the data file. (for ex. if the data file needs 4 columns and a 100 rows the table would automatically adjust to this) I know that i need to use either arrays or vectors but I cannot seem to figure out how to make the table since every tutorial I have found online has the data that goes into the rows and columns written in the code. Thanks for the help.

    If I could I would like to use the defaultTableModel at the end. I just need help on having the table know how many rows and columns to have.
    Last edited by Knizz; 07-08-2009 at 12:15 AM. Reason: an attempt to clarify things

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, say you want to create a table with 4 columns and 100 rows. How you can create that table? Just forget the dynamic size and try to workout with the fixed size. Can you show your attempt here.

  3. #3
    Knizz is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    This is what I have so far. It is in 2 separate classes.

    Table
    import java.util.Vector;

    /**
    * A class that creates dynamic tables in memory, extends the Vector class
    *
    *
    */

    public class Table extends Vector {
    String [] columns = null;

    /**
    * Constructor for the table.
    * @param col. String []. An array that contains the column names for this table.
    */
    public Table(String [] col) {
    columns = col;
    }

    /**
    * Add a row to the table.
    * @param newRow. Vector. Vector that contains the new row data.
    * @throws Exception. If the vector size != to columns length of the constructor, throws an exception.
    */
    public void addRow(Vector newRow) throws Exception {
    if (newRow.size() != columns.length) {
    throw new Exception("Wrong number of elements, found " +
    newRow.size() + ", expected " + columns.length);
    }
    this.add(newRow);
    }

    /**
    * Get a row from the table.
    * @param n. int. The row number get and return.
    * @return. Vector. Vector of objects at row 'n'.
    * @throws Exception. If the row doesn't exist, throws an exception.
    */
    public Vector getRow(int n) throws Exception {
    return (Vector)this.get(n);
    }

    /**
    * Remove a row from the table.
    * @param n. int. Which row to remove.
    * @throws Exception. If the row doesn't exist, throws an exception.
    */
    public void rmRow(int n) throws Exception {
    this.remove(n);
    }

    /**
    * Remove all the rows from the table.
    */
    public void rmAll() {
    this.removeAllElements();
    }

    /**
    * Pretty print the table into a string.
    * @return. String. Contains a print of the rows into a string.
    */
    public String dump() {
    Vector row;
    String rets = "";
    for (int j = 0; j<this.size();j++) {
    row = (Vector)this.get(j);
    for (int i=0;i<row.size();i++) {
    rets += row.get(i) + "\t";
    }
    rets += "\n";
    }
    return rets;
    }
    }


    Test
    import java.util.Vector;

    public class Test {
    public static void main(String [] args) {

    String [] columns = new String[4];
    columns[0] = "Name";
    columns[1] = "Date";
    columns[2] = "Time";
    columns[3] = "Purchase Price";

    Vector row = new Vector();

    Table ben = new Table(columns);
    int rows = ben.size();
    System.out.println("Rows = " + rows);

    try {
    row.add("IBM");
    row.add("10-25-2009");
    row.add("09:00");
    row.add(123.4);
    ben.addRow(row);

    row = new Vector();
    row.add("NG");
    row.add("10-25-2009");
    row.add("09:00");
    row.add(10.4);
    ben.addRow(row);


    row = new Vector();
    row.add("GOG");
    row.add("10-25-2009");
    row.add("09:00");
    row.add(123.4);
    ben.addRow(row);

    System.out.println(ben.dump());

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void printRow(Vector row) {
    for (int i=0;i<row.size();i++) {
    System.out.print(row.get(i) + "\t");
    }
    System.out.println();
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sorry for the late replay here. :)

    Quote Originally Posted by Knizz View Post
    I know that i need to use either arrays or vectors but I cannot seem to figure out how to make the table since every tutorial I have found online has the data that goes into the rows and columns written in the code.
    Ok, you've create a table with fixed data in the above code. Now you want to adjust it dynamically.

    Java Code:
    String [] columns = new String[4];
    columns[0] = "Name";
    columns[1] = "Date";
    columns[2] = "Time";
    columns[3] = "Purchase Price";
    asically you have to change values in the above code dynamically. So why you cannot do that? After the collect number of rows and columns from the file, initialize the array, and the rest of the code may change slight change.

Similar Threads

  1. Replies: 2
    Last Post: 02-23-2009, 02:18 AM
  2. Replies: 0
    Last Post: 07-11-2008, 04:31 PM
  3. Simple Table Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:58 PM
  4. regarding to creating table.....
    By daredavil82 in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 04:55 AM
  5. Table Help
    By CoOlbOyCoOl in forum NetBeans
    Replies: 1
    Last Post: 05-06-2007, 04:19 PM

Tags for this Thread

Posting Permissions

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