我试图使用DefaultTableModel将数据动态地添加到JTable中,但是它不起作用。

我创建了两个类:一个类(Input.java)代表一个弹出窗口,该弹出窗口允许您写入数据(名称,选项和约束)并保存。另一个类表示一个包含表(InputMain.java)的弹出窗口。然后,InputMain将这些信息(如果收到)转换为表中的一行,并在三列中列出所有这些信息。在Input中,我创建了InputMain的实例,并将表的模型设置为新的DefaultTableModel()(如果不这样做,它将引发错误)(在InputMain中已经创建了表,因此如果我创建了InputMain的实例,可能是在InputMain中调用了该表。然后,根据actionPerformed(ActionEvent e),在JPanel中单击“输入”按钮后,它将创建一个具有nameText,optionText和约束Text的新数据数组。最后,数据将添加到模型中。

我不知道为什么不能将所有这些数据添加到表中,除了上面的方法对我来说听起来很合逻辑。

这是InputMain.java类:

    package com.company;

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

/**
 * Created by paull on 14/5/2016.
 */
public class InputMain {
    JButton add;
    JButton change;
    JButton delete;
    JTable table;
    String[] columns;
    String[][] data;
    JButton create;
    JPanel labelPanel;
    JPanel bigPanel;
    JPanel centerPanel;

    public InputMain() {
        int fontSize = 50;
        add = new JButton("add");
        change = new JButton("change");
        delete = new JButton("delete");
        create = new JButton("create");
        columns = new String[]{"name","option","constraint"};
        data = new String[][]{{"","",""}};

        table = new JTable(data,columns) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; //to avoid it from being changable by double clicking any data
            }
        };
        table.setPreferredScrollableViewportSize(new Dimension(450,63));
        table.setFillsViewportHeight(true);

        JScrollPane jsp = new JScrollPane(table); // not always can the table be fully shown --> add a scrollbar


        add.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        change.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        delete.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        create.setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        table.setFont(new Font(table.getName(),Font.PLAIN,fontSize));
        //table.getEditorComponent().setFont(new Font(add.getName(),Font.PLAIN,fontSize));
        table.getTableHeader().setFont(new Font(table.getName(),Font.PLAIN,fontSize));

        labelPanel = new JPanel();
        labelPanel.add(add);
        labelPanel.add(change);
        labelPanel.add(delete);
        labelPanel.setLayout(new FlowLayout(10,30,10));

        centerPanel = new JPanel();
        centerPanel.add(labelPanel);
        centerPanel.setLayout(new GridBagLayout());

        bigPanel = new JPanel();
        bigPanel.setLayout(new GridLayout(3,0));
        bigPanel.add(centerPanel);
        bigPanel.add(jsp);
        bigPanel.add(create);

        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InputFrame s = new InputFrame();
                Input i = new Input();
                s.add(i.labelPanel);
                s.setVisible(true);
            }
        }); // pressing add button pops up the Input Frame

    }

}


这是Input.java类:

package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by paull on 14/5/2016.
 */
public class Input extends JPanel{

    JLabel nameLabel;
    JLabel optionLabel;
    JLabel constraintLabel;
    JButton enter;
    JPanel labelPanel;
    JPanel jp2;
    JPanel jp3;
    JPanel jp4;
    JTextField nameText;
    String[] optionStrings;
    JComboBox<String> optionText;
    JCheckBox wk1;
    JCheckBox wk2;
    JCheckBox wk3;
    JCheckBox wk4;
    JCheckBox wk5;


    public Input() {
        nameLabel = new JLabel("name: ");
        optionLabel = new JLabel("option: ");
        constraintLabel = new JLabel("constraint:");
        enter = new JButton("add");
        nameText = new JTextField(10);
        Options c = new Options();
        optionStrings = new String[]{c.satNight,c.sunEight,c.sunNineThirty,c.sunEleven,c.sunNight};
        optionText = new JComboBox<String>(optionStrings);
        wk1 = new JCheckBox("1");
        wk2 = new JCheckBox("2");
        wk3 = new JCheckBox("3");
        wk4 = new JCheckBox("4");
        wk5 = new JCheckBox("5");


        int fontSize = 50;
        nameLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        constraintLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        enter.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        nameText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk1.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk2.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk3.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk4.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk5.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));

        labelPanel = new JPanel();
        labelPanel.add(nameLabel);
        labelPanel.add(nameText);
        labelPanel.add(optionLabel);
        labelPanel.add(optionText);
        labelPanel.add(constraintLabel);
        labelPanel.add(wk1);
        labelPanel.add(wk2);
        labelPanel.add(wk3);
        labelPanel.add(wk4);
        labelPanel.add(wk5);
        labelPanel.add(enter);
        labelPanel.setLayout(new FlowLayout(10,30,10));


        InputMain i = new InputMain();
        i.table.setModel(new DefaultTableModel());
        DefaultTableModel model = (DefaultTableModel) i.table.getModel();

        enter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String constraintText = "";
                String[] data = new String[]{nameText.getText(),optionText.getSelectedItem().toString(),constraintText};
                model.addRow(data);

                nameText.setText("");

            }
        });

    }


}


提前致谢。

最佳答案

我认为问题出在InputMain的actionPerformed上。

       add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InputFrame s = new InputFrame();
                Input i = new Input();
                s.add(i.labelPanel);
                s.setVisible(true);
            }
        });


您构造Input的新实例。这意味着您每次单击添加按钮时都会创建一个新的空DefaultTableModel并将其附加到表中。尝试将输入的构造移到actionPerformed之外:

         Input i = new Input();
         add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    InputFrame s = new InputFrame();
                    s.add(i.labelPanel);
                    s.setVisible(true);
                }


更新
当然,Input内部的InputMain实例化也应该删除。而是在Input中具有TypeInputMain类型的成员:

private InputMain inputMain;


并更改Input的构造函数以设置值:

public Input (InputMain inputMain) {
    this.inputMain = inputMain;
    ...


并将InputMain中Input的实例化为

Input i = new Input(this);

07-24 13:40