Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
/*
* Created on 21.03.2005
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.io.*;
public class CSVTest extends JFrame
{
private class MeinWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent arg0)
{
System.exit(0);
}
}
private String[] columnNamen = { "Spalte 1", "Spalte 2" };
private DefaultTableModel tableModel;
private JButton button;
private JComponent contentPane = (JComponent) getContentPane();
CSVTest()
{
super("CSV Test");
setSize(800, 600);
initLayout();
setLocationRelativeTo(null);
addWindowListener(new MeinWindowListener());
setVisible(true);
setResizable(true);
}
void initLayout()
{
contentPane.setLayout(new BorderLayout());
tableModel = new DefaultTableModel(null, columnNamen);
JTable tableZahlen = new JTable(tableModel);
tableZahlen.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(tableZahlen);
scrollPane.setPreferredSize(new Dimension(300, 300));
JPanel jpTable = new JPanel();
jpTable.add("Center", scrollPane);
contentPane.add("West", jpTable);
button = new JButton("Einlesen");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
readFile();
}
});
JPanel jpButton = new JPanel();
jpButton.add(button);
contentPane.add("South", jpButton);
}
/**
*
*/
protected void readFile()
{
try{
BufferedReader reader = new BufferedReader(new FileReader("C:\\Mappe1.csv"));
String strLine;
while ( (strLine = reader.readLine()) != null )
{
String[] items = strLine.split(";");
tableModel.addRow(items);
}
tableModel.fireTableDataChanged();
}
catch(FileNotFoundException e) {}
catch(IOException e) {}
}
public static void main(String[] args)
{
CSVTest frame = new CSVTest();
frame.setLocation(150, 90);
frame.setSize(600, 500);
frame.setResizable(true);
frame.setVisible(true);
}
}
/**
*
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
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 Administrator
*
*/
public class CSVJTableExample extends JFrame {
private JTable table;
private JButton btnAction;
private JFileChooser fileChooser = new JFileChooser() {
{
setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith("csv");
}
@Override
public String getDescription() {
return "CSV Datei (Comma Seperated Values)";
}
});
}
};
public CSVJTableExample() {
super("CSVJTableExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btnAction = new JButton("Dateiauswahl");
btnAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!(fileChooser.showOpenDialog(CSVJTableExample.this) == JFileChooser.APPROVE_OPTION))
return;
final File file = fileChooser.getSelectedFile();
if (file == null)
return;
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;
}
/**
* @param args
*/
public static void main(String[] args) {
new CSVJTableExample();
}
}