[英]Adding a new row to JTable
我正在嘗試根據用戶輸入的內容向JTable添加新行,並使其重復次數與用戶注冊的次數相同。 例如,假設用戶在寄存器TF中輸入了“ Susie”,為lengthTF輸入了“ 11:00”,為IDTF輸入了“ 60”。 我可以毫無問題地將其作為行添加到我的JTable中的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();
}
}
這是我的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);
}
}
如創建表模型中所示,從視圖 ( JTable
)中分離模型 ( TableModel
)。 然后,您可以在啟動時從后備存儲初始化模型,並在輸入新行時在TableModelListener
記錄新行。 典型的方法使用嵌入式數據庫,例如H2數據庫引擎 。 另外, java.util.Preferences
適合中等需求。 RCPrefs
是一個示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.