如何打开窗体时加载值到一个JTable,使其显示previous值? [英] How do you load values into a JTable so that it shows the previous values when the form is opened?

查看:125
本文介绍了如何打开窗体时加载值到一个JTable,使其显示previous值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何加载阵列值成的JTable 这样,每当打开表单,它显示了表的previous值?我不想的形式连接到任何数据库。

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.

这是我的code,到目前为止,它可以让我输入文本的文本字段,当我点击创造客户按钮,它存储的值到的JTable 。但是,如果我退出并重新打开窗体,数据previously表中消失。我已经做了一些研究,但似乎netbeans的连接到数据库是保存和检索数据的唯一途径。不过,笔者认为,将数据存储到数组也是可能的,但是...我不知道如何带出的值的数组中插入表中。

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.

我需要一些帮助。这是我的学校项目。
仅供参考,有些我使用符号的: = RBTN单选按钮,TB =文本框,LBL =标签

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

我已经包括了我的code的要点,希望它足以查看!

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

推荐答案

有关少量的数据,考虑的 的java.util。preFS。preferences

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?

<被检查几个例子EM> preferences API概述的和举的例子这里(的 API 和的 code )。另外,考虑 javax.jnlp.PersistenceService ,引 href=\"http://stackoverflow.com/a/6886694/230513\">对于在受限执行环境中运行的应用程序。

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."

这小例子,通过添加previously保存的值表并重写表格模型的 setValueAt()执行保存任何变化更新单个细胞。编辑表格,退出并重新启动才能看到效果。

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 http://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,使其显示previous值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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