gpt4 book ai didi

java - 使用本地 JSON 文件填充 Jtable

转载 作者:行者123 更新时间:2023-12-02 02:55:13 24 4
gpt4 key购买 nike

我试图从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();
}
});
}

最佳答案

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

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数据的逻辑。

关于java - 使用本地 JSON 文件填充 Jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com