- Prepare a data structure to hold the formula and the value; Linked List preferred.
- Prepare a JTable to display the data structure prepared.
- Create a module so that whenever there are changes in JTable, the data structure is automatically synchronized.
JTable displays data by interpreting TableModel. It is easily achieved by setting the model of the JTable with the object of DefaultTableModel. This DefaultTableModel can also be configured to detect any changes happened to the model. Thus, any addition of the data in the model can be detected for later be added into the data structure. Refer to the listing below:
DefaultTableModel model = new DefaultTableModel();model.addColumn("A");model.addColumn("B");model.addColumn("C");model.setNumRows(10);model.addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { DefaultTableModel modelAsal=(DefaultTableModel)e.getSource(); int baris = e.getFirstRow(); int kolom = e.getColumn(); System.out.println(modelAsal.getValueAt(baris, kolom)); }});jTable1.setModel(model);addTableModelListener is a method to add TableModelListener event to detect any data changes. The data can then be obtained by first getting the model by using the e.getSource() to get the data specified by baris and kolom variable. The data obtained can then be added or manipulated as needed.
This explanation is a supplement for Data Structure 2010/2011 project in Sekolah Tinggi Teknik Surabaya.