使用ArrayList创建jTable [英] Create jTable with ArrayList

查看:59
本文介绍了使用ArrayList创建jTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一段时间,但找不到解决问题的方法.

I searched for a while but I cant find a solution for my problem.

我想在JTable中显示ArrayList的值,对此我很陌生,无法修复错误.

I want to display the Values of an ArrayList in a JTable, i'm pretty new to this and cant fix the error.

package Statistik;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Statistic {

    private static ArrayList<String> rowA = new ArrayList();
    private static ArrayList<String> rowB = new ArrayList();
    private static ArrayList<String> rowC = new ArrayList();
    private static ArrayList<String> titel = new ArrayList();
    private static ArrayList<Object> table = new ArrayList();

    public static void main(String[] args) {

        titel.add("Name");
        titel.add("Art der Bearbeitung");
        titel.add("Datum");

        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");

        table.add(rowA);
        table.add(rowB);
        table.add(rowC);


        // Das JTable initialisieren
        JTable EndTable = new JTable( table , titel );

        JFrame frame = new JFrame( "Demo" );
        frame.getContentPane().add(new JScrollPane( EndTable ) );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
    }

    public static void addRows(String rowa, String rowb, String rowc) {

        rowA.add(rowa);
        rowB.add(rowb);
        rowC.add(rowc);

    }

}

我不能将ArrayList table设置为EndTable中的第一个值,但是我不知道应该怎么做.

I can't set the ArrayList table as first Value in my EndTable, but i dont know how I should do otherwise.

谢谢大家尝试回答我的问题.

Thank you all for trying to answer my problem.

修改

我的目标是创建一个列表

My goal is to make a List with

实体名称,变化的艺术,日期

Entity-Name, art of change, Date

所以我认为最好使用ArrayList,因为它很灵活. 它必须具有灵活性,因为我们不知道用户将更改多少.

so I thought it would be the best to use an ArrayList because it's flexible. It have to be flexible because we dont know how much the user will change.

推荐答案

不会,因为JTable没有为ArrayList保留的参数,但是可以解决这个问题!您知道JTable的参数也是(Object[][], object[])

Nope since , JTable has no reserved argument for ArrayList, but a trick will solve it ! you know that the arguments for JTable is also (Object[][], object[])

      Object[] tempTitel = titel.toArray(); // return Object[]

      String[][] tempTable = new String[table.size()][]; 

     int i = 0;
     for (List<String> next : table) {
      tempTable[i++] = next.toArray(new String[next.size()]); // return Object[][]
    }



   JTable EndTable = new JTable(tempTable,tempTitel);

请注意,我将ArrayList<String> table= new ArrayList()更改为ArrayList<ArrayList<String>> table = new ArrayList(); 所以当结合它时:

Note that I change ArrayList<String> table= new ArrayList() to ArrayList<ArrayList<String>> table = new ArrayList(); so when combine it :

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Statistic {

    private static ArrayList<String> rowA = new ArrayList();
    private static ArrayList<String> rowB = new ArrayList();
    private static ArrayList<String> rowC = new ArrayList();
    private static ArrayList<String> titel = new ArrayList();
    private static ArrayList<ArrayList<String>> table = new ArrayList();

    public static void main(String[] args) {

        titel.add("Name");
        titel.add("Art der Bearbeitung");
        titel.add("Datum");

        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");
        addRows("buchung", "Created", "10.10.10");

        table.add(rowA);
        table.add(rowB);
        table.add(rowC);

          Object[] tempTitel = titel.toArray();
          String[][] tempTable = new String[table.size()][];
       int i = 0;
       for (List<String> next : table) {
       tempTable[i++] = next.toArray(new String[next.size()]);
        }

       JTable EndTable = new JTable(tempTable,tempTitel);

        JFrame frame = new JFrame("Demo");
        frame.getContentPane().add(new JScrollPane(EndTable));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void addRows(String rowa, String rowb, String rowc) {

        rowA.add(rowa);
        rowB.add(rowb);
        rowC.add(rowc);

    }

}

这篇关于使用ArrayList创建jTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆