2017-04-04 16 views
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(); 
     } 
    }); 
} 
+3

'私がループ内のjtableのデータを埋めようとすると、データをフレームに乗算してしまいました。ループの繰り返しごとに新しいテーブルとフレームを作成するのはなぜですか?ループの実行が完了した後に、1つのテーブルと1つのフレームを作成する必要があります。基本的なロジックは、ループが開始する前に空のDefaultTableModelを作成することです。ループの中で、DefaultTableModelの 'addRow(...)'メソッドを使ってモデルにデータを追加します。ループが終了したら、TableModelを使用してテーブルを作成し、テーブルをフレームに追加します。 – camickr

+0

ループ内のデータにアクセスできますか? – LyodMichael

+0

ありがとうございます! – LyodMichael

答えて

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ファイルの形式はどのようなものか分かりませんが、「概念」は同じである必要があります。

したがって、1行のデータを読み込み、そのデータをJSONデータの行を解析するロジックで解析するロジックを置き換えます。

関連する問題