Wednesday, January 21, 2009

[Java] Creating JTable from Resultset

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel; 

public
class ResultTableModel extends AbstractTableModel {
      /**
       *
       */
      private static final long serialVersionUID = 1L;
      boolean DEBUG = false;
      private String[] columnNames;
      private Object[][] data;

      
public ResultTableModel(ResultSet rs, String[] columnNames) {
            ResultSetMetaData meta;
            if (rs != null) {
                  this.columnNames = columnNames;
                  try {
                        meta = rs.getMetaData();
                        int rowcount = 0, colcount = 0;
                        if (rs.last()) {
                              rowcount = rs.getRow();
                              rs.first();
                        }
                        colcount = meta.getColumnCount();
                        data = new Object[rowcount][colcount];
                        int i = 0;
                        do {
                              for (int j = 0; j < colcount; j++) {
                                    data[i][j] = rs.getObject(j + 1);
                              }
                              i++;
                        } while (rs.next());
                  } catch (SQLException e) {
                        e.printStackTrace();
                  }
            }
      }

      
public int getColumnCount() {
            return columnNames.length;
      }

      
public int getRowCount() {
            return data.length;
      }

      
public String getColumnName(final int col) {
            return columnNames[col];
      }

      
public Object getValueAt(final int row, final int col) {
            return data[row][col];
      }

      
/*
       * JTable uses this method to determine the default renderer/ editor for
       * each cell. If we didn't implement this method, then the last column would
       * contain text ("true"/"false"), rather than a check box.
       */
      public Class getColumnClass(final int c) {
            return getValueAt(0, c).getClass();
      }

      
/*
       * Don't need to implement this method unless your table's editable.
       */
      public boolean isCellEditable(final int row, final int col) {
            // Note that the data/cell address is constant,
            // no matter where the cell appears onscreen.
            if (col < 2) {
                  return false;
            } else {
                  return true;
            }
      }

      
/*
       * Don't need to implement this method unless your table's data can change.
       */
      public void setValueAt(final Object value, final int row, final int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
      }
}

Monday, January 12, 2009

[Delphi] String Split

function TForm1.Split(s:String): TStringList;
var
sl : TStringList;
i : Integer;
tmp : String;
begin
sl := TStringList.Create;
for i := 1 to Length(s) do
begin
if s[i] <> ',' then
begin
if s[i] <> '"' then // skip "
begin
tmp := tmp + s[i];
end;
end else
begin
sl.Add(tmp);
tmp := '';
end;
end;
sl.Add(tmp);
Result := sl;
end;

Thursday, January 8, 2009

Windows XP Service Shortcut

Do you know that Windows XP has a shortcut which can be activated by put it into “Run” menu.
for the example press “Start” and Then Press “Run“. and the dialog box will show up.

and then fill it with the service that you want “perfmon.msc”, press OK

” The Performance Monitor ” will comes up to your windows

as easy as that.

hereunder other, dissimilar example of service name which can be activated by through “Run” menu.
* Component Services - comexp.msc
* Computer Management - compmgmt.msc
* Device Manager - devmgmt.msc
* Disk Defrag - dfrg.msc
* Disk Managment - diskmgmt.msc
* Event Viewer - eventvwr.msc
* Group Policies - gpedit.msc
* Local Security Settings - secpol.msc
* Local Users and Groups - lusrmgr.msc
* Performance Monitor - perfmon.msc
* Resultant Set of Policies - rsop.msc
* Services - services.msc
* Shared Folders - fsmgmt.msc


Original from: Windows XP service shortcut: Posted by: trueheart77 | September 25, 2007

Sunday, January 4, 2009

[Delphi] Canvas Print Right Aligned Text


As you know, TCanvas don't have a method to print right aligned text. So what to do?

If the text is left aligned, it should start at X1. Now we want to start from X2, so we use the calculation above to get our X2.

procedure TForm1.Button3Click(Sender: TObject);
var
  X1,X2,W1,W2,W3: Integer;
  _text: String;
begin
  W1 := 100;
  X1 := 5;   // Original X
  _text := '12345.78';
  with Image1.Canvas do
  begin
    W2 := TextWidth(_text);
    W3 := W1 - W2;
    X2 := X1 + W3;
    TextOut( X2, 5, _text);   // 5 = Y
  end;
end;