Re: How to bind JTable and data in a text file ?




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
.



Relevant Pages

  • Re: Instrumentation + BCEL | ASM
    ... final String owner, ... Strangely enough, with this method added, I can only transform methods with exactly one argument of type String). ... But I don't seem to know how to get hold of method signature, so there can be problems if consecutive threads call timed methods with the same name, but different signatures. ... private void genStartCode() { ...
    (comp.lang.java.programmer)
  • Yet another way to use Java
    ... * @param mainClass e.g. FontShower, ... StompProject (String mainClass, ... * does this project have a jar ... void mkDescBtm() ...
    (comp.lang.java.programmer)
  • Re: Creating multiple instances of objects
    ... > Can you post the server maincode, your IDL, and the full stack ... // Naming Service specification. ... typedef string tTbls; ... void SetCurrentTableraises; ...
    (comp.object.corba)
  • Re: Command Parser
    ... shortcut to writing the full declaration of 'void function1(Connection ch, ... UserList users, string ARGS)'. ... Then you can instantiate the class with the invariant arguments passed to the constructor or otherwise initialized as part of the instantiation, and then just pass the non-invariant data (the command argument) to each method when it's called. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UDP broadcasting problem
    ... private void Client_OnConnect(string msg) ... get {return mygroupPort;} ...
    (microsoft.public.dotnet.languages.csharp)