Re: How to bind JTable and data in a text file ?
- From: RedGrittyBrick <RedGrittyBrick@xxxxxxxxxxxxxxxxx>
- Date: Tue, 04 Nov 2008 17:21:26 +0000
tobleron wrote:
@RGB
I prefer with A option. But since my JTable component was created by
NetBeans protected code, how can I modify it ?
1. Start Notepad
2. Cut & paste the text below into it
3. Save as C:\temp\FileTable.java
4. Open a Command Prompt and enter these commands
5. cd \temp
6. javac FileTable.java
7. java FileTable
8. Ponder if NetBeans is getting in the way of an education.
9. Find an evening course at your local college.
-----------------------------------8<----------------------------------
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.table.AbstractTableModel;
/**
* @author: RedGrittyBrick
*/
public class FileTable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileTable().createAndShowGUI();
}
});
}
private static final String FILENAME = "ID.txt";
private void createAndShowGUI() {
final TextFileModel model = new TextFileModel();
new SwingWorker() {
@Override
protected Object doInBackground() throws Exception {
// Exception handling omitted for brevity - bad code!
if (!f.isFile())
writeFile(); // for 1st test only
model.readFile();
return null;
}
}.execute();
JTable table = new JTable(model);
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table), BorderLayout.CENTER);
JFrame f = new JFrame("");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
// For testing only
private void writeFile() {
try {
FileWriter fw = new FileWriter(FILENAME);
fw.write("file1.dcm;ID001;Tobleron\n");
fw.write("file2.dcm;ID002;Lucas\n");
fw.write("file3.dcm;ID003;Mark\n");
fw.close();
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
class TextFileModel extends AbstractTableModel {
List<ID> cache = new ArrayList<ID>();
public void readFile() throws IOException {
FileReader fr = new FileReader(FILENAME);
BufferedReader br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
String[] column = s.split(";");
ID id = new ID(column[0], column[1], column[2]);
cache.add(id);
}
fr.close();
fireTableRowsInserted(0, cache.size() - 1);
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public int getRowCount() {
return cache.size();
}
@Override
public Object getValueAt(int row, int column) {
ID id = cache.get(row);
switch (column) {
case 0:
return id.fileName;
case 1:
return id.fileID;
case 2:
return id.personName;
}
return null;
}
}
class ID {
public String fileName, fileID, personName;
ID(String fileName, String fileID, String personName) {
this.fileName = fileName;
this.fileID = fileID;
this.personName = personName;
}
}
}
-----------------------------------8<----------------------------------
--
RGB
.
- References:
- How to bind JTable and data in a text file ?
- From: tobleron
- Re: How to bind JTable and data in a text file ?
- From: RedGrittyBrick
- Re: How to bind JTable and data in a text file ?
- From: tobleron
- How to bind JTable and data in a text file ?
- Prev by Date: Re: 7.0 wishlist?
- Next by Date: Re: How to bind JTable and data in a text file ?
- Previous by thread: Re: How to bind JTable and data in a text file ?
- Next by thread: Re: How to bind JTable and data in a text file ?
- Index(es):
Relevant Pages
|