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

2
  • Possible duplicate of this question. If not, then it might provide some inspiration :) Commented May 3, 2013 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 Commented May 3, 2013 at 21:30

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

i still get "[Ljava.lang.Object@478e2443" where the table supposed to be, when trying to run the program
yes, i have changed the code in my question to a little program
I think you missed part of the correction in my answer, look at the declaration of your cells ;)
hey, thx for answering again :] it does work with that correction. Thanks again, i really appreicate it :)
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);
  }
}

3 Comments

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.
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.