gpt4 book ai didi

java - Jtable 不返回最后一个单元格值

转载 作者:行者123 更新时间:2023-12-01 23:51:23 24 4
gpt4 key购买 nike

这是我的表类

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 字符

上面代码给出的输出::Type1 int
类型2

  • 第二个问题是取消注释

    if((Boolean)model.getValueAt(r, 2)== true) { col.setPK(true); } 和

    if((Boolean)model.getValueAt(r, 3)== true) { col.setPK(true); }

由于返回类型为 boolean 值,因此出现错误。

最佳答案

Its not saving the last value

也许Table Stop Editing会有所帮助。

关于java - Jtable 不返回最后一个单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16267980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com