将值从JTextFied添加到JTable

时间:2015-10-03 04:25:17

标签: java swing jtable jbutton actionlistener

group_cols = []
for x, d in enumerate(col_groups):
    try:
        group_cols.append({col_groups[x].keys()[0]:(col_groups[x].values()[0],(col_groups[(x+1)].values()[0] - 1))})
    except:
        group_cols.append({col_groups[x].keys()[0]:(col_groups[x].values()[0],tap_sh.max_column)})

此代码的问题是类Bill detais包含一些文本框和一个按钮BillTablepane类包含一个Jtable我想将BillDetaisaPanel中的项添加到Jtable

单击未显示任何错误但未插入值的Jbutton

完整的来源是有人请帮助我找到逻辑错误,

1 个答案:

答案 0 :(得分:2)

在actionPerformed方法中,您在第(1)行创建一个新的BillTablePanel对象,然后尝试在第(2)行添加到表模型:

public void actionPerformed(ActionEvent e) {

    BillTablePanel btp=new BillTablePanel();  // **** (1)
    // ...

    btp.model.addRow(data);  // ***** (2)

但要明白,新的BillTablePanel只是一个全新且独特的对象,与显示的对象完全无关。要更改显示数据的状态,必须在显示的BillTablePanel对象上调用方法,而不是在为actionPerformed方法创建的新对象上调用方法。

例如,这是一个类似的最小程序:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TableExample extends JPanel {
    private HoldsTable holdsTable = new HoldsTable();
    private JTextField lastNameField = new JTextField(10);
    private JTextField firstNameField = new JTextField(10); 

    public TableExample() {
        JPanel fieldPanel = new JPanel();
        fieldPanel.add(new JLabel("Last Name:"));
        fieldPanel.add(lastNameField);
        fieldPanel.add(new JLabel("First Name:"));
        fieldPanel.add(firstNameField);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("Your Action") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                HoldsTable ht = new HoldsTable(); // creates a new reference -- bad!
                String lastName = lastNameField.getText();
                String firstName = firstNameField.getText();

                ht.addName(lastName, firstName);
            }
        }));
        buttonPanel.add(new JButton(new AbstractAction("My Action") {

            @Override
            public void actionPerformed(ActionEvent evt) {
                // HoldsTable ht = new HoldsTable();
                String lastName = lastNameField.getText();
                String firstName = firstNameField.getText();

                // ht.addName(lastName, firstName);
                holdsTable.addName(lastName, firstName); // use the ref to the displayed object
            }
        }));

        setLayout(new BorderLayout());
        add(holdsTable, BorderLayout.CENTER);
        add(fieldPanel, BorderLayout.PAGE_START);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    private static void createAndShowGui() {
        TableExample mainPanel = new TableExample();

        JFrame frame = new JFrame("TableExample");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

class HoldsTable extends JPanel {
    private static final String[] COL_NAMES = { "Last Name", "First Name" };
    private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
    private JTable table = new JTable(model);

    public HoldsTable() {
        setLayout(new BorderLayout());
        add(new JScrollPane(table));
    }

    public void addName(String lastName, String firstName) {
        String[] row = { lastName, firstName };
        model.addRow(row);
    }
}

您的程序会创建一个新的不显示的对象,并更改其属性,类似于上面程序中的代码:

        @Override
        public void actionPerformed(ActionEvent evt) {
            HoldsTable ht = new HoldsTable(); // creates a new reference --
                                              // bad!
            String lastName = lastNameField.getText();
            String firstName = firstNameField.getText();

            ht.addName(lastName, firstName);
        }
    }));

但是,由于状态正在更改的对象ht,但在您的代码中btp,不是显示的对象,因此不会显示任何内容。

其他操作中显示了正确的方法:

        @Override
        public void actionPerformed(ActionEvent evt) {
            // HoldsTable ht = new HoldsTable();
            String lastName = lastNameField.getText();
            String firstName = firstNameField.getText();

            // ht.addName(lastName, firstName);
            holdsTable.addName(lastName, firstName); // use the ref to the
                                                     // displayed object
        }

我创建了一个GUI视图的字段,其中包含JTable,这里是holdsTable并在其上调用方法。由于holdTable可见,其状态的变化将显示在程序中(此处为JTable)。