i'm trying to populate a JList, and i saw that this is how it supose to work

JTable tabel = new JTable();
tabel = new JTable(row, cols);

while (it.hasNext() && it1.hasNext() && it2.hasNext()
						&& it3.hasNext() && it4.hasNext()) {

					Object ob[][] = {
							{ it.next(), it1.next(), it2.next(), it3.next(), it4.next() }}
		
					DefaultTableModel model = new DefaultTableModel(ob, col);
					tabel = new JTable(model);
				}

i have 5 fields which are populated from 5 ArrayLists, the data comes from a MySql Database, in my database i have 5 contact
the problem is that it populates my table only with the first contact. If i move the lines:

DefaultTableModel model = new DefaultTableModel(ob, col);
tabel = new JTable(model);

outside the while, of course, it doesn't recognize ob[][],
if i try just to declare

Object ob[][] = null

outside the while() i get an error.

the only solution i found so far is to write

{ it.next(), it1.next(), it2.next(), it3.next(), it4.next() }

for each element i have in database, which is bad programming, so the final code is

while (it.hasNext() && it1.hasNext() && it2.hasNext()
						&& it3.hasNext() && it4.hasNext()) {

					Object ob[][] = {
							{ it.next(), it1.next(), it2.next(), it3.next(),
									it4.next() },
							{ it.next(), it1.next(), it2.next(), it3.next(),
									it4.next() },
							{ it.next(), it1.next(), it2.next(), it3.next(),
									it4.next() },
							{ it.next(), it1.next(), it2.next(), it3.next(),
									it4.next() },
							{ it.next(), it1.next(), it2.next(), it3.next(),
									it4.next() } };
					DefaultTableModel model = new DefaultTableModel(ob, col);
					tabel = new JTable(model);
				}

does anyone have a suggestion?

Recommended Answers

All 4 Replies

Object[] getField(){
        return list.toArray();
    }

?

i solved the problem, i give u the final code in case someone needs it

while (it.hasNext()) {
					String table[][]= new String[row][];
					for (int i = 0; i < lis.size(); i++) {
						table[i] = new String[5];
						table[i][0] = it.next();
						table[i][1] = it1.next();
						table[i][2] = it2.next();
						table[i][3] = it3.next();
						table[i][4] = it4.next();
					}
					
					DefaultTableModel model = new DefaultTableModel(table, col);
					tabel = new JTable(model);
				}

This is my view

Object[][] data = new Object[][]{list0.toArray(), list1.toArray(), list2.toArray(), list3.toArray(), list4.toArray()};
        //needs switching rows to columns in a 2d array (Object[][] data)
        //In mathematics this is known as the transpose of a matrix,
        //M.transpose(data) - a static method, which makes it, returns transposed data array
       
        DefaultTableModel model = new DefaultTableModel(M.transpose(data), columnNames);
        tabel = new JTable(model);

Your method is nice, make it on fly.
Should be a direct constructor:

DefaultTableModel model = new DefaultTableModel(data, columnNames, transpose /*true*/);

thank you for the suggestion, i'll try that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.