0

I got this list which contains some data that I gain from a file.

List<String> destinationList = new ArrayList<String>();

I then have a JTable which I want to transfer the data from the list into. I saw an example on how to enter data into JTable:

Object[][] data = {
    {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
    {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
    {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
    {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
    {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
};

I am wondering if I somehow can past the data from my list into the Object[][] data so I can then transfer it into my JTable. Thanks in advance!

Tom
  • 16,842
  • 17
  • 45
  • 54
Fjondor
  • 39
  • 7
  • You need to show us what the `String`s in `destinationList` look like. By the way, you can just write `{"Kathy", "Smith", "Snowboarding", 5, false}`. – Paul Boddington Dec 30 '14 at 14:04
  • Iterate through the list and ad element to array as you wish – Thusitha Thilina Dayaratne Dec 30 '14 at 14:07
  • Okay, the thing above was just an example from the API. But the Strings in destinationList are just Strings, not sure what u mean.. the list is filled with locations like: "Paris" "New York"... etc – Fjondor Dec 30 '14 at 14:08
  • If you have a List of Strings, then maybe you should be using a `JList` since it seems to me that you only have a single column of data. Storing row/column data in a single List is not a very good design. – camickr Dec 30 '14 at 15:52

1 Answers1

3

I am wondering if I somehow can past the data from my list into the Object[][] data so I can then transfer it into my JTable.

Instead of wasting a single minute in tryining to do that, I'd suggest you:

  1. Create a bean class that wraps that info in a OO way (i.e.: Contact).
  2. Convert each string in your list in a Contact object. Perhaps String#split() might help in getting the fields separated.
  3. Use a more sophisticated table model than DefaultTableModel, that allows you to work directly with beans (objects). Something like DataObjectTableModel or Rob Camick's RowTableModel / ListTableModel / BeanTableModel
  4. Set your list as the underlying data in the table model.

This approach will save you time for example when you want to use the selected contacts info. Not to mention that is a better practice than working with arrays, in terms of OOP.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Not really sure how to do all that but will this make it able to change the table incase the data changes from the file from where I am getting the data. To add lines or to remove. Never heard of bean class either :s – Fjondor Dec 30 '14 at 14:38
  • *Not really sure how to do all that...* Make a step at the time and ask for help in the process. Perhaps you can show how does a line in the text file look like. *...will this make it able to change the table incase the data changes from the file from where I am getting the data.* No. If the file is modified then you'll have to re-read the file and update the data in your table model. *Never heard of bean class* A bean class is just a POJO with attributes (or class members) and getters and setters to access them from the outside. @Fjondor – dic19 Dec 30 '14 at 15:01