2017-12-20 19 views
0

.csvファイルからJTable列のヘッダーを追加したいとします。.csvファイルからJTableの列にヘッダーを追加する方法

私の.csvファイルが画像に表示されます。

CSV file

私はこのコードのフラグメントを削除する必要があります。 private String[] columnNames = { "Country", "Capital", "Population" };し、代わりにそれを.CSVファイル内の列の私の名前から列の名前を取得するために利用され、別の機能を置きます。

私のメインクラス:

public class App extends JFrame { 
private Object[][] data; 
private String[] columnNames = { "Country", "Capital", "Population" }; 
private DefaultTableModel tableModel; 
private JTable table; 
private CountryList myList; 

public App(String title) { 
    super(title); 
    setBounds(10, 10, 400, 300); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myList = new CountryList(); 
    myList.readFromCSV("data/country.csv"); 
    data = myList.convert2Data(); 
    tableModel = new DefaultTableModel(data, columnNames); 
    table = new JTable(tableModel); 
    table.setAutoCreateRowSorter(true); 
    JScrollPane scrollPane = new JScrollPane(table); 
    scrollPane.setPreferredSize(new Dimension(380, 280)); 
    JPanel panel = new JPanel(); 
    panel.add(scrollPane); 
    add(panel, BorderLayout.CENTER); 
} 

public static void main(String[] args) { 
    App myApp = new App("Basic JTable"); 
    myApp.setVisible(true); 
} 
} 

そして、私のクラスCountryList:

public class CountryList { 
private ArrayList<Country> books; 

public CountryList() { 
    books = new ArrayList<Country>(); 
} 

public void add(Country sb) { 
    books.add(sb); 
} 

public void readFromCSV(String filename) { 
    File file = new File(filename); 
    FileReader reader = null; 
    try { 
     reader = new FileReader(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    BufferedReader infile = new BufferedReader(reader); 
    String line = ""; 
    try { 
     boolean done = false; 
     while (!done) { 
      line = infile.readLine(); 
      if (line == null) { 
       done = true; 
      } else { 
       String[] tokens = line.trim().split(";"); 
       String country = tokens[0].trim(); 
       String capital = tokens[1].trim(); 
       int population = Integer.parseInt(tokens[2].trim()); 
       Country sb = new Country(country, capital, population); 
       books.add(sb); 
      } 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
} 

答えて

1

CSVファイルを読み込み、JTableのヘッダに最初の行を追加することができます。

public String[] getColumnNames(String csvFileDestination){ 
    BufferedReader br = null; 
    String line = ""; 
    String[] columnNames; 

    try { 

     br = new BufferedReader(new FileReader(csvFileDestination)); 
     while ((line = br.readLine()) != null) { 

      // use comma as separator 
      columnNames = line.split(","); 
      break;// Breaking out because you only need the first row 

     } 
     return columnNames; 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return null; 
} 

をしてprivate String[] columnNames;とプライベートString[] columnNames = { "Country", "Capital", "Population" };を交換し、コンストラクタでこの追加:あなたのCountryListクラスに次のメソッドを追加します columnNames = myList.getColumnNames("/path/to/csv/file.csv");

関連する問題