如何将数据添加到JTable?

时间:2014-05-31 11:34:49

标签: java swing jtable defaulttablemodel

我希望我的用户能够输入4个允许他们填充JTable的字段。我在CreateFxitures课程中有一个JPanel。在这个JPanel上,他们能够将数据输入两个ComboBoxes和2个TextFields。输入所有数据后,应单击Create按钮,将所有数据添加到输入表中的rowNames数组中。但是,当他们单击Create时,该行会创建一个新行,但不会添加数据。我该怎么做呢?

主要课程:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Main 
{

    public Main()
    {
        final JFrame window = new JFrame ("AQA Computer Science Application");

        final CardLayout c1 = new CardLayout();
        final JPanel container = new JPanel(c1);

        Home homeCall = new Home();
        GuestFixtures guestCall = new GuestFixtures();
        final Login loginCall = new Login();
        final AdminFixtures adminCall = new AdminFixtures();
        final CreateFixtures createCall = new CreateFixtures();

        container.add(homeCall, "1");
        container.add(guestCall, "2");
        container.add(loginCall, "3");
        container.add(adminCall, "4");
        container.add(createCall, "5");
        createCall.createB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {

                if (createCall.descTF.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "All fields must be entered");
                }else{
                    c1.show(container, "4");

                    adminCall.model.addRow(createCall.rowData);

                }

            }
        });

    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });

    }

}

CreateFixtures:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class CreateFixtures extends JPanel
{

    JButton backB, createB;
    JLabel typeL, descL, dateL, resultL;
    JComboBox typeCB, monthCB, dayCB;
    JTextField descTF, resultTF;
    String[] typeCBSTR = { "Football", "Tennis", "Swimming"};
    String[] monthCBSTR = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    String[] dayINT = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", };
    Object typeObject, monthObject;
    String typeSTR, descSTR, monthSTR, resultSTR;
    protected Vector rowData;
    public CreateFixtures()
    {

        final AdminFixtures adminCall = new AdminFixtures();

        this.setLayout(new BorderLayout());

        JPanel createC = new JPanel(new GridLayout(12, 4));
        typeL = new JLabel("Type of Sport");
        typeCB = new JComboBox(typeCBSTR);
        descL = new JLabel("Description");
        descTF = new JTextField();
        dateL = new JLabel("Date");
        monthCB = new JComboBox(monthCBSTR);
        dayCB = new JComboBox(dayINT);
        resultL = new JLabel("Result");
        resultTF = new JTextField("TBA");

        createC.add(typeL);
        createC.add(typeCB);
        createC.add(descL);
        createC.add(descTF);
        createC.add(dateL);
        createC.add(monthCB);
        createC.add(dayCB);
        createC.add(resultL);
        createC.add(resultTF);

        typeObject = typeCB.getSelectedItem();
        typeSTR = typeObject.toString();
        descSTR = descTF.getText();
        monthObject = monthCB.getSelectedItem();
        resultSTR = resultTF.getText();

        Object[] rowData = {typeSTR, descSTR, monthSTR, resultTF};

        JPanel createS = new JPanel(new FlowLayout());
        backB = new JButton("Back");
        createB = new JButton("Create");

        createS.add(backB);
        createS.add(createB);

        createC.setBackground(Color.LIGHT_GRAY);
        createS.setBackground(Color.GRAY);

        add(createC, BorderLayout.CENTER);
        add(createS, BorderLayout.SOUTH);


    }

}

AdminFixtures:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class AdminFixtures extends JPanel
{

    JButton backB, createFixturesB, deleteButton;
    JTable adminFixturesTable;
    String[] columnNames = {"Fixture", "Description", "Date", "Result"};
    String[][] rowNames = null;
    DefaultTableModel model;

    public AdminFixtures()
    {

        this.setLayout(new BorderLayout());

        JPanel adminC = new JPanel();
        adminFixturesTable = new JTable(new DefaultTableModel(rowNames, columnNames));
        JScrollPane scrollpane = new JScrollPane(adminFixturesTable);
        model = (DefaultTableModel) adminFixturesTable.getModel();

        JPanel adminS = new JPanel(new FlowLayout());
        backB = new JButton("Back");
        createFixturesB = new JButton("New Fixture");
        deleteButton = new JButton("Delete Row");

        adminS.add(backB);
        adminS.add(createFixturesB);
        adminS.add(deleteButton);

        adminC.setBackground(Color.LIGHT_GRAY);
        adminS.setBackground(Color.GRAY);

        add(scrollpane, BorderLayout.CENTER);
        add(adminS, BorderLayout.SOUTH);

    }

}

1 个答案:

答案 0 :(得分:1)

首先使用getText()获取字段值。然后从值创建一个Object数组。然后像你一样使用addRow(array)。您当前尝试添加的数组包含所有空值。所以你只需要获取文本字段的当前值,然后用它们添加行。忘掉createCall.rowData。它没有为你做任何事。每按一次按钮,创建一个新的Object[]。像

这样的东西
String data1 = typeSTR.getText();
String data2 = descSTR.getText();
String data3 = monthSTR.getText();
String data4 = resultSTR.getText();
Object[] row = { data1, data2, data3, data4 };
model.addRow( row );