2009-09-14 127 views
2

我有CSV數據(實際上這些值由製表符分隔,但我可以使用;作爲分隔符)。數據存儲在String對象中。使用CSV數據填充JTable的簡單方法

是否有簡單的方法來創建一個帶有這些數據的JTable(沒有通過讀取和解析String對象手動執行)?

(注:我的項目是使用Java 1.4,但如果你有需要的Java 1.5的解決方案,我會反正高興)

回答

2

TableModelExtTextLoader from swinglabs會做你。它支持製表符和逗號分隔的文本。

+1

OpenCSV也是一個簡單的方法來獲取它:http://stackoverflow.com/questions/10466079/import-csv-to-jtable – Benj 2013-08-14 10:10:17

1

,除非有一個CSV - > TableModel的方法有某處你將不得不編寫代碼來自己填充模型。

+0

這就是我期待的;) 我已經實現了手工代碼創建此表,但我想看看是否有一個更簡單的方法來做到這一點... – romaintaz 2009-09-14 08:36:39

2

沒有外部庫很容易做到這一點。下面是使用Scanner類從Java 1.5的例子:

import java.io.*; 
import java.net.URL; 
import java.util.Scanner; 
import javax.swing.*; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.DefaultTableModel; 

public class CSVTable extends JFrame { 
    JTable table; 
    DefaultTableModel model; 
    JButton closeButton, webButton; 
    /** 
    * Takes data from a CSV file and places it into a table for display. 
    * @param source - a reference to the file where the CSV data is located. 
    */ 
    public CSVTable(String title, String source) { 
    super(title); 
    table = new JTable(); 
    JScrollPane scroll = new JScrollPane(table); 
    String[] colNames = { "LastName", "FirstName", "Email Address", "Dept."}; 
    model = new DefaultTableModel(colNames, 0); 
    InputStream is; 
    try { 
     if(source.indexOf("http")==0) { 
      URL facultyURL = new URL(source); 
      is = facultyURL.openStream(); 
     } 
     else { //local file? 
      File f = new File(source); 
      is = new FileInputStream(f); 
     } 
     insertData(is); 
     //table.getColumnModel().getColumn(0).setCellRenderer(new CustomCellRenderer()); 
    } 
    catch(IOException ioe) { 
     JOptionPane.showMessageDialog(this, ioe, "Error reading data", JOptionPane.ERROR_MESSAGE); 
    } 

    JPanel buttonPanel = new JPanel(); 
    closeButton = new JButton("Close"); 
    webButton = new JButton("Proctinator.com"); 
    buttonPanel.add(closeButton); 
    buttonPanel.add(new JLabel(" You can download this file from our site: ")); 
    buttonPanel.add(webButton); 

    JPanel notesPanel = new JPanel(); 
    JLabel note1 = new JLabel(" Make sure that your list is formatted exactly as shown below, including the *markers between categories "); 
    JLabel note2 = new JLabel(" Be sure to place each faculty member into the correct category: *Teacher, *Subs, *TeacherAids, *TeacherAssistants "); 
    JLabel note3 = new JLabel(" Note that the your faculty list must be a plain text file: Export to either CSV or tab delimited format."); 
    BoxLayout layout = new BoxLayout(notesPanel, BoxLayout.Y_AXIS); 
    notesPanel.setLayout(layout); 
    notesPanel.add(note1); 
    notesPanel.add(note2); 
    notesPanel.add(note3);  
    getContentPane().add(notesPanel, BorderLayout.NORTH); 
    getContentPane().add(scroll, BorderLayout.CENTER); 
    getContentPane().add(buttonPanel, BorderLayout.SOUTH); 
    pack(); 
} 

/** 
* Places the data from the specified stream into this table for display. The data from the file must be in CSV format 
* @param is - an input stream which could be from a file or a network connection or URL. 
*/ 
void insertData(InputStream is) { 
    Scanner scan = new Scanner(is); 
    String[] array; 
    while (scan.hasNextLine()) { 
     String line = scan.nextLine(); 
     if(line.indexOf(",")>-1) 
      array = line.split(","); 
     else 
      array = line.split("\t"); 
     Object[] data = new Object[array.length]; 
     for (int i = 0; i < array.length; i++) 
      data[i] = array[i]; 

     model.addRow(data); 
    } 
    table.setModel(model); 
} 

public static void main(String args[]) { 
    CSVTable frame = new CSVTable("Faculty List Example","http://proctinator.com/help/faculty.csv"); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

}

+0

感謝您的答覆,但我的問題是如何做到這一點*沒有*重新發明輪子。從CSV文件中提取數據很容易,並將它們插入到JTable中。 – romaintaz 2012-05-06 13:35:25