Reading from Vector - Page 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Reading from Vector

  1. #16
    Join Date
    Feb 2004
    Posts
    541
    read my last post, you're not actually casting the Employee object back from an Object type.

  2. #17
    Join Date
    Apr 2004
    Posts
    28
    Mike yes that did the trick --- I missed off the (Employee) bit in my code and have added it and tested it ok

    Thanks again

    Still having difficulty with the picture Object though ie reading it into my application in the correct format

  3. #18
    Join Date
    Feb 2004
    Posts
    541
    i'd go with Cjards second suggestion. Don't mess around with object streams for reading and writing a BLOB to the database. Simply store the path to the picture in the database (would work well as a primary key depending on whats in the table), and then you can simply extract the path from the database and use it in your app to link to the picture. Thats exactly what I did the last time I needed to store pictures.

  4. #19
    Join Date
    Apr 2004
    Posts
    28
    Originally posted by mikeBarr81
    firstly, definately do what reinkesm says about making a User object to store all the data and use getter and setter methods to access it. Then store the User object in the Vector (or whatever else you want to store it in).

    To display the data in a JTable you need to create a class that extends AbstractTableModel and overides all it's abstract methods. The JTable will use this object to determine what to display where. I won't explain how to use it here because sun have done a wonderful job of explaining it on their own. If you go here you'll be taken to suns "How to use JTable" page. I read this a few weeks ago and in half an hour i'd knocked up an AbstractTableModel to suit my needs. You'll need to add your Vector of information to this abstract table model object using a method of your own making, and the methods in the object will be tailored to your data so all the JTable has to do is ask "What should i put in row 2 column 5" and your data model will know what to tell it.
    I have read the tutorial on JTables (link above) but cant quite figure out how to get my data into the correct format. here is my code :

    public MyTableModel(){


    for (int i= 0;i<DBAccess.allData.size();i++){
    Employee em = (Employee)DBAccess.allData.get(i);
    Object data[][]= {{(""+em.getuserID()),(em.getTitle()),(em.getInitials()),(em.getSurname()),
    (em.getRole()),(em.getWorkDept()),(em.getTel()),(new Boolean(em.getDoor1())),(new Boolean(em.getDoor2())),
    (new Boolean(em.getDoor3())),(new Boolean(em.getStore())),(new Boolean(em.getServerRm())),(new Boolean(em.getAny())),
    (new Boolean(em.getWorking())),(new Boolean(em.getNight())),(new Boolean(em.getLocked()))}};

    }

    From the code above you can see I am trying to iterate through all my Employees and asign the selected values to the array. Ive tried using loops but as soon as I place a variable inside the array ie data[i][] it brings up an error. Variable i is the row and the other data is the columns.

    Any ideas ?

  5. #20
    Join Date
    Feb 2004
    Posts
    541
    exactly what errors do you get?

  6. #21
    Join Date
    Apr 2004
    Posts
    28
    Originally posted by mikeBarr81
    exactly what errors do you get?
    data = new Object [DBAccess.allData.size()][16];
    for (int i= 0;i<DBAccess.allData.size();i++){
    Employee em = (Employee)DBAccess.allData.get(i);
    for (int x=0;x<16;x++){
    data [i][x]= {{""+em.getuserID(),em.getTitle(),em.getInitials(),
    em.getSurname(),em.getRole(),em.getWorkDept(),em.getTel(),
    new Boolean(em.getDoor1()),new Boolean(em.getDoor2()),new Boolean(em.getDoor3()),
    new Boolean(em.getStore()),new Boolean(em.getServerRm()),new Boolean(em.getAny()),
    new Boolean(em.getWorking()),new Boolean(em.getNight()),new Boolean(em.getLocked())}};

    With the above code I get "Illegal start of expression at the line data[i][x]

  7. #22
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by Green_Manalishi


    public MyTableModel(){


    for (int i= 0;i<DBAccess.allData.size();i++){
    Employee em = (Employee)DBAccess.allData.get(i);
    Object data[][]= {{(""+em.getuserID()),(em.getTitle()),(em.getInitials()),(em.getSurname()),
    (em.getRole()),(em.getWorkDept()),(em.getTel()),(new Boolean(em.getDoor1())),(new Boolean(em.getDoor2())),
    (new Boolean(em.getDoor3())),(new Boolean(em.getStore())),(new Boolean(em.getServerRm())),(new Boolean(em.getAny())),
    (new Boolean(em.getWorking())),(new Boolean(em.getNight())),(new Boolean(em.getLocked()))}};

    }

    From the code above you can see ...
    no offence meant or anything.. but I cant see anything in that code.. you might be able to read it, because you wrote it.. but it's seriously hard on someone else who has to wade through it..

    it's about as readable as this:

    em()((.(Boolean)(em((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolean (em(.Boolean)))()em.em(Boolean)))()em.em()((.(Boolean)Boolean()em))(emBoolean((Boolean)em) .em((Boolean(em(.Boolean)))()em.em()((.(Boolean)()((.(Boolean)(em((Boolean).Boolean((em.Bo olean()em))(emBoolean((Boolean)em).em((Boolean(em(.Boolean)))()em.em()((.(Boolean)(em((Boo lean).(em(.em((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolean(em(em ((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolean(em(.Boolean)))()em .em()((.(Boolean)(em((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolea n(em(.Boolean)))()em.em()((.(Boolean)(em((Boolean).Boolean((em.Boolean()em))(emBoolean((Bo olean)em).em((Boolean(em(.Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolean(em( .Boolean)))()em.em()((.(Boolean)(em((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean )em).em((BooleanBoolean)))()em.em()((.(Boolean)(em((Boolean).Boolean((em.(.Boolean)))()em. em()((.(Boolean)(em((Boolean).Boolean((em.Boolean()em))(emBoolean((Boolean)em).em((Boolean (em(.Boolean)))()em

    trust me.. that really is what it looks like to us
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  8. #23
    Join Date
    Feb 2004
    Posts
    808
    basically, youre going about this the wrong way..

    When you write your own tablemodel, you dont take everything out of a database and store it in an array of employees, then take everything out of the array of employees and store it in an array full of objects, then give it to the jtable, who will take everything out of the array of objects...

    what you should do is completely implement a table model:

    Read the entire relevant contents out of the database into an array of Employees

    then write the correct methods for the table model.. i forget what these are called but it's stuff like "getCellAtIndex()" and the method will be passed an X and a Y.. so the Y is found by going to your EMployee array at that index..
    And the X, well what is returned depends on how you want your table laid out..

    Suppose you have 3 Employees like this:

    Fred, programmer, 20000,
    John, manager, 40000
    Dave, advertising, 15000

    when the table asks the model for 1,2 .. that's the job title of dave:

    Code:
    public string getCellAtIndex(x, y){
     if x == 0
      return theEMployees[y].getName();
     else if x == 1
      return theEmployees[y].getJobTitle();
     else if x ==  2
      return theEmployees[y].getSalary();
    }
    do you see, that all the table does is repeatedly ask for the data it should display in every cell, in a simple fashion:
    0,0
    0,1
    0,2
    1,0
    1,1
    1,2
    2,0
    2,1
    2,2

    etc.. and all your table model has to do, is return the data depending on what is being requested. the table doesnt care about the model, heck you could program this:

    public string getCellIndexAt(x,y){
    return "go away "+x+","+y;
    }

    and no matter what data your model had in it.. your table would look like this:

    Code:
    go away 0,0 | go away 0,1 | go away 0,2
    _________________________________
    go away 1,0 | go away 1,1 | go away 1,2
    and so on..

    -
    so think about what youre actually supposed to be putting in the model..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  9. #24
    Join Date
    Apr 2004
    Posts
    28
    What you are describing is exactly what I am trying to achieve in my program.

    My difficulty has been putting the data set into the array as described.

    The method you refer to is indeed in my class and it reads as follows :


    public Object getValueAt(int row, int col) {
    return data[row][col];
    }

    Rather than put the implementation in here I have tried to put the data into this format so it can be read by the above method.

    In my Employee class I have 21 attributes of which I want to use only 16 in the JTable hence the complicated series of code to extract it from the employee class. All that is doing is creating an Employee instance and equating it to the Employees in turn in my database. There are strings ints and boolean values in these attributes which need wrapper classes to put into the correct format ie Objects.

    I came across this piece of code in my search so I decided to follow it to the letter ish.

    class FillArray {

    public static void main (String args[]) {

    int[][] matrix;
    matrix = new int[4][5];

    for (int row=0; row < 4; row++) {
    for (int col=0; col < 5; col++) {
    matrix[row][col] = row+col;
    }
    }

    }

    }

    Compare this to mine

    data = new Object [DBAccess.allData.size()][16];// creates new array with the correct number of rows and columns required for the table
    for (int i= 0;i<DBAccess.allData.size();i++){
    Employee em = (Employee)DBAccess.allData.get(i);//grabs the first Employee instance at position i
    for (int x=0;x<16;x++){
    data [i][x]= {{""+em.getuserID(),em.getTitle(),em.getInitials(),
    em.getSurname(),em.getRole(),em.getWorkDept(),em.getTel(),
    new Boolean(em.getDoor1()),new Boolean(em.getDoor2()),new Boolean(em.getDoor3()),
    new Boolean(em.getStore()),new Boolean(em.getServerRm()),new Boolean(em.getAny()),
    new Boolean(em.getWorking()),new Boolean(em.getNight()),new Boolean(em.getLocked())}};// this lot grab the necessary attributes from the employee class
    }

    }// then goes back and grabs the next employee and starts again until alla employeees are processed.

    The beauty is that if I take this out of the loop ie use a fixed value for i such as it works

    Employee em = (Employee)DBAccess.allData.get(3)

    data [][]= {{""+em.getuserID(),em.getTitle(),em.getInitials(),
    em.getSurname(),em.getRole(),em.getWorkDept(),em.getTel(),
    new Boolean(em.getDoor1()),new Boolean(em.getDoor2()),new Boolean(em.getDoor3()),
    new Boolean(em.getStore()),new Boolean(em.getServerRm()),new Boolean(em.getAny()),
    new Boolean(em.getWorking()),new Boolean(em.getNight()),new Boolean(em.getLocked())}};

    note the difference is that I leave the [][] fields empty

    On a different note I couldnt help but notice a tone of patronism in your reply. If this is not the case then please accept my apology. I am merely trying to solve a problem with help from those with more experience than me ....nothing more nothing less.

  10. #25
    Join Date
    Apr 2004
    Posts
    28
    Ive solved my own problem

    Here is the code

    public DataTable(){

    data = new Object [DBAccess.allData.size()][16];
    for (int i= 0;i<DBAccess.allData.size();i++){
    Employee em = (Employee)DBAccess.allData.get(i);
    data [i][0]= ""+em.getuserID();
    data [i][1]= em.getTitle();
    data [i][2]= em.getInitials();
    data [i][3]= em.getSurname();
    data [i][4]= em.getRole();
    data [i][5]= em.getWorkDept();
    data [i][6]= em.getTel();
    data [i][7]= new Boolean(em.getDoor1());
    data [i][8]= new Boolean(em.getDoor2());
    data [i][9]= new Boolean(em.getDoor3());
    data [i][10]= new Boolean(em.getStore());
    data [i][11]= new Boolean(em.getServerRm());
    data [i][12]= new Boolean(em.getAny());
    data [i][13]= new Boolean(em.getWorking());
    data [i][14]= new Boolean(em.getNight());
    data [i][15]= new Boolean(em.getLocked());
    }

    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center