使用本地JSON文件填充Jtable

时间:2017-04-04 01:55:38

标签: java json swing

我试图从json文件获取所有数据并将数据填充到jtable,我已经从json文件获取数据并打印输出但是当我尝试填充数据时我无法将数据放在jtable上循环中的jtable我最终将数据的帧放在一起。请帮帮我,我的代码如下:

我已经导入了所有需要的jar。

public Main(){
    super(new GridLayout(1,0));
    BufferedReader br = null;
    JSONParser parser = new JSONParser();
    String inputline;
    try {
        br = new BufferedReader(new FileReader("/Users/lyod/Documents/sample.json"));
        try {
            String id = null,
            component = null,
            title = null,
            lat = null,
            lng = null,
            cost = null,
            status = null;
            Object[][] data;
            while ((inputline = br.readLine()) != null) {
                JSONArray a = (JSONArray) parser.parse(inputline);
                String[] columns = new String[] {
                    "Id", 
                    "Title",
                    "Component",
                    "LAT",
                    "LNG",
                    "Cost"
                };
                for (Object o : a) {
                    JSONObject sample = (JSONObject) o;
                    id = (String) sample.get("id");
                    component = (String) sample.get("component");
                    title = (String) sample.get("title");
                    lat = (String) sample.get("lat");
                    lng = (String) sample.get("lng");
                    cost = (String) sample.get("cost");
                    status = (String) sample.get("status");
                    Object[][] data = new Object[][] {
                        {id,title,component,lat,lng,cost, false },
                    };
                }
                JTable table = new JTable(data, columns);
                add(new JScrollPane(table));
                JFrame frame = new JFrame("test v2");
                frame.add(table);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Main();
        }
    });
}

1 个答案:

答案 0 :(得分:2)

以下是从常规文本文件中填充数据的示例。它演示了从循环中的文件读取数据然后在循环结束后创建表的概念。

import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableFromFile extends JPanel
{
    public TableFromFile()
    {
        setLayout( new BorderLayout() );

        JTable table = new JTable( getTableModel() );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private TableModel getTableModel()
    {
        String delimiter = ":";
        DefaultTableModel model = new DefaultTableModel();

        try
        {
            BufferedReader reader = getFileReader();

            //  First line will contain the column names

            String line = reader.readLine();
            model.setColumnIdentifiers( line.split(delimiter) );

            //  Remaining lines in the file will be the data

            while ((line = reader.readLine()) != null)
            {
                model.addRow( line.split(delimiter) );
            }

            reader.close();
        }
        catch(Exception e) { System.out.println(e); }


        return model;
    }

    private BufferedReader getFileReader()
    {
        //  Create data to simulate reading data from a file

        String data =
            "Letter:Number\n" +
            "A:1\n" +
            "B:2\n" +
            "C:3";

        BufferedReader reader = new BufferedReader( new StringReader( data ) );

        //  In your real application the data would come from a file

        //Reader reader = new BufferedReader( new FileReader(...) );

        return reader;
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Table From File");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TableFromFile() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

我不知道JSON文件的格式是什么样的,但"概念"应该是一样的。

因此,替换读取单行数据的逻辑,并使用解析一行JSON数据的逻辑解析数据。