Results 1 to 9 of 9
Thread: Jtable and Arraylist
- 12-29-2008, 08:58 PM #1
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:
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; }
-
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.
- 12-30-2008, 01:01 AM #4
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?
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.Java Code:list1.add(list2); //?
- 12-30-2008, 01:23 AM #6
i was indeed, many thanks again!!!
- 12-30-2008, 01:45 AM #7
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.
- 12-30-2008, 05:49 PM #8
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.
- 12-30-2008, 09:20 PM #9
Similar Threads
-
Arraylist
By gnarly hogie in forum New To JavaReplies: 2Last Post: 12-11-2008, 01:59 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:47 AM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks