Zitat Zitat von Thomas Darimont
Hallo!

Versuchs mal hiermit:

Spaltenanzahl muss bei allen Datensatzen gleich sein ...

Code :
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
 
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
 
/*
 * FileImport.java
 *
 * Created on 14. April 2004, 14:16
 */
 
/**
 *
 * @author  Darimont
 */
public class FileImport extends JFrame {
 
    private BufferedReader br;
    private File importFile;
    private JFileChooser chooser;
    private FileImport _this;
    private volatile boolean importRunning = false;
    private DefaultTableModel model;
 
    /** Creates new form FileImport */
    public FileImport() {
        initComponents();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _this = this;
    }
 
    /** 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.
     */
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
 
        bgPanel = new JPanel();
        tablePanel = new JPanel();
        scrollPane = new JScrollPane();
        jTable1 = new JTable();
        controlsPanel = new JPanel();
        btnChoseFile = new JButton();
        txtPath = new JTextField();
        btnImport = new JButton();
        lblPath = new JLabel();
        lblDelimiter = new JLabel();
        txtDelimiter = new JTextField();
 
        getContentPane().setLayout(new java.awt.FlowLayout());
 
        setTitle("FileImport");
 
        bgPanel.setLayout(new java.awt.GridBagLayout());
 
        
        bgPanel.setMinimumSize(new java.awt.Dimension(400, 300));
        bgPanel.setPreferredSize(new java.awt.Dimension(400, 300));
        
        tablePanel.setMinimumSize(new java.awt.Dimension(400, 200));
        tablePanel.setPreferredSize(new java.awt.Dimension(400, 200));
        scrollPane.setMinimumSize(new java.awt.Dimension(400, 200));
        scrollPane.setPreferredSize(new java.awt.Dimension(400, 200));
 
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        scrollPane.setViewportView(jTable1);
 
        tablePanel.add(scrollPane);
 
        bgPanel.add(tablePanel, new java.awt.GridBagConstraints());
 
        controlsPanel.setLayout(new java.awt.GridBagLayout());
 
        controlsPanel.setMinimumSize(new java.awt.Dimension(400, 100));
        controlsPanel.setPreferredSize(new java.awt.Dimension(400, 100));
        btnChoseFile.setText("Chose File");
        btnChoseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChoseFileActionPerformed(evt);
            }
        });
 
        controlsPanel.add(btnChoseFile, new java.awt.GridBagConstraints());
 
        txtPath.setColumns(20);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        controlsPanel.add(txtPath, gridBagConstraints);
 
        btnImport.setText("Import");
        btnImport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImportActionPerformed(evt);
            }
        });
 
        controlsPanel.add(btnImport, new java.awt.GridBagConstraints());
 
        lblPath.setText("Path: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        controlsPanel.add(lblPath, gridBagConstraints);
 
        lblDelimiter.setText("Delimiter: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        controlsPanel.add(lblDelimiter, gridBagConstraints);
 
        txtDelimiter.setColumns(3);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        controlsPanel.add(txtDelimiter, gridBagConstraints);
 
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        bgPanel.add(controlsPanel, gridBagConstraints);
 
        getContentPane().add(bgPanel);
 
        pack();
    }
 
    private void btnImportActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        if (importFile != null)
            if (!importRunning) {
 
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        doImport(importFile);
                    }
                });
            } else {
                System.out.println("Still importing ... ");
            }
    }
 
    /**
     * @param importFile
     */
    protected void doImport(File importFile) {
        // TODO Auto-generated method stub
        String delimiter = txtDelimiter.getText();
        if (delimiter == null || !delimiter.equals(""))
            delimiter = ";";
        try {
            br = new BufferedReader(new FileReader(importFile));
 
            ArrayList rows = new ArrayList();
            Object[] clmHeaders = null;
 
            String line = null;
            StringTokenizer tokenizer = null;
 
            int tCnt = -1;
 
            while ((line = br.readLine()) != null) {
                tokenizer = new StringTokenizer(line, delimiter);
                tCnt = tokenizer.countTokens();
                Object[] rowData = new Object[tCnt];
                int i = 0;
                while (tokenizer.hasMoreTokens())
                    rowData[i++] = tokenizer.nextToken();
                rows.add(rowData);
            }
 
            clmHeaders = new Object[((Object[]) rows.get(0)).length];
 
            for (int i = 0; i < clmHeaders.length; i++) {
                clmHeaders[i] = "Column" + i;
            }
 
            Object[][] oArray = new Object[tCnt][];
            int lstSize = rows.size();
            for (int i = 0; i < lstSize; i++)
                oArray[i] = (Object[]) rows.get(i);
 
            model = new DefaultTableModel(oArray, clmHeaders);
 
            jTable1.setModel(model);
            jTable1.updateUI();
 
            System.out.println("Import complete!");
 
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    private void btnChoseFileActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        if (chooser == null)
            chooser =
                new JFileChooser(new File(System.getProperty("user.dir")));
 
        chooser.showDialog(this, "load");
        importFile = chooser.getSelectedFile();
        if (importFile != null)
            txtPath.setText(importFile.getAbsolutePath());
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new FileImport().show();
    }
 
    // Variables declaration - do not modify
    private JButton btnChoseFile;
    private JButton btnImport;
    private JPanel bgPanel;
    private JPanel tablePanel;
    private JPanel controlsPanel;
    private JScrollPane scrollPane;
    private JTable jTable1;
    private JLabel lblDelimiter;
    private JLabel lblPath;
    private JTextField txtDelimiter;
    private JTextField txtPath;
    // End of variables declaration
 
}

Gruß Tom
Hallo Java-Profi,
geht es dass man anstatt eine File mehrere File(gespeichert in einem Verzeichnis und haben die selbe Endung) mit der selben Methode auszulesen und in Jtable anzeigen.
Habe mit deinem Code versucht eine Datei auszulesen das klappt soweit allerdings in JTable sehe ich immer nur eine Spalte mit der Beschriftung Column 0. und den ganzen Inhalt in diser Spalte also nicht getrennt wie ich es möchte.
Danke im voraus.
Gruss