如何将值加载到 JTable 中,以便在打开表单时显示先前的值? [英] How do you load values into a JTable so that it shows the previous values when the form is opened?

查看:47
本文介绍了如何将值加载到 JTable 中,以便在打开表单时显示先前的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数组值加载到 JTable 中,这样无论何时打开表单,它都会显示表的先前值?我不想将表单连接到任何数据库.

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

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

public class Customer extends javax.swing.JFrame {字符串性别;int year = Calendar.getInstance().get(Calendar.YEAR);int 月 = Calendar.getInstance().get(Calendar.MONTH);int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);整数 m=10;//数组内存大小字符串 [] 名称 = 新字符串 [m];字符串[]年龄=新字符串[m];字符串[] 性别m = 新字符串[m];字符串[] id = 新字符串[m];String[] mobile = new String [m];String[] email = new String [m];String[] 地址 = 新字符串 [m];String[] 日期 = 新字符串 [m];String[] photo = new String[m];公共客户(){初始化组件();tbdate.setText(day+"/"+(month+1)+"/"+year);lblphoto.setIcon(null);}私有无效 btncreateActionPerformed(java.awt.event.ActionEvent evt) {//TODO 在此处添加您的处理代码:如果(rbtnmale.isSelected()){性别 = "男";}否则如果(rbtnfemale.isSelected()){性别 = "女";}DefaultTableModel 模型 = (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

我已经把我的代码的要点都收录进去了,希望足够查看!

解决方案

对于少量数据,可以考虑

package org.name.table;导入 java.awt.Dimension;导入 java.awt.EventQueue;导入 java.util.prefs.Preferences;导入 javax.swing.JFrame;导入 javax.swing.JScrollPane;导入 javax.swing.JTable;导入 javax.swing.table.DefaultTableModel;/*** @see https://stackoverflow.com/a/34616583/230513*/公共类 TablePreference {私人无效显示(){JFrame f = new JFrame("TablePreference");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.add(new JScrollPane(new JTable(new PrefModel()){@覆盖公共维度 getPreferredScrollableViewportSize() {返回新维度(128, 32);}}));f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}私有静态类 PrefModel 扩展了 DefaultTableModel {私有静态最终 int DEFAULT = 42;私有静态最终字符串VALUE_NAME =值";私人最终首选项 p = Preferences.userNodeForPackage(TablePreference.class);公共偏好模型(){addColumn("A");addRow(new Object[]{p.getInt(VALUE_NAME, DEFAULT)});}@覆盖public void setValueAt(Object aValue, int row, int col) {super.setValueAt(aValue, row, col);p.putInt(VALUE_NAME, (int) aValue);}@覆盖公共类getColumnClass(int col) {返回 getValueAt(0, col).getClass();}}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new TablePreference()::display);}}

How do you load array values into a JTable such that whenever the form is opened, it shows the previous values of the table? I do not want to connect the form to any databases.

This is my code so far, it allows me to enter texts to the text field, and when I click "create customer" button, it stores the value into the JTable. However if I exit and reopen the form, the data previously in the table disappears. And i have done some research, but it seems like connecting netbeans to a database is the only way to save and retrieve data. However, I believe that storing data into the array is possible too, but Ii do not know how to bring out the value in the array into the table.

I need some help. It is for my school project. FYI, some of the notations I've used: 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);
}                                         

I have included the main points of my code, hope it is sufficient to view!

解决方案

For small amounts of data, consider java.util.prefs.Preferences.

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

Several examples are examined in the Preferences API Overview and the example cited here (API and code). Alternatively, consider javax.jnlp.PersistenceService, cited here, "for applications that are running in the restricted execution environment."

This minimal example updates a single cell by adding the previously saved value to the table and overriding the table model's setValueAt() implementation to save any change. Edit the table, quit and restart to see the effect.

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);
    }
}

这篇关于如何将值加载到 JTable 中,以便在打开表单时显示先前的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆