I have this program here which has a table and an add button. By default there are already two data. I want to implement it in such a way that when I add new item by clicking the add button, I want the table to be updated automatically. I have been searching for this thing for last 2 days. I have tried lots of things but none of them work.
Probably I need to set setValueAt() method as well which I do not know how to implement in this situation. Here is my full code. You can test it.
Any suggestions or helps are highly appreciated.
public class TableDemo extends JPanel implements ActionListener{ private JLabel name, contact; private JTextField nameField, contactField; private static JButton addButton, ok; JTable table; JFrame myFrame; MyTableModel myModel; public TableDemo() { super(new GridLayout(1, 0)); myModel = new MyTableModel(new Contact("Jeff", "456")); table = new JTable(myModel); table.setFillsViewportHeight(true); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); // Create the scroll pane and add the table to it. JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to this panel. add(scrollPane); } class MyTableModel extends AbstractTableModel { private String[] columnNames = { "Full Name", "Contact Number" }; ArrayList<Contact> dataList = new ArrayList(); public MyTableModel(Contact c) { dataList.add(c); dataList.add(new Contact("Mary", "456")); } public MyTableModel(ArrayList mylist) { for (int i = 0; i < dataList.size(); i++) { dataList.add((Contact) mylist.get(i)); } } public MyTableModel() { // TODO Auto-generated constructor stub } public int getColumnCount() { return columnNames.length; } public int getRowCount() { return dataList.size(); } public String getColumnName(int col) { return columnNames[col]; } public void setValueAt(Contact value, int row, int col) { ArrayList object = dataList; object.add(value); dataList.set(row, value); fireTableDataChanged(); } public Object getValueAt(int row, int col) { Contact widget = (Contact) dataList.get(row); switch (col) { case 0: return widget.getFullName(); case 1: return widget.getPhoneNumber(); default: return null; } } public boolean isCellEditable(int row, int col) { return false; } public void addContact(Contact c) { dataList.add(c); myModel.fireTableDataChanged(); // fireTableRowsInserted(dataList.size() - 1, dataList.size() - 1); } } private static void createAndShowGUI() { // Create and set up the window. JFrame frame = new JFrame("TableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create and set up the content pane. TableDemo newContentPane = new TableDemo(); newContentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(newContentPane); addButton = new JButton("Add"); JPanel myPanel = new JPanel(); myPanel.add(addButton); addButton.addActionListener(new TableDemo()); frame.setLayout(new GridLayout(2, 1)); frame.setResizable(false); frame.setSize(500, 500); frame.add(myPanel); // Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // Schedule a job for the event-dispatching thread: // creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub newFrame(); } public void newFrame() { myFrame = new JFrame(); JPanel myPanel = new JPanel(); name = new JLabel("Full Name"); nameField = new JTextField(10); contact = new JLabel("Contact Number"); contactField = new JTextField(10); ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myModel.addContact(new Contact(nameField.getText(), contactField.getText())); myFrame.dispose(); } }); myPanel.add(name); myPanel.add(nameField); myPanel.add(contact); myPanel.add(contactField); myPanel.add(ok); myFrame.add(myPanel); myFrame.setVisible(true); myFrame.setSize(250, 250); } }
AND THE CONTACT CLASS IS HERE
public class Contact { String fullName, phoneNumber; public Contact(String fullName, String phoneNumber) { this.fullName = fullName; this.phoneNumber = phoneNumber; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } }