这是我的Table类
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];
}
下面的按钮将Jtable上插入的值保存到表的对象中。
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);
}
});
问题:: *如果最后插入的值位于model.getValueAt(1,1)或其位于model.getValueAt(2,1)
,则不保存最后一个值如果Jtable上的输入是Type1 int
Type2 char
上面代码给出的输出:: Type1 int
Type2
第二个问题是取消注释
if((Boolean)model.getValueAt(r,2)== true) { col.setPK(真); } 和
if((Boolean)model.getValueAt(r,3)== true) { col.setPK(真); }
它给出错误,因为返回类型是布尔值。