Java 如何在退出GUI后仍在GUI中保留表的值
我在保留/保留保存保存在GUI表中的值时遇到问题。我的目标是将值保存在表中存储的位置,退出GUI,然后再次打开它,然后我希望看到表中存储的相同值。我这里的问题是,每当我关闭并再次打开GUI时,它都会刷新值并使其再次为空 我通过写入和读取CSV文件来保存我的值 在编写CSV文件时,它取决于用户在我的GUI的文本字段上输入的值,然后它在我的驱动器C的特定位置上创建CSV文件 在读取CSV文件时,它会读取写入的CSV文件的内容/值,然后使用这些值存储在2D数组中,以便将其显示在我的表格中。但问题是,我无法在表中保留这些值,并且该表在显示的数据旁边显示空值(我尝试了!=null,但它仍然在输入值之后显示连续的空值) 图形用户界面: CSV文件: 以下是我的源代码:Java 如何在退出GUI后仍在GUI中保留表的值,java,csv,user-interface,multidimensional-array,Java,Csv,User Interface,Multidimensional Array,我在保留/保留保存保存在GUI表中的值时遇到问题。我的目标是将值保存在表中存储的位置,退出GUI,然后再次打开它,然后我希望看到表中存储的相同值。我这里的问题是,每当我关闭并再次打开GUI时,它都会刷新值并使其再次为空 我通过写入和读取CSV文件来保存我的值 在编写CSV文件时,它取决于用户在我的GUI的文本字段上输入的值,然后它在我的驱动器C的特定位置上创建CSV文件 在读取CSV文件时,它会读取写入的CSV文件的内容/值,然后使用这些值存储在2D数组中,以便将其显示在我的表格中。但问题是,我
public void writeCustomerCSV(){ // everything in this snippet code works fine(it creates a CSV file which stores the inputs of the user)
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
StringBuilder sb = new StringBuilder();
int y;
for(int x = 0; x < itemTo2D.length; x++){
for(y = 0; y < itemTo2D[0].length; y++){
if(itemTo2D[x] != null){
sb.append(itemTo2D[x][y]);
sb.append(",");
}
}
sb.append("-"); //separation for rows
sb.append(","); // separation for columns
}
bw.write(sb.toString());
bw.close();
} catch (Exception ex){
}
}
public void readCustomerCSV(){ // reads the contents of the CSV file *having issues in retaining the values
int read2DStringIndex = 0;
int newVarIndexer = 0;
String[] fromfile = {}; // 1d string for getting the columns(7 columns) of the CSV file
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
String line;
while ((line = br.readLine()) != null){
fromfile = line.split(","); //separates the columns by a comma
}
} catch (Exception ex){
}
for(int g = 0; g < fromfile.length; g++){
if(fromfile[g].equals("-")){ //if there is a presence of a dash, it increments the read2DStringINdex (row index) of the 2D array
read2DStringIndex++;
newVarIndexer = 0;
}
else{
cust[read2DStringIndex][newVarIndexer] = fromfile[g]; //cust is the 2D array(declared universal) which is going to display the values to the table
newVarIndexer++;
}
}
for(int h = 0; h < cust.length; h++){ //prints cust (2D array) , just to check what data is being stored
for(int p = 0; p < cust[0].length; p++){
System.out.println(cust[h][p] + ",");
}
}
DefaultTableModel tblmodelll = (DefaultTableModel) mainTable.getModel(); // table
setrowcount = 0;
for(int r = 0; r < cust.length; r++){
if(setrowcount == 0){
tblmodelll.setRowCount(0);
}
if(cust[r][0] != null){
tblmodelll.addRow(cust[r]); //displays the cust(2D array) data to table
}
setrowcount++;
}
public void writeCustomerCSV(){//此代码段中的所有内容都可以正常工作(它创建了一个CSV文件,用于存储用户的输入)
试一试{
BufferedWriter bw=新的BufferedWriter(新文件编写器(“C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.CSV”);
StringBuilder sb=新的StringBuilder();
int-y;
对于(int x=0;x
}
我是否缺少代码中需要添加的内容,或者代码的逻辑是否不正确?
你的回答确实有助于解决这个问题。
非常感谢 您可以这样读写您的表(如果它是一个字符串表。我在这里使用的是Gson库的JsonWriter和JsonReader。但是其他JsonReader和JsonWriter也应该可以使用。我想):
publicstaticvoidwrite(Writer w,String[][]表)引发IOException{
JsonWriter=null;
试一试{
writer=新的JsonWriter(w);
writer.setIndent(“”);
writer.setSerializeNulls(true);
int高度=表格长度;
整数宽度=高度>0?表[0]。长度:0;
writer.beginObject();
作者姓名(“len”);
写入值((长)宽*(长)高);
作者姓名(“子”);
写入值(宽度);
作者姓名(“条目”);
writer.beginArray();
int y,x;
对于(y=0;y<高度;y++){
对于(x=0;xpublic static void write(Writer w, String[][] table) throws IOException {
JsonWriter writer = null;
try {
writer = new JsonWriter(w);
writer.setIndent(" ");
writer.setSerializeNulls(true);
int height = table.length;
int width = height > 0 ? table[0].length : 0;
writer.beginObject();
writer.name("len");
writer.value((long) width * (long) height);
writer.name("sub");
writer.value(width);
writer.name("entries");
writer.beginArray();
int y, x;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
String entry = table[y][x];
if (entry == null) writer.nullValue();
else writer.value(entry);
}
}
writer.endArray();
writer.endObject();
writer.flush();
} finally {
if (writer != null) writer.close();
}
}
public static String[][] read(Reader r) throws IOException {
JsonReader reader = null;
long len = 0, sub = 0;
String[][] table = null;
try {
reader = new JsonReader(r);
reader.beginObject();
read:
do {
while (reader.peek() != JsonToken.NAME) {
if (!reader.hasNext()) break read;
}
String name = reader.nextName();
if ("len".equals(name)) len = reader.nextLong();
else if ("sub".equals(name)) sub = reader.nextLong();
else if ("entries".equals(name)) {
reader.beginArray();
table = new String[(int) (len / sub)][(int) sub];
long i;
int y, x;
String entry;
for (i = 0L; i < len && reader.hasNext(); i++) {
y = (int) (i / sub); x = (int) (i % sub);
if (reader.peek() != JsonToken.STRING) {
entry = null;
reader.skipValue();
} else entry = reader.nextString();
table[y][x] = entry;
}
reader.endArray();
}
} while (true);
reader.endObject();
} finally {
if (reader != null) reader.close();
}
return table;
}
for(int x = 0; x < itemTo2D.length; x++){
for(y = 0; y < itemTo2D[0].length; y++){
if(itemTo2D[x] != null){
sb.append(itemTo2D[x][y]);
sb.append(",");
}
}
for(int x = 0; x < itemTo2D.length; x++){
for(y = 0; y < itemTo2D[0].length; y++){
if(itemTo2D[x] != null){
sb.append(itemTo2D[x][y]);
}
sb.append(",");
}
while ((line = br.readLine()) != null){
fromfile = line.split(","); //separates the columns by a com
}