/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myserver;
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.util.Arrays;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
*
* @author PC-schlappi
*/
public class server extends javax.swing.JFrame {
private JTable table;
private Component btnAction;
/**
* Creates new form server
*/
public server() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Impocrt CSV");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(297, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(266, Short.MAX_VALUE))
);
jButton1.getAccessibleContext().setAccessibleName("JButton_ImportCSV");
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("DateiAuswahl");
fc.setFileFilter(new FileNameFilter());
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
final File file = fc.getSelectedFile();
System.out.println(file.getName());
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
TableModel model = parseCSVAndCreateTableModel(file);
table.setModel(model);
}
});
}
table = new JTable();
add(new JScrollPane(table), BorderLayout.CENTER);
add(btnAction, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private TableModel parseCSVAndCreateTableModel(File file) {
Vector v = new Vector();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
// Überschriften stehen in erster Zeile
String[] rowItems = br.readLine().split(",");
Vector header = new Vector(Arrays.asList(rowItems));
while (br.ready()) {
rowItems = br.readLine().split(",");
v.add(new Vector(Arrays.asList(rowItems)));
}
DefaultTableModel model = new DefaultTableModel(v, header);
return model;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public class FileNameFilter extends FileFilter
{
@Override
public boolean accept(File arg0)
{
if(arg0.isDirectory()) return true;
if(arg0.getName().endsWith("csv")) return true;
return false;
}
@Override
public String getDescription()
{
return "Nur CSV";
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws IOException {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new server().setVisible(true);
}
});
/*
* Socket aufbauen
*/
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening)
new serverThread(serverSocket.accept()).start();
serverSocket.close();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}