用本地 JSON 文件填充 Jtable [英] Fill Jtable with local JSON file

查看:28
本文介绍了用本地 JSON 文件填充 Jtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

am trying to get all the data from a json file and fill the data to a jtable, I already get the data from the json file and printout but i cant put the data on the jtable when i try to fill the data on the jtable inside the loop i ended up multiplying the frame insted the data. please help me on this my code are bellow:

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

I already imported all jar needed.

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();
        }
    });
}

推荐答案

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

Here is an example the populates data from a regular text file. It demonstrates the concepts of reading data from a file within a loop and then create the table after the loop is finished.

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 文件的格式是什么样的,但概念"应该是相同的.

I don't know what the format of a JSON file is like but the "concept" should be the same.

因此将读取一行数据并解析数据的逻辑替换为解析一行 JSON 数据的逻辑.

So replace the logic that read a single line of data and parses the data with the logic that parses a line of JSON data.

这篇关于用本地 JSON 文件填充 Jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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