i'm struggeling to make my arraylist into an 2D array and then adding it on a table to show the data.
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class Planettabell
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame vindu = new Test();
vindu.setVisible(true);
}
});
}
}
class Test extends JFrame
{
private String[] name = {"Name", "grade"};
Object[][] cell = {{"nameHer", "GradeHer"}};
Object[][] cell2 = {{"nameHer2", "gradeHer2"}};
Object[][] cell3 = {{"nameHer3", "gradeHer3"} };
public Test()
{
setTitle("Planettabell");
setSize(500, 210);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
List<Object[]> list = new ArrayList<Object[]>();
list.add(cell);
list.add(cell2);
list.add(cell3);
Object[][]array = list.toArray(new Object[list.size()][]);
JTable tabell = new JTable(array, name);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(new JScrollPane(tabell), BorderLayout.CENTER);
}
}
i will get this message if i run it
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
this code is working if i add 'cell' instead of 'array' on JTable, but i need the entire array from list to work.
i have also tried:
int number = list.size()/2;
Object[][] ArrayNew = new Object[number][2];
for(int x = 0; x< number; x++)
{
for(int z = 0; z < 2; z++)
{
int y = 2 * x;
ArrayNew [x][z] = list.get(y+z);
}
}
JTable tabell = new JTable(ArrayNew, name);
instead of list.toarray. But then i only gett [[Ljava.lang.Object;@28864ae7 and [[Ljava.lang.Object;@49214a13 where the text in the table supposed to be.
would appreicate any answer :)