This is my Table class
package build;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Table
{
private ArrayList<Column> columns;
private String NAME;
private static int count=0;
public Table()
{
setColumns(new ArrayList<Column>());
NAME=new String();
this.NAME="Table"+count;
count++;
}
public void AddColumn(Column column)
{
for(Column c: getColumns())
{
if(c.getname().equals(column.getname())){return;}
}
getColumns().add(column);
}
public int getCount(){return count;}
public void SaveToFile() throws IOException
{
FileWriter fw = new FileWriter("C:/Users/Ashad/Desktop/text.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("\r\n");
bw.write(this.NAME);
bw.write("\r\n");
for(Column c: getColumns())
{
bw.write("\r\n");
bw.write(c.getname()+" ");
bw.write(c.getdatatype()+" ");
if(c.getPK()== true)
{
bw.write("true"+" ");
}
else
{ bw.write("false"+" ");
}
if(c.getNN()== true)
{
bw.write("true"+" ");
}
else
{bw.write("false"+" "); }
}
bw.close();
}
public boolean DeleteColumn(String name)
{
for (Column c : getColumns())
{
if (c.getname().equals(name)) {return getColumns().remove(c);}
}
return false;
}
public void viewColumns()
{
System.out.append(NAME+" ");
for(Column c: getColumns())
{
System.out.append("\n");
System.out.append(c.getname()+" ");
System.out.append( c.getdatatype()+" ");
if(c.getPK()== true)
{
System.out.print(true);
}
else
System.out.print(false);
}
}
public String getNAME() {
return NAME;
}
public ArrayList<Column> getColumns() {
return columns;
}
public void setColumns(ArrayList<Column> columns) {
this.columns = columns;
}
}
JTable
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
},
new String[] {
"Column Name", "Data Type", "NN", "PK"
}
) {
Class[] columnTypes = new Class[] {
String.class, Object.class, Boolean.class, Boolean.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
The Button below saves the values that were inserted on the Jtable to the objects of the table.
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// getTable Values
for (int r = 0; r < model.getRowCount(); r++)
{
if((String)model.getValueAt(r, 0) != null)
{
Column col=new Column();
col.setname((String) model.getValueAt(r, 0)) ;
col.setdatatype((String) model.getValueAt(r, 1));
/* if((Boolean)model.getValueAt(r, 2)== true)
{
col.setPK(true);
}
if((Boolean)model.getValueAt(r, 3)== true)
{
col.setNN(true);
}
*/
T.AddColumn(col);
}
else
break;
}
try {
T.SaveToFile();
} catch (IOException e1) {
e1.printStackTrace();
}
T.viewColumns();
frame.setVisible(false);
}
});
PROBLEM :: * Its not saving the last value for example if the last inserted value is at model.getValueAt(1, 1) or its at model.getValueAt(2, 1)
if input on Jtable is Type1 int
Type2 char
Output which above code gives :: Type1 int
Type2
Second problem is on uncommenting
if((Boolean)model.getValueAt(r, 2)== true) { col.setPK(true); } and
if((Boolean)model.getValueAt(r, 3)== true) { col.setPK(true); }
It gives error as return type is boolean.