Results 1 to 9 of 9
  1. #1
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default Jtable and Arraylist

    Hi i'm new to these forums and new to java and i was wondering if anyone can help me im getting confused with a program i'm trying to create. I have a Jtable which uses my own attempt at a table model however im getting myself confused as im trying to read in a data from a text file which i can do into an arraylist, its just then getting it into the Jtable as from what i can see which has to be in a two dimensional array which is something iv not played with yet, i can however get this working by just typing the data straight into the code. Can anyone advise me on how to get from my arraylist data to the Jtable.

    Many thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you look into perhaps using a DefaultTableModel, which derives from AbstractTableModel but already has much of the model code fleshed out.

    You might look into creating a method that takes an object that you're working on and converts into an array of Object. For instance say you had a class called "Liquor" that had 4 fields, an int, String, double, and int, you could convert an object of this into an Object array, something that can be easily added to a tablemodel row like so:

    Java Code:
      private Object[] getLiquorRowData(Liquor liquor)
      {
        Object[] rowData = new Object[4];
        rowData[0] = new Integer(liquor.getId());
        rowData[1] = liquor.getDescription();
        rowData[2] = new Double(liquor.getPrice());
        rowData[3] = new Integer(liquor.getAmount());
        return rowData;
      }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, I'm by nature lazy, and if someone has already fixed most of the code for me, and it works well, I'm going to use this. So if this were me, my MyTableModel would extend DefaultTableModel not AbstractTableModel, I'd get rid of the Object[][] arrays and what not, and just use this model. I'd extend it if I had to, but it is pretty much usable right out of the box.

  4. #4
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    Thanks for the advice Fubarable it certainly makes sence to use stuff allready done for me, however im just playing around with code and iv found myself with what i do so im determined to continue on.

    I have another question furthered on kind of from my earlier, i have been messing around with arraylists and i find when i add one to the other i dont get the output i expect:

    First ArrayList: [random1, random2, random3]
    Then added to the second shows : [(this Collection)]

    This is when im just doing println, has anyone any idea why?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Just how are you "adding" them?

    Java Code:
    list1.add(list2); //?
    If so, that's not how you do it. If you have a look at the API you'll find the correct method that will allow you to addAll (hint, hint) of one array list to another.

  6. #6
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    i was indeed, many thanks again!!!

  7. #7
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    Weirdly i think i just understood my own first question, does anyone have any examples of converting a 2D arraylist into a 2d array? Im abit stumped.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    A JTable can have just one column, in which case you don't need two dimensions. From what you are describing, that's the case. If you only have one column, the column index will always be zero, and you can just ignore it.

    In other words, a one column TableModel is extra easy to implement.

  9. #9
    Manofest's Avatar
    Manofest is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    thanks for the suggestion however i am after more then one column just i only put one so that i could just make it easier on myself to get it working, which i am still unable to do :mad:

Similar Threads

  1. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 01:59 AM
  2. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06:45 PM

Posting Permissions

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