Minggu, 12 Juni 2011

JTable as Spreadsheet - The Outline

Developing a spreadsheet in JTable can be done in these steps:
  1. Prepare a data structure to hold the formula and the value; Linked List preferred.
  2. Prepare a JTable to display the data structure prepared.
  3. 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.

Kamis, 21 April 2011

MDF in Microsoft Visual Studio 2010

MDF is a file which is used by SQL Server to save its data. It can be found in your SQL Server installation folder, whether it's express or not. But be careful, the MDF files should always with the LDF files in order for it to work.

Connecting to an MDF file to Microsoft Visual Studio 2010, can be done through Server Explorer. Look at the exhibit below.


Then, you have to choose Microsoft SQL Server Database File in Change Data Source dialog.

If you're facing the exhibit below, just point your mouse to Change button and change the data source.

At last, browse for the MDF file you want to connect.

Voila! The MDF file can then be read for your application.

ps: To be able to edit the content of the MDF files, you should put the files in unrestricted folder (a folder which has the priviledge to read, write, or modify).