Java 如何在JTable from.csv文件中向列添加标题

Java 如何在JTable from.csv文件中向列添加标题,java,swing,csv,jtable,Java,Swing,Csv,Jtable,我想在.csv文件的JTable列中添加标题 您可以在图片上看到我的.csv文件 我需要删除这段代码: private String[]columnNames={“国家”、“首都”、“人口”}并使用另一个函数代替它,该函数可用于从.csv文件中的“我的列名”中获取列名 我的主要班级: public class App extends JFrame { private Object[][] data; private String[] columnNames = { "Country", "Ca

我想在.csv文件的JTable列中添加标题

您可以在图片上看到我的.csv文件

我需要删除这段代码:
private String[]columnNames={“国家”、“首都”、“人口”}
并使用另一个函数代替它,该函数可用于从.csv文件中的“我的列名”中获取列名

我的主要班级:

public class App extends JFrame {
private Object[][] data;
private String[] columnNames = { "Country", "Capital", "Population" };
private DefaultTableModel tableModel;
private JTable table;
private CountryList myList;

public App(String title) {
    super(title);
    setBounds(10, 10, 400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myList = new CountryList();
    myList.readFromCSV("data/country.csv");
    data = myList.convert2Data();
    tableModel = new DefaultTableModel(data, columnNames);
    table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setPreferredSize(new Dimension(380, 280));
    JPanel panel = new JPanel();
    panel.add(scrollPane);
    add(panel, BorderLayout.CENTER);
}

public static void main(String[] args) {
    App myApp = new App("Basic JTable");
    myApp.setVisible(true);
}
}
还有我的班级名单:

public class CountryList {
private ArrayList<Country> books;

public CountryList() {
  books = new ArrayList<Country>();
}

public void add(Country sb) {
    books.add(sb);
}

public void readFromCSV(String filename) {
    File file = new File(filename);
    FileReader reader = null;
    try {
        reader = new FileReader(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    }
    BufferedReader infile = new BufferedReader(reader);
    String line = "";
    try {
        boolean done = false;
        while (!done) {
            line = infile.readLine();
            if (line == null) {
                done = true;
            } else {
                String[] tokens = line.trim().split(";");
                String country = tokens[0].trim();
                String capital = tokens[1].trim();
                int population = Integer.parseInt(tokens[2].trim());
                Country sb = new Country(country, capital, population);
                books.add(sb);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
公共类国家列表{
私人ArrayList书籍;
公共国家名单(){
books=新数组列表();
}
公共空间添加(国家/地区sb){
加上(某人);
}
public void readFromCSV(字符串文件名){
文件=新文件(文件名);
FileReader=null;
试一试{
reader=新文件读取器(文件);
}catch(filenotfounde异常){
e、 printStackTrace();
系统出口(1);
}
BufferedReader infle=新的BufferedReader(读卡器);
字符串行=”;
试一试{
布尔完成=假;
而(!完成){
line=infle.readLine();
如果(行==null){
完成=正确;
}否则{
String[]tokens=line.trim().split(;);
字符串country=tokens[0]。trim();
字符串大写=令牌[1]。trim();
int population=Integer.parseInt(标记[2].trim());
国家sb=新国家(国家、首都、人口);
加上(某人);
}
}
}捕获(IOE异常){
e、 printStackTrace();
系统出口(1);
}
}

您可以读取csv文件并将第一行添加到JTable标题中。 将以下方法添加到CountryList类:

public String[] getColumnNames(String csvFileDestination){
    BufferedReader br = null;
    String line = "";
    String[] columnNames;

    try {

        br = new BufferedReader(new FileReader(csvFileDestination));
        while ((line = br.readLine()) != null) {

            // use comma as separator
            columnNames = line.split(",");
            break;// Breaking out because you only need the first row

        }
        return columnNames;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
           try {
               br.close();
            } catch (IOException e) {
               e.printStackTrace();
           }
        }
    }
    return null;
}
并将private
String[]columnNames={“Country”、“Capital”、“Population”};
替换为
private String[]columnNames;
并在构造函数中添加以下内容:
columnNames=myList.getColumnNames(“/path/to/csv/file.csv”);