Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using ArrayList to display database with JTable?

807605Oct 10 2007 — edited Oct 12 2007
I have a Swing app that serves as a front-end for a database. So I'm using a JTable, etc. to display the various records and fields in the database.
The problem is this: I'm using a 2-dimensional (fixed) array of objects to store the data:

        private Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Float(0), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Float(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Knitting", new Float(2), new Boolean(false)},
            {"Sharon", "Zakhour",
             "Speed reading", new Float(20), new Boolean(true)},
            {"Philip", "Milne",
             "Pool", new Float(10), new Boolean(false)}
        };
and I would like to be able to ADD a row, if someone selected "Add New Record". I would somehow need to add to the array I already defined.
Well, it turns out that I need to go with ArrayList() in some fashion. The question is, how exactly? It would be easy if this were 1-dimensional, but I have a 2-dimensional array to worry about.

I considered this:
	private java.util.List[] data;

        // There are 32 fields in this database
	data = new ArrayList[32];
			
	// Sample data, so program knows how to render the various fields (it needs to know datatype)
        data[0].set(0, false);
        data[1].set(0, 3);
        data[2].set(0, "Bubba Jones");
But then I'd have to create a new row on EACH of 32 different ArrayLists, one for each field:
        data[0].add(true);
        data[1].add(2);
        data[2].add("Throck Morton");
I need to flip this around or something, so I can have the ArrayList be the record, and add a record with one single statement:
data.add(NewRecord);
Any suggestions? Should I go with an ArrayList of ArrayLists? Create a custom class and make an Arraylist of those?

Thanks,

Matthew
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 9 2007
Added on Oct 10 2007
6 comments
670 views