简体   繁体   中英

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. For example let's say the user entered "Susie" in registerTF, "11:00" for lengthTF, and "60" for IDTF. 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. Before anyone asks, no this is not homework I'm doing it for a family member.

Here is my Registration.java code:

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:

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 ). Then you can initialize the model from a backing store at startup and record new rows in your TableModelListener as they are entered. The typical approach uses an embedded database, such as H2 Database Engine . Alternatively, java.util.Preferences is fine for modest needs; RCPrefs is an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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