1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import javax.swing.JFileChooser;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.regex.Pattern;
public class ImportTxt extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new ImportTxt();
}
public ImportTxt(){
this.setTitle("Importation");
JTable table = new JTable();
String [][] data = { };
String [] headers = {"Option","Chaine",
"Interface i ","Interface o", "@IP source", "@IP destination",
"Protocole" ,"Type","Port source", "Port destination",
"Suivi de la connexion","Action"};
DefaultTableModel model = new DefaultTableModel(data,headers);
table = new JTable(model);
table.setModel(model);
String contents = null;
try
{
JFileChooser fc = new JFileChooser(".");
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try
{
File file = fc.getSelectedFile();
FileReader files = new FileReader(file);
BufferedReader buf = new BufferedReader(files);
while ((contents = buf.readLine()) != null)
{
//String[] tokens = contents.split(" ");
String tokens[] = null;
// String splitPattern = "";
Pattern p = Pattern.compile(" ");
tokens = p.split(" ");
model.insertRow(table.getRowCount(),tokens);
}
} catch(Throwable err){
System.out.println("Error occured when opening file: " + err.getMessage());
}
} else {
System.out.println("Open command cancelled by user.");
}
}catch (Exception e){
e.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
setSize(900,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Partager