向JTable添加新行

时间:2012-02-08 23:21:36

标签: java swing jtable rows

我正在尝试根据用户输入的内容向我的JTable添加一个新行,并在用户注册时重复该次数。例如,假设用户在registerTF中输入“Susie”,在lengthTF中输入“11:00”,在IDTF中输入“60”。我可以在examInfo.java中将其作为一行添加到我的JTable中,没有任何问题,但是如何重复执行此操作以便下次在运行Registration.java时将在这些字段中输入的所有内容放入一个全新的行中我的JTable。在有人要求之前,不是这不是家庭作业我是为家庭成员做的。

这是我的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();
}  
}

这是我的testInfo.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);

  }
}

1 个答案:

答案 0 :(得分:1)

Creating a Table Model所示,将模型TableModel)与视图JTable)分开。然后,您可以在启动时从后备存储中初始化模型,并在输入时在TableModelListener中记录新行。典型方法使用嵌入式数据库,例如H2 Database Engine。或者,java.util.Preferences可以满足适度的需求; RCPrefs就是一个例子。