سؤال

ولدي بيانات CSV (في الحقيقة يتم فصل القيم عن طريق حرف جدولة، ولكن يمكنني استخدام ; كفاصل). يتم تخزين البيانات في كائن String.

هل هناك طريقة بسيطة لإنشاء JTable مع هذه البيانات (دون القيام بذلك يدويا من خلال قراءة وتحليل الكائن String

(ملاحظة: مشروعي يستخدم جافا 1.4، ولكن إذا كان لديك الحل الذي يحتاج جافا 1.5، وسأكون سعيدا على أي حال)

هل كانت مفيدة؟

المحلول

في TableModelExtTextLoader من swinglabs سيفعل لك. وهو يدعم كل من علامة التبويب والفاصلة النص فصل.

نصائح أخرى

وانها السهل بما فيه الكفاية للقيام بذلك دون وجود مكتبة الخارجية. هنا مثال باستخدام فئة الماسح الضوئي من جاوة 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);
}

و}

ما لم يكن هناك CSV -> طريقة tablemodel هناك في مكان ما سيكون لديك لكتابة التعليمات البرمجية لملء نموذج نفسك للأسف

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top