[英]Jtable not returning the last cell value
This is my Table class 这是我的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 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. 下面的按钮将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);
}
});
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) PROBLEM :: *如果最后插入的值位于model.getValueAt(1,1)或其位于model.getValueAt(2,1),则不保存最后一个值
if input on Jtable is Type1 int 如果Jtable上的输入是Type1 int
Type2 char Type2 char
Output which above code gives :: Type1 int 上面代码的输出给出:: Type1 int
Type2 类型2
Second problem is on uncommenting 第二个问题是取消注释
if((Boolean)model.getValueAt(r, 2)== true) { col.setPK(true); if((Boolean)model.getValueAt(r,2)== true){col.setPK(true); } and
和
if((Boolean)model.getValueAt(r, 3)== true) { col.setPK(true); if((Boolean)model.getValueAt(r,3)== true){col.setPK(true); }
}
It gives error as return type is boolean. 它给出错误,因为返回类型是布尔值。
Its not saving the last value
它没有保存最后一个值
Maybe Table Stop Editing will help. 也许表停止编辑会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.