在构造函数外部添加元素到数组

时间:2012-12-02 08:33:34

标签: java arrays listview

我希望获取数组中的所有值,并以不通过单个调用存储的方式将它们存储到listview中。 以下是我的代码,有人帮我修理吗? 是否有一个变量可以表示数组的所有内容

这是我的代码:

String[] TKA = new String[100];

    JTable table;  
    Object tableData[][]={  
            {TKA[1],TKA[2],TKA[3],TKA[4],TKA[5]},
            {TKA[6],TKA[7],TKA[8],TKA[9],TKA[10]},
            {TKA[11],TKA[12],TKA[13],TKA[14],TKA[15]},
            {TKA[16],TKA[17],TKA[18],TKA[19],TKA[20]},
            {TKA[21],TKA[22],TKA[23],TKA[24],TKA[25]},
            {TKA[26],TKA[27],TKA[28],TKA[29],TKA[30]},
            {TKA[31],TKA[32],TKA[33],TKA[34],TKA[35]},
            {TKA[36],TKA[37],TKA[38],TKA[39],TKA[40]},
            {TKA[41],TKA[42],TKA[43],TKA[44],TKA[45]},
            {TKA[46],TKA[47],TKA[48],TKA[49],TKA[50]},
            {TKA[51],TKA[52],TKA[53],TKA[54],TKA[55]},
            {TKA[56],TKA[57],TKA[58],TKA[59],TKA[60]},
            {TKA[61],TKA[62],TKA[63],TKA[64],TKA[65]},
            {TKA[66],TKA[67],TKA[68],TKA[69],TKA[70]},
            {TKA[71],TKA[72],TKA[73],TKA[74],TKA[75]},
            {TKA[76],TKA[77],TKA[78],TKA[79],TKA[80]},
            {TKA[81],TKA[82],TKA[83],TKA[84],TKA[85]},
            {TKA[86],TKA[87],TKA[88],TKA[89],TKA[90]},
            {TKA[91],TKA[92],TKA[93],TKA[94],TKA[95]},
            {TKA[96],TKA[97],TKA[98],TKA[99],TKA[100]},             
    };  
    String[] columnTitle = { "AA","BB","CC","DD","EE" }; 

        table = new JTable(tableData , columnTitle);
        JScrollPane jt = new JScrollPane(table);
        add(jt);
        jt.setBounds(10, 200, 700, 500);

2 个答案:

答案 0 :(得分:2)

创建一个新的TableModel,用于定义您希望表格的显示方式。

<强>应用

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

    public class StackExample {

        public static void main(String[] args) {

            MyTableModel model = new MyTableModel("This is some string that I want to show");

            // Create a JTable and tell it to display our model
            JTable table = new JTable(model);

            // Display it all in a scrolling window and make the window appear
            JFrame frame = new JFrame("FileTableDemo");
            frame.getContentPane().add(new JScrollPane(table), "Center");
            frame.setSize(600, 400);
            frame.setVisible(true);
        }
    }

<强>的TableModel

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private String data;
    String[] columnTitle = { "AA","BB","CC","DD","EE" }; 

    public MyTableModel(String data) {
        this.data = data;
    }

    @Override
    public int getRowCount() {
        return data.length() / 5;
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.charAt((rowIndex * 5) + columnIndex);
    }

    @Override
    public String getColumnName(int column) {
        return columnTitle[column];
    }
}

答案 1 :(得分:1)

改为使用集合,然后转换为String[][]

ArrayList<String[]> temp = new ArrayList<String[]>();

int i = 0;
String innerArray;
while( i < TKA.length){
    innerArray = new String[5];
    for(int j = 0 ; j < innerArray.length ; j++){
        innerArray[j] = TKA[i];
        i++;
    }

    temp.add(innerArray);
}

String[][] result = temp.toArray(new String[0][0]);