-
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
-
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:
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;
}
-
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.
-
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?
-
Just how are you "adding" them?
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.
-
i was indeed, many thanks again!!!
-
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.
-
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.
-
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: