gpt4 book ai didi

java - 如何将值加载到 JTable 中,以便它在打开表单时显示以前的值?

转载 作者:行者123 更新时间:2023-11-30 08:06:09 25 4
gpt4 key购买 nike

如何将数组值加载到 JTable 中,以便在打开表单时显示表的先前值?我不想将表单连接到任何数据库。

到目前为止,这是我的代码,它允许我在文本字段中输入文本,当我单击“创建客户”按钮时,它会将值存储到 JTable 中。但是,如果我退出并重新打开表单,表中以前的数据就会消失。我做了一些研究,但似乎将 netbeans 连接到数据库是保存和检索数据的唯一方法。但是,我相信将数据存储到数组中也是可能的,但我不知道如何将数组中的值取出到表中。

我需要一些帮助。这是为了我的学校项目。仅供引用,我使用的一些符号:rbtn = radiobutton, tb = textfields, lbl = label

public class Customer extends javax.swing.JFrame {
String gender;
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH);
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

int m=10; //array memory size

String[] name = new String[m];
String[] age = new String[m];
String[] genderm = new String [m];
String[] id = new String [m];
String[] mobile = new String [m];
String[] email = new String [m];
String[] address = new String [m];
String[] date = new String [m];
String[] photo = new String[m];

public Customer() {
initComponents();
tbdate.setText(day+"/"+(month+1)+"/"+year);
lblphoto.setIcon(null);
}

private void btncreateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (rbtnmale.isSelected()){
gender = "Male";
}
else if (rbtnfemale.isSelected()){
gender = "Female";
}
DefaultTableModel model = (DefaultTableModel) jtablecustinfo.getModel();
model.addRow(new Object[]{tbname.getText(),tbage.getText(),gender,tbid.getText(),tbmobile.getText(),tbemail.getText(),tbaddress.getText(),tbdate.getText(),lblphoto.getIcon()});

for(int i=0;i<m;i++){
name[i]=tbname.getText();
age[i] = tbage.getText();
genderm[i]=gender;
id[i]=tbid.getText();
mobile[i]=tbmobile.getText();
email[i]=tbemail.getText();
address[i]=tbaddress.getText();
date[i]=tbdate.getText();
photo[i]= tbimage.getText();;
}

//Reset everything after creation
JOptionPane.showMessageDialog(null,"Successfully Created Customer");
tbname.setText("");
tbage.setText("");
tbid.setText("");
tbmobile.setText("");
tbemail.setText("");
tbaddress.setText("");
tbdate.setText("");
rbtnmale.setSelected(false);
rbtnfemale.setSelected(false);
tbdate.setText(day+"/"+(month+1)+"/"+year);
gender = "";
tbimage.setText("");
lblphoto.setText(" -Import photo-");
lblphoto.setIcon(null);
}

我已经包含了我的代码的要点,希望足以查看!

最佳答案

对于少量数据,请考虑 java.util.prefs.Preferences .

Would you be able to provide me with some examples on how to use it?

Preferences API Overview 中检查了几个示例和引用的例子 here ( APIcode )。或者,考虑 javax.jnlp.PersistenceService,引用 here , “对于在受限执行环境中运行的应用程序。”

这个最小的示例通过将先前保存的值添加到表格并覆盖表格模型的 setValueAt() 实现来更新单个单元格以保存任何更改。编辑表格,退出重启看看效果。

image

package org.name.table;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
* @see https://stackoverflow.com/a/34616583/230513
*/
public class TablePreference {

private void display() {
JFrame f = new JFrame("TablePreference");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(new JTable(new PrefModel()) {

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(128, 32);
}
}));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static class PrefModel extends DefaultTableModel {

private static final int DEFAULT = 42;
private static final String VALUE_NAME = "value";
private final Preferences p = Preferences.userNodeForPackage(TablePreference.class);

public PrefModel() {
addColumn("A");
addRow(new Object[]{p.getInt(VALUE_NAME, DEFAULT)});
}

@Override
public void setValueAt(Object aValue, int row, int col) {
super.setValueAt(aValue, row, col);
p.putInt(VALUE_NAME, (int) aValue);
}

@Override
public Class<?> getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new TablePreference()::display);
}
}

关于java - 如何将值加载到 JTable 中,以便它在打开表单时显示以前的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574745/

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