0
JTable
으로 text/excel 파일을 읽으려고합니다. 그러나 행에 하나의 행으로 표시되지만 헤더에 의존하지는 않습니다. 나는 헤더도 얻지 못한다.텍스트 파일에서 한 줄로 공백을 나누는 방법
public class TableTest extends JFrame {
//create row and col as vector
private Vector data;
private Vector column;
private final JPanel pane;
private JTable table;
//design the frame
public TableTest() throws IOException {
//set the title
super("Table test");
//set the frame size
setSize(400, 400);
//make frame visible
setVisible(true);
//close when click the cross button
setDefaultCloseOperation(EXIT_ON_CLOSE);
//set display layout
setLayout(new BorderLayout());
//create panel to display table
pane = new JPanel();
pane.setLayout(new BorderLayout());
//create table
table = new JTable();
//add it into panel
pane.add(table);
//add panel into frame
add(pane);
readFile(new DefaultTableModel(), table);
}
//read the file and populate table
private void readFile(DefaultTableModel model, JTable table) throws FileNotFoundException, IOException {
//create file reference which will be read
FileReader reader = new FileReader("C:\\test.xls");
//try buffered reader to read data from file reader
try{
//create buffered reader for read string stream
BufferedReader br = new BufferedReader(reader);
//store file data into string
String storedata;
column = new Vector();
data = new Vector();
//add the header text of the file menually
column.addElement("Day");
column.addElement("Ramadan");
column.addElement("Date");
column.addElement("Sahree");
column.addElement("Ifter");
//get the column name automatically
for (int i = 0; i < column.size(); i++) {
model.addColumn(column.elementAt(i));
}
//read total data from the file and set it into data vector
while ((storedata = br.readLine()) != null) {
//add the row data into vector
//String value[] = br.readLine().split(",");
data.addElement(storedata);
//if found , end of the text then split it into new line use ,
//if(storedata.endsWith(",")){
// storedata.split(",");
//continue;
//}
ListIterator listIterator = data.listIterator();
while(listIterator.hasNext()){
storedata = (String) listIterator.next();
//System.out.println((storedata));
}
data.addElement(storedata);
//System.out.println(storedata);
//finally add the data into table model
model.addRow(data);
}
//populate data into table
table.setModel(model);
//refresh the table
table.repaint();
//debug
System.out.println(column);
System.out.println(data);
//filanlly close the buffered reader
br.close();
} catch(Exception ex){
JOptionPane.showMessageDialog(null, ex, "Error about file reading !", JOptionPane.ERROR_MESSAGE);
Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
//read the file and populate table
private void read(DefaultTableModel model, JTable table) throws FileNotFoundException, IOException {
//try buffered reader to read data from file reader
try{
column = new Vector();
//add the header text of the file menually
column.addElement("Day");
column.addElement("Ramadan");
column.addElement("Date");
column.addElement("Sahree");
column.addElement("Ifter");
//get the column name automatically
for (int i = 0; i < column.size(); i++) {
column.elementAt(i);
model.addColumn(column);
}
data = new Vector();
data.addElement("col 1");
data.addElement("col 2");
data.addElement("col 3");
data.addElement("col 4");
data.addElement("col 5");
data.addElement("col 6");
data.addElement("col 7");
for(int i = 0; i<data.size(); i++){
data.elementAt(i);
model.addRow(data);
}
//populate data into table
table.setModel(model);
//refresh the table
table.repaint();
//debug
System.out.println(column);
System.out.println(data);
} catch(Exception ex){
JOptionPane.showMessageDialog(null, ex, "Error about file reading !", JOptionPane.ERROR_MESSAGE);
Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) throws IOException {
TableTest test = new TableTest();
}
}
감사가 많이 나에게 도움이 대답. 나는 한 줄 밖에 없다. for 루프를 사용하여 데이터를 가져 오지만 한 행만 가져옵니다. 파일의 전체 행을 얻는 방법. –