JTable 只显示导入数据的第一列。我如何显示所有列?

问题描述 投票:0回答:0

我正在从 csv 表中构建一个导入值表。我有 95 行和 26 列。我想在我的 JTable 中显示所有“dataS”值,但我只得到第一列。

我存储数据值的方式有问题吗?

谢谢!

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.*;


public class mealBuilder {

    public static void main(String[] args) throws FileNotFoundException {


        File myObj = new File("NutrData.csv");
        Scanner myReader = new Scanner(myObj);
        ArrayList<String[]> records = new ArrayList<String[]>();
        String[] record = new String[2];

        while (myReader.hasNextLine()) {
            record = myReader.nextLine().split(",");
            records.add(record);
        }

        String[] columnNames = new String[]{Arrays.toString(records.get(0))}

        Object[][] dataS = new Object[records.size()][records.get(0).length];

        for (int i = 0; i < records.size() - 1; i++) {
            for (int j = 0; j < records.get(0).length - 1; j++) {
                dataS[i][j] = records.get(i)[j];
            }
        }

            JFrame frame = new JFrame("Meal-Builder");
            JPanel panel = new JPanel(new BorderLayout());


            JTable foodMenu = new JTable();
            foodMenu = new JTable(dataS, columnNames);
            JScrollPane scroll = new JScrollPane(foodMenu, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setLayout(null);
            frame.setSize(1400, 900);


            panel.setSize(700, 400);
            panel.setLocation(25, 25);
            foodMenu.setPreferredScrollableViewportSize(new Dimension(600, 400));

            frame.add(panel);
            panel.add(scroll, BorderLayout.CENTER);
            frame.setVisible(true);
        }
    }

java multidimensional-array jtable jscrollpane
© www.soinside.com 2019 - 2024. All rights reserved.