2017-04-04 96 views
0

正試圖從json文件中獲取所有數據並將數據填充到jtable,我已經從json文件和打印輸出中獲取數據,但是我無法將數據放在jtable上,當我嘗試填充循環內的jtable上的數據,我最終將數據放入框架。請幫助我,我的代碼如下:用本地JSON文件填充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(); 
     } 
    }); 
} 
+3

'當我嘗試填充循環內的jtable上的數據時,我最終將數據放大到框架 - 完全相同。爲什麼你會爲循環的每次迭代創建一個新的表格和框架?您應該只在循環完成執行後創建一個表格和一個框架。所以基本的邏輯是在循環開始之前創建一個空的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文件的格式是什麼樣的,但「概念」應該是相同的。

因此,替換讀取單行數據並使用解析一行JSON數據的邏輯來解析數據的邏輯。