0
で埋めて、jsonファイルからすべてのデータを取得し、jtableにデータを書き込もうとしていますが、すでにjsonファイルとプリントアウトからデータを取得していますが、ループ内のjtableのデータを埋めるようにしてください。フレームを乗算してデータをインストールしました。このコードで私を助けてください。私のコードは迷っています:jsonをローカルのJSONファイル
私はすでに必要なすべての瓶をインポートしました。
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();
}
});
}
'私がループ内のjtableのデータを埋めようとすると、データをフレームに乗算してしまいました。ループの繰り返しごとに新しいテーブルとフレームを作成するのはなぜですか?ループの実行が完了した後に、1つのテーブルと1つのフレームを作成する必要があります。基本的なロジックは、ループが開始する前に空のDefaultTableModelを作成することです。ループの中で、DefaultTableModelの 'addRow(...)'メソッドを使ってモデルにデータを追加します。ループが終了したら、TableModelを使用してテーブルを作成し、テーブルをフレームに追加します。 – camickr
ループ内のデータにアクセスできますか? – LyodMichael
ありがとうございます! – LyodMichael