简体   繁体   English

向JTable添加新行

[英]Adding a new row to JTable

I'm trying trying to add a new row to my JTable based upon what the user inputs and have that repeat for as many times as the user registers. 我正在尝试根据用户输入的内容向JTable添加新行,并使其重复次数与用户注册的次数相同。 For example let's say the user entered "Susie" in registerTF, "11:00" for lengthTF, and "60" for IDTF. 例如,假设用户在寄存器TF中输入了“ Susie”,为lengthTF输入了“ 11:00”,为IDTF输入了“ 60”。 I can add that in as a row to my JTable in examInfo.java with no problem, but how do I do it repeatedly so the next time Registration.java is run everything that is entered in those fields is put into a brand new row into my JTable. 我可以毫无问题地将其作为行添加到我的JTable中的JTable中,但是如何重复执行此操作,以便下次运行Registration.java时,将在这些字段中输入的所有内容放入一个崭新的行中。我的JTable。 Before anyone asks, no this is not homework I'm doing it for a family member. 在任何人问之前,不,这不是我为家庭成员做的家庭作业。

Here is my Registration.java code: 这是我的Registration.java代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Registration extends JFrame{

    private static final int WIDTH = 400;
    private static final int HEIGHT = 300;
    private JLabel registerL, lengthL, IDL;
    private JTextField registerTF, lengthTF, IDTF;
    private JButton registerB, exitB;

    //Button handlers:
    private CalculateButtonHandler cbHandler;

    private ExitButtonHandler ebHandler;



    public Registration(){

        registerL = new JLabel("Enter Name: ", SwingConstants.RIGHT);
        lengthL = new JLabel("Enter Registration: ", SwingConstants.RIGHT);
        IDL = new JLabel("Length of exam: ", SwingConstants.RIGHT);

        registerTF = new JTextField(10);
        lengthTF = new JTextField(10);
        IDTF = new JTextField(10);


        //Specify handlers for each button and add (register) ActionListeners to each button.
        registerB = new JButton("Register");
        cbHandler = new CalculateButtonHandler();
        registerB.addActionListener(cbHandler);
        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        exitB.addActionListener(ebHandler);



        setTitle("Registration");
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(4, 2));


        //Add things to the pane in the order you want them to appear (left to right, top to bottom
        pane.add(registerL);
        pane.add(registerTF);
        pane.add(lengthL);
        pane.add(lengthTF);
        pane.add(IDL);
        pane.add(IDTF);
        pane.add(registerB);
        pane.add(exitB);

        setSize(WIDTH, HEIGHT);
        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    private class CalculateButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){

    }
}

public class ExitButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        System.exit(0);
    }
}

public static void main(String[] args){
    Registration rectObj = new Registration();
}  
}

This is my code for examInfo.java where my JTable is: 这是我的examInfo.java代码,其中我的JTable是:

import java.util. Arrays;
import java.util.Vector;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class examInfo {
    public static void main(String agrs[]){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Vector<String> rowOne = new Vector<String>();
        rowOne.addElement("Row1-Column1");
        rowOne.addElement("Row1-Column2");
        rowOne.addElement("Row1-Column3");

        Vector<String> rowTwo = new Vector<String>();
        rowTwo.addElement("Row2-Column1");
        rowTwo.addElement("Row2-Column2");
        rowTwo.addElement("Row2-Column3");

        Vector<Vector> rowData = new Vector<Vector>();
        rowData.addElement(rowOne);
        rowData.addElement(rowTwo);

        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Time");
        columnNames.addElement("Length");
        columnNames.addElement("ID");
        JTable table = new JTable(rowData, columnNames);

        table.setValueAt("11:00 Wed", 0, 0);    

        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(300, 150);
        frame.setVisible(true);

  }
}

As shown in Creating a Table Model , separate your model ( TableModel ) from the view ( JTable ). 创建表模型中所示,从视图JTable )中分离模型TableModel )。 Then you can initialize the model from a backing store at startup and record new rows in your TableModelListener as they are entered. 然后,您可以在启动时从后备存储初始化模型,并在输入新行时在TableModelListener记录新行。 The typical approach uses an embedded database, such as H2 Database Engine . 典型的方法使用嵌入式数据库,例如H2数据库引擎 Alternatively, java.util.Preferences is fine for modest needs; 另外, java.util.Preferences适合中等需求。 RCPrefs is an example. RCPrefs是一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM