|
日時: 2017/05/25 08:00
名前: lightbox
|
拡張子:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main extends JFrame {
private JPanel contentPane;
private JTable table;
private JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1000, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnNewButton = new JButton("読み込み");
btnNewButton.setBounds(10, 10, 91, 21);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadCsv();
}
});
contentPane.setLayout(null);
contentPane.add(btnNewButton);
panel = new JPanel();
panel.setBounds(12, 52, 960, 600);
panel.setLayout(new BorderLayout(0, 0));
contentPane.add(panel);
table = new JTable() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
scrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
private void loadCsv(){
try {
FileInputStream fis = new FileInputStream("c:\\user\\社員.csv");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);
String line_buffer;
String[] adata = null;
int line_count = 0;
while ( null != (line_buffer = br.readLine() ) ) {
if ( adata == null ) {
adata = line_buffer.split(",");
int count = adata.length;
// 先に全ての列を登録する必要があります
for( int i = 0; i < count; i++) {
addColumn(table, String.format("col%d", i));
}
// タイトル文字列の変更を行っています。
// 変更しない場合は、列を登録時の第二引数になります
for( int i = 0; i < count; i++) {
setColumnTitle(table, String.format("col%d", i),adata[i]);
}
}
else {
addRow(table);
adata = line_buffer.split(",");
int count = adata.length;
for( int i = 0; i < count; i++) {
setColumn(table,line_count,i,adata[i]);
}
line_count++;
}
}
br.close();
isr.close();
fis.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
// ****************************
// 列の追加
// ****************************
private void addColumn(JTable table, String name) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
dtm.addColumn(name);
}
// ****************************
// 列のタイトル文字列変更
// ****************************
private void setColumnTitle(JTable table, String name,String title) {
TableColumn tc1 = table.getColumn(name);
tc1.setHeaderValue(title);
tc1.setIdentifier(name);
}
// ****************************
// 空の行追加
// ****************************
private void addRow(JTable table) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
Object[] obj = null;
dtm.addRow(obj);
}
// ****************************
// 指定カラムへデータをセット
// ****************************
private void setColumn(JTable table,int row,int col, String data) {
table.setValueAt( data, row, col );
}
}
|