简体   繁体   中英

Arraylist to 2D array and then to a JTable

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

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

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);
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM