JTable
で.txtをロードする必要があります。ここdelimiter
で良いサンプルを見つけました。
これはサンプルデータです:
102|Beth Reiser||New York|(212)5558725
111|Dylan Ricci||Syracuse|(315)5554486
116|Brian Gugliuzza||Mamaroneck|(914)5553817
120|Gertrude Stein||Elmsford|(914)5553476
131|Daljit Sinnot||Bohemia|(516)5559811
これは私の変更されたコードです:
package Model;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
public class DataFileTableModel extends AbstractTableModel {
protected Vector data;
protected Vector columnNames ;
protected String datafile;
public DataFileTableModel(String f, String delimiter){
datafile = f;
initVectors(delimiter);
}
public void initVectors(String delimiter) {
String aLine ;
data = new Vector();
columnNames = new Vector();
int lineNum=0;
try {
FileInputStream fin = new FileInputStream(datafile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names
StringTokenizer st1 =
new StringTokenizer(br.readLine(), delimiter);
while(st1.hasMoreTokens())
columnNames.addElement(st1.nextToken());
// extract data
while ((aLine = br.readLine()) != null && lineNum<20) {
StringTokenizer st2 =
new StringTokenizer(aLine, delimiter);
lineNum++;
while(st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int getRowCount() {
return data.size() / getColumnCount();
}
public int getColumnCount(){
return columnNames.size();
}
public String getColumnName(int columnIndex) {
String colName = "";
if (columnIndex <= getColumnCount())
colName = (String)columnNames.elementAt(columnIndex);
return colName;
}
public Class getColumnClass(int columnIndex){
return String.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
return;
}
}
データによると、5列が表示されますが、4列のみが表示され、列3は空であるためスキップされました。すべての列を表示したいのですがJTable
、どうすればこれを実現できますか?@ hovercraft-full-of-eelsによると、空の列がテーブルの中央にある場合は正常に表示されますが、裏側の空の列を処理することはできません。これを取得する方法は?
裏側の空の列のデータサンプル:
102|Beth Reiser||New York|(212)5558725||||
111|Dylan Ricci||Syracuse|(315)5554486||||
116|Brian Gugliuzza||Mamaroneck|(914)5553817||||