0

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 :)

Madde
  • 471
  • 1
  • 7
  • 22
  • Possible duplicate of [this question](http://stackoverflow.com/questions/2146488/converting-an-arraylist-into-a-2d-array). If not, then it might provide some inspiration :) – davmos May 03 '13 at 21:06
  • i have seen the question before and i have tried the answers, but sadly no one of them seems to work for me – Madde May 03 '13 at 21:30

3 Answers3

2

Your list is effectively a 3D data structure (a list of 2D arrays), it should be only 2D (a list of arrays):

Object[] information = {"nameHer", "GradeHer"};
List<Object[]> list = new ArrayList<Object[]>();
list.add(information); // more data here 

Object[][]array = list.toArray(new Object[list.size()][]);

In your code, Object[][] cell = {{"nameHer", "GradeHer"}}; is a 2D array, then you add it into a list (making your list 3 dimensionnal in the process). Your cells shouldn't be 2D, they represent your rows and must be1D arrays.

Replace by Object[] cell = {"nameHer", "GradeHer"}; and it will work

Guillaume
  • 14,306
  • 3
  • 43
  • 40
  • i still get "[Ljava.lang.Object@478e2443" where the table supposed to be, when trying to run the program – Madde May 03 '13 at 21:46
  • yes, i have changed the code in my question to a little program – Madde May 04 '13 at 00:16
  • I think you missed part of the correction in my answer, look at the declaration of your cells ;) – Guillaume May 04 '13 at 09:15
  • hey, thx for answering again :] it does work with that correction. Thanks again, i really appreicate it :) – Madde May 04 '13 at 17:39
2

Planettabell

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[][] cells = {
      {"nameHer", "GradeHer"},
      {"nameHer2", "gradeHer2"},
      {"nameHer3", "gradeHer3"}
  };

  public Test()
  {
    setTitle("Planettabell");
    setSize(500, 210);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable tabell = new JTable(cells, name);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(new JScrollPane(tabell), BorderLayout.CENTER);
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • hi, thx for answering, the only problem is that i need it to come from the arraylist since my real program don't know how many people(which can be more or less at any moment) it is in the system. and therfore i will not know how many cells it will be. – Madde May 04 '13 at 02:37
  • So either 1) build the `ArrayList` into a 2D array just before insertion into the table or 2) create a `TableModel` that will accept the `ArrayList`. No problems, just alternatives. – Andrew Thompson May 04 '13 at 02:42
  • i'm trying to convert the arraylist into a 2D array, if that is what you mean? but it dosen't seem to work as you can see in the program. – Madde May 04 '13 at 02:45
1

You're approaching this problem entirely wrong.

a DefaultTableModel takes a 2d array and displays everything for you including column headers. So, without seeing the code to your KarakterTabell I can't imagine what more you're trying to achieve.

To do a table model correctly all you need to do is have a means to access your data in an x,y fashion. Then, pass in the data stream into this new model it will run when the table comes up:

public class KarakterTableModel implements TableModel {
    List<String[]> data = new ArrayList<String[]>(); 

    public KarakterTableModel(BufferedReader reader) {
      while(reader.ready()) {

        String columnData = reader.readLine();
        String[] columns = columnData.split(" ");
        data.add(columns);
      }
    }
    public Object getValueAt(int x, int y) {
       String[] row = data.get(x);
       return row[y];
    }

}

JTable table = new Jtable(new KarakterMode(new BufferedReader(System.in));

Also remember: it's public Object getValueAt() -- the JTable will put the "toString()" call of whatever is returned from this call into the cell.

Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76