How to add new row to opened JTable

166 Views Asked by At

I am having trouble adding a new row to my JTable. Basically, every time I click the Add button, it will load new frame with new JTable with new row added and I know where this happens but don't know how to fix it.

I have a method to read the text file and load data into DefaultTableModel. This method will execute when button is clicked then data from DefaultTableModel get passed through to JTable in below frame. Perhaps this is not the right way to do it?

Would you please guide me through how to fix it? Any inputs will be much appreciated. Thank you.

Here is the frame that has the 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);        
    }
}

And here is the Add button. I know because I initiate a new DefaultTableModel, that's why it creates new frame.

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

There are 0 best solutions below