import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JTableTextDemo extends JFrame {
private JTable table;
private JScrollPane scrollPane;
private String filePath = "c:/Data.csv";
private JButton btnLoad, btnSave;
private Object[] tableHeader;
private static final char SEPARATOR_CHAR = ';';
private boolean occupied = false;
public JTableTextDemo() {
super("JTableTextDemo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
pack();
}
/**
*
*/
private void init() {
ActionListener listener = new ActionHandler();
btnLoad = new JButton("Load");
btnLoad.addActionListener(listener);
getContentPane().add(btnLoad, BorderLayout.CENTER);
btnSave = new JButton("Save");
btnSave.addActionListener(listener);
getContentPane().add(btnSave, BorderLayout.SOUTH);
table = new JTable(0, 0);
table.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
int code = evt.getKeyCode();
if (code == KeyEvent.VK_ENTER || code == KeyEvent.VK_TAB) {
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
(
(DefaultTableModel) table
.getModel())
.fireTableCellUpdated(
row,
column);
}
}
});
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.NORTH);
}
public static void main(String[] args) {
new JTableTextDemo();
}
private Object[][] readData() {
File file = new File(filePath);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
StringTokenizer tokenizer = null;
int clmCnt = 0;
List list = new ArrayList();
while ((line = br.readLine()) != null) {
tokenizer = new StringTokenizer(line, ";");
clmCnt = tokenizer.countTokens();
Object[] rowData = new Object[clmCnt];
int i = 0;
while (tokenizer.hasMoreTokens()) {
rowData[i++] = tokenizer.nextElement();
}
list.add(rowData);
}
tableHeader = new Object[clmCnt];
for (int i = 0; i < tableHeader.length; i++) {
tableHeader[i] = "Column" + i;
}
int size = list.size();
Object[][] rows = new Object[size][];
for (int i = 0; i < size; i++) {
rows[i] = (Object[]) list.get(i);
}
list.clear();
return rows;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return new Object[0][0];
}
public void store() {
BufferedWriter bw = null;
File file = new File(filePath);
try {
bw = new BufferedWriter(new FileWriter(file));
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
Object[] oa = dtm.getDataVector().toArray();
for (int i = 0; i < oa.length; i++) {
Object[] o = ((Vector) oa[i]).toArray();
for (int j = 0; j < o.length; j++) {
bw.write(o[j].toString());
bw.write(SEPARATOR_CHAR);
}
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.flush();
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == btnLoad) {
if (occupied)
return;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
occupied = true;
DefaultTableModel dtm =
new DefaultTableModel(readData(), tableHeader);
table.setModel(dtm);
occupied = false;
}
});
} else if (src == btnSave) {
if (occupied)
return;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
occupied = true;
store();
occupied = false;
}
});
}
}
}
}