[英]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. 我正在努力使我的arraylist变成2D数组,然后将其添加到表上以显示数据。
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
如果Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
运行Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
我将收到此消息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. 如果我在JTable上添加“ cell”而不是“ array”,此代码将起作用,但是我需要从列表中获取整个数组才能工作。
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. 而不是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. 但是然后我只得到[[Ljava.lang.Object; @ 28864ae7和[[Ljava.lang.Object; @ 49214a13],其中表中的文本应该是。
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): 您的列表实际上是3D数据结构(2D数组的列表),它应该只有2D(数组的列表):
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"}};
在您的代码中, Object[][] cell = {{"nameHer", "GradeHer"}};
is a 2D array, then you add it into a list (making your list 3 dimensionnal in the process). 是2D数组,然后将其添加到列表中(使列表在此过程中成为3维)。 Your cells shouldn't be 2D, they represent your rows and must be1D arrays. 您的单元格不应该是2D,它们代表您的行,并且必须是1D数组。
Replace by Object[] cell = {"nameHer", "GradeHer"};
替换为Object[] cell = {"nameHer", "GradeHer"};
and it will work 它会工作
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. DefaultTableModel使用2d数组,并为您显示所有内容,包括列标题。 So, without seeing the code to your KarakterTabell I can't imagine what more you're trying to achieve. 因此,在没有看到KarakterTabell的代码的情况下,我无法想象您还想实现什么。
To do a table model correctly all you need to do is have a means to access your data in an x,y fashion. 要正确地建立表模型,您所需要做的就是以一种x,y方式访问数据。 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. 还要记住:这是公共对象 getValueAt()- JTable将把此调用返回的所有内容的“ toString()”调用放入单元格中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.