Hello friends,
How to add data to JTable from a .txt file.
I had seen this code from one of the tutorial from java.com. This code doesnt take any input. But shows what is written in the array.
But i want the table to display the data from a .txt file in which i had stored information in this format. Can anyone please modify the codes and explain me.
Name:Mr.xyz
Phone:111111222 222
Birth Date:3/3/08
Name:Mr.abc
Phone:111111333 33
Birth Date:4/4/07
I tried it my self using FileReader and then storing it into an array . But the error comes when I feed the array in rows and columns.
import javax.swing.JFr ame;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTa ble;
import javax.swing.tab le.AbstractTabl eModel;
import java.awt.Dimens ion;
import java.awt.GridLa yout;
/**
* TableDemo is just like SimpleTableDemo , except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = false;
public TableDemo() {
super(new GridLayout(1,0) );
JTable table = new JTable(new MyTableModel()) ;
table.setPrefer redScrollableVi ewportSize(new Dimension(500, 70));
table.setFillsV iewportHeight(t rue);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(tab le);
//Add the scroll pane to this panel.
add(scrollPane) ;
}
class MyTableModel extends AbstractTableMo del {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian "};
private Object[][] data = {
{"Mary", "Campione",
"Snowboardi ng", new Integer(5), new Boolean(false)} ,
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)} ,
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)} ,
{"Isaac", "Rabinovitc h",
"Nitpicking ", new Integer(1000), new Boolean(false)}
};
public int getColumnCount( ) {
return columnNames.len gth;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(i nt col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass( int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable( int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
How to add data to JTable from a .txt file.
I had seen this code from one of the tutorial from java.com. This code doesnt take any input. But shows what is written in the array.
But i want the table to display the data from a .txt file in which i had stored information in this format. Can anyone please modify the codes and explain me.
Name:Mr.xyz
Phone:111111222 222
Birth Date:3/3/08
Name:Mr.abc
Phone:111111333 33
Birth Date:4/4/07
I tried it my self using FileReader and then storing it into an array . But the error comes when I feed the array in rows and columns.
import javax.swing.JFr ame;
import javax.swing.JPa nel;
import javax.swing.JSc rollPane;
import javax.swing.JTa ble;
import javax.swing.tab le.AbstractTabl eModel;
import java.awt.Dimens ion;
import java.awt.GridLa yout;
/**
* TableDemo is just like SimpleTableDemo , except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = false;
public TableDemo() {
super(new GridLayout(1,0) );
JTable table = new JTable(new MyTableModel()) ;
table.setPrefer redScrollableVi ewportSize(new Dimension(500, 70));
table.setFillsV iewportHeight(t rue);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(tab le);
//Add the scroll pane to this panel.
add(scrollPane) ;
}
class MyTableModel extends AbstractTableMo del {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian "};
private Object[][] data = {
{"Mary", "Campione",
"Snowboardi ng", new Integer(5), new Boolean(false)} ,
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)} ,
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)} ,
{"Isaac", "Rabinovitc h",
"Nitpicking ", new Integer(1000), new Boolean(false)}
};
public int getColumnCount( ) {
return columnNames.len gth;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(i nt col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass( int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable( int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
Comment