问题描述
这是我的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(true); } 和
if((Boolean)model.getValueAt(r,3)== true) { col.setPK(true); }
由于返回类型为布尔值,因此会出错.
也许表停止编辑会有所帮助. >
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.
Maybe Table Stop Editing will help.
这篇关于JTable不返回最后一个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!