I have an arrayList, consisting of a name, followed by a score, corresponding to the name. I would like to show this information in a Jtable, but there seems to be a problem. My table only shows 2 rows. Here's the code:
int numberOfScores = allScores.size()/6; //arrayList of a username, followed by a score
Object[][] newArrayContent = new Object[numberOfScores][6];
for(int x = 0; x<numberOfScores; x++){
for(int z = 0; z < 6; z++){
int y = 6 * x;
newArrayContent [x][z] = allScores.get(y+z);
System.out.println(newArrayContent [x][z].toString());
}
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object rowData[][] = newArrayContent;
Object columnNames[] = { "username", "score"};
JTable table = new JTable(newArrayContent, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
I noticed that if I add 2 more columns in columnNames[]
, I get 4 results, instead of 2, but they're horizontal in the table under another username column and another scores column. I just want an ordinary table of 2 columns and 20-30 rows. Could anyone help?
There are some slight adjustments you can make and you should be good to go on what you're wanting.
Results: