如何向打开的JTable添加新行

时间:2014-12-07 07:05:08

标签: java swing jtable defaulttablemodel

我在向JTable添加新行时遇到问题。基本上,每次单击“添加”按钮时,它都会加载新的JTable并添加新行,我知道这种情况发生在哪里,但不知道如何修复它。

我有一种方法来读取文本文件并将数据加载到DefaultTableModel中。单击按钮时将执行此方法,然后将来自DefaultTableModel的数据传递到下一帧中的JTable。也许这不是正确的方法吗?

请您指导我如何修复它?任何输入将非常感激。谢谢。

这是具有JTable的框架。

public class TableFrame extends JFrame {
    public TablePanel tbl;
    public JPanel panel;
    public CardLayout card;
    public JButton add, save, delete, close, exit;
    public String[] headers;
    public DefaultTableModel model;

    public TableFrame()
    {
        super("Address Book");

        model = new DataTable().Table();
        JTable addressTable = new JTable(model);                    

        JTableHeader header = addressTable.getTableHeader();
        header.setBackground(Color.ORANGE);
        header.setForeground(Color.BLACK);
        header.setFont(new Font("SansSerif", Font.BOLD, 14));
        JScrollPane sp = new JScrollPane(addressTable);

        panel = new JPanel();
        card = new CardLayout();
        panel.setLayout(card);             
        tbl = new TablePanel();
        panel.add(sp);
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));


        JPanel buttonPanel = new JPanel();
        add = new JButton("Add");
        save = new JButton("Save");
        delete = new JButton("Delete");
        close = new JButton("Close");
        exit = new JButton("Exit");

        Dimension d = new Dimension(100, 30);
        add.setPreferredSize(d);
        save.setPreferredSize(d);
        delete.setPreferredSize(d);
        close.setPreferredSize(d);
        exit.setPreferredSize(d);

        TableFrameButtonHandler handle = new TableFrameButtonHandler(this);       
        add.addActionListener(handle);
        save.addActionListener(handle);
        delete.addActionListener(handle);
        close.addActionListener(handle);
        exit.addActionListener(handle);

        buttonPanel.add(add);
        buttonPanel.add(save);
        buttonPanel.add(delete);
        buttonPanel.add(close);
        buttonPanel.add(exit);

        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        setSize(840, 540);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }
}

这是添加按钮。我知道因为我发起了一个新的DefaultTableModel,这就是它创建新框架的原因。

public class TableFrameButtonHandler implements ActionListener{

    TableFrame frame;
    String lastName;
    String firstName;
    String address;
    String city;
    String state;
    String phone;

    public TableFrameButtonHandler(TableFrame frame)
    {
        this.frame = frame;
    }

    public TableFrameButtonHandler() {
    } 

    public void actionPerformed(ActionEvent e)
    {        
        if(e.getSource() == frame.add)
        {           
            DefaultTableModel model = new TableFrame().model;
            Object [] row = new Object [6];
            row[0] = lastName;
            row[1] = firstName;
            row[2] = address;
            row[3] = city;
            row[4] = state;
            row[5] = phone;
            model.addRow(row);         
        }

        if(e.getSource() == frame.delete)
        {

        }

        if(e.getSource() == frame.close)
        {
            frame.dispose();            
        }

        if(e.getSource() == frame.exit)
            System.exit(0);
    }
}

0 个答案:

没有答案