2013-04-02 2 views
1

JInternal 프레임 안에 테이블을로드하는 데 문제가 있습니다. 설명 할 수는 없지만 이미지를 보여 주므로 내 뜻을 이해할 수 있습니다. 여기 JInternal Frame에서 데이터베이스 테이블을로드하는 방법은 무엇입니까?

pic

내 코드입니다 :

--SparepartController.groovy--

package sparepart 

import griffon.transform.Threading 

class SparepartController { 
    def model 
    def view 
    def builder 

    @Threading(Threading.Policy.INSIDE_UITHREAD_ASYNC) 

    def supplier = { 
     String id = 'supplier' 
     def (m, v, c) = createMVCGroup('supplier', id, title: "Supplier") 
     builder.desktopPane(view.desktop) { 
      widget(v.window) 
     } 
    } 

    def logout = { 

    } 

    def quit = { 
     app.shutdown() 
    } 
} 

--SparepartView.groovy--

package sparepart 

actions { 
    action(id: 'loginAction', 
      name: 'Login') 
    action(id: 'logoutAction', 
      enabled: bind {model.auth}, 
      name: 'Logout', 
      closure: controller.logout) 
    action(id: 'quitAction', 
      name: 'Quit', 
      closure: controller.quit) 
    action(id: 'jenisBarangAction', 
      name: 'Jenis Barang', 
    ) 
    action(id: 'barangAction', 
      name: 'Barang', 
    ) 
    action(id: 'supplierAction', 
      name: 'Supplier', 
      closure: controller.supplier 
    ) 
    action(id: 'pembelianAction', 
      name: 'Pembelian', 
    ) 
    action(id: 'penjualanAction', 
      name: 'Penjualan', 
    ) 
    action(id: 'userAction', 
      name: 'User', 
    ) 
    action(id: 'reportBarangAction', 
      name: 'Barang', 
    ) 
    action(id: 'reportSupplierAction', 
      name: 'Supplier', 
    ) 
    action(id: 'reportPembelianAction', 
      name: 'Pembelian', 
    ) 
    action(id: 'reportPejualanAction', 
      name: 'Penjualan', 
    ) 
    action(id: 'reportBarangTerlarisAction', 
      name: 'Barang Terlaris', 
    ) 
} 

application(title: 'DIKA MOTOR', 
    preferredSize: [950, 650], 
    pack: true, 
    //location: [50,50], 
    locationByPlatform: true, 
    iconImage: imageIcon('/griffon-icon-48x48.png').image, 
    iconImages: [imageIcon('/griffon-icon-48x48.png').image, 
       imageIcon('/griffon-icon-32x32.png').image, 
       imageIcon('/griffon-icon-16x16.png').image]) { 
    // add content here 
    menuBar { 
     menu('Menu') { 
      menuItem loginAction 
      menuItem logoutAction 
      menuItem quitAction 
     } 

     //if(model.auth) { 
      menu('Master Data') { 
       menuItem jenisBarangAction 
       menuItem barangAction 
       menuItem supplierAction 
      } 
      menu('Transaksi') { 
       menuItem pembelianAction 
       menuItem penjualanAction 
      } 
      menu('Utility') { 
       menuItem userAction 
      } 
      menu('Report') { 
       menuItem reportPembelianAction 
       menuItem reportPejualanAction 
       menuItem reportSupplierAction 
       menuItem reportBarangTerlarisAction 
      } 
     //} 
    } 
    desktopPane(id: 'desktop') 
} 

--SupplierController.groovy -

package sparepart 

import groovy.sql.DataSet 
import groovy.sql.Sql 

class SupplierController { 
    def model 
    def view 
    private String id 

    void mvcGroupInit(Map args) { 
     id = args.mvcName 

     edt { 
      model.listSupplier.clear() 
     } 

     withSql { dataSourceName, Sql sql -> 
      sql.eachRow("SELECT * FROM tblsupplier") { 
       Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email] 
       edt { 
        model.listSupplier << baris 
       } 
      } 
     } 
    } 

    void mvcGroupDestroy() { 
     execOutsideUI() { 
      def desktop = view.window.parent 
      desktop.remove view.window 
      desktop.invalidate() 
      desktop.repaint() 
     } 
    } 

    def keluar = { destroyMVCGroup id } 

    def simpan = { 
     withSql { dataSourceName, Sql sql -> 
      DataSet supplier = sql.dataSet("tblsupplier") 
      if (sql.firstRow("SELECT kodeSupplier FROM tblsupplier WHERE kodeSupplier = ${model.kodeSupplier}") != null) { 
       // sudah ada NIM yang sama, maka lakukan update. 
       supplier.executeUpdate("UPDATE tblsupplier SET namaSupplier = ${model.namaSupplier}, alamat = ${model.alamat}, telp = ${model.telp}, email = ${model.email} WHERE kodeSupplier = ${model.kodeSupplier}") 

       // update baris di model 
       edt { 
        int index = model.listSupplier.findIndexOf{it['kodeSupplier']==model.kodeSupplier} 
        model.listSupplier[index] += [namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email] 
       } 

      } else { 
       // belum ada NIM, maka tambahkan. 
       supplier.add(kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email) 

       // tambahkan baris baru ke model 
       edt { model.listSupplier << [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email] } 
      } 
     } 
    } 

    def hapus = { 
     withSql { dataSourceName, Sql sql -> 
      sql.executeUpdate("DELETE FROM tblsupplier WHERE kodeSupplier = ${model.kodeSupplier}") 
     } 

     // menghapus baris dari model 
     edt { 
      int index = model.listSupplier.findIndexOf{it['kodeSupplier']==model.kodeSupplier} 
      model.listSupplier.remove(index) 
     } 
    } 

    def refresh = { app -> 
     withSql { dataSourceName, Sql sql -> 
      sql.eachRow("SELECT * FROM tblsupplier") { 
       Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, alamat: model.alamat, telp: model.telp, email: model.email] 
       edt { 
        model.listSupplier << baris 
       } 
      } 
     } 
    } 
} 

--SupplierModel.groovy--

package sparepart 

import ca.odell.glazedlists.BasicEventList 
import ca.odell.glazedlists.EventList 
import ca.odell.glazedlists.swing.EventSelectionModel 
import groovy.beans.Bindable 

class SupplierModel { 
    @Bindable String kodeSupplier 
    @Bindable String namaSupplier 
    @Bindable String alamat 
    @Bindable String telp 
    @Bindable String email 

    EventList listSupplier = new BasicEventList() 
    EventSelectionModel eventSelectionModel 

--SupplierView.groovy--

package sparepart 

import ca.odell.glazedlists.FilterList 
import ca.odell.glazedlists.impl.filter.StringTextFilterator 
import ca.odell.glazedlists.swing.EventSelectionModel 
import ca.odell.glazedlists.swing.TextComponentMatcherEditor 
import net.miginfocom.swing.MigLayout 

// Helper untuk mengambil nilai kolom dari baris terpilih 
def getSelectedValue = { kolom -> 
    EventSelectionModel selection = model.eventSelectionModel 
    if (!selection.isSelectionEmpty()) { 
     selection.getSelected()[0][kolom] 
    } else { 
     null 
    } 
} 

internalFrame(title: title, size: [650, 500], id: 'window', visible: true, resizable: true, iconifiable: true, maximizable: true) { 
    borderLayout() 

    panel(constraints: PAGE_START) { 
     flowLayout(alignment: FlowLayout.RIGHT) 
     label("Cari: ") 
     textField(id: "txtPencarian", columns: 10) 
    } 

    scrollPane(constraints: CENTER) { 
     table (rowSelectionAllowed: true, id: 'table') { 
      eventTableModel(source: new FilterList(model.listSupplier, 
        new TextComponentMatcherEditor(txtPencarian, new StringTextFilterator())), 
        format: defaultTableFormat(columnNames: ['Kode Supplier', 'Nama Supplier', 'Alamat', 'Telp', 'Email'])) 
      model.eventSelectionModel = installEventSelectionModel(source: model.listSupplier) 
     } 
    } 

    panel(layout: new MigLayout('', '[right][left,grow]',''), constraints: PAGE_END) { 
     label('Kode Supplier:') 
     textField(id: 'txtKodeSupplier', columns: 5, text: bind(target: model, targetProperty: 'kodeSupplier'), constraints: 'wrap') 
     label('Nama Supplier:') 
     textField(id: 'txtNamaSupplier', columns: 20,text: bind(target: model, targetProperty: 'namaSupplier'), constraints: 'wrap') 
     label('Alamat:') 
     textField(id: 'txtAlamat', columns: 100, text: bind(target: model, targetProperty: 'alamat'), constraints: 'growx, wrap') 
     label('Telp.:') 
     textField(id: 'txtTelp', columns: 20,text: bind(target: model, targetProperty: 'telp'), constraints: 'wrap') 
     label('Email:') 
     textField(id: 'txtEmail', columns: 30,text: bind(target: model, targetProperty: 'email'), constraints: 'wrap') 

     bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged', 
       sourceValue: {getSelectedValue("kodeSupplier")}, target: txtKodeSupplier, targetProperty: 'text') 
     bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged', 
       sourceValue: {getSelectedValue("namaSupplier")}, target: txtNamaSupplier, targetProperty: 'text') 
     bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged', 
       sourceValue: {getSelectedValue("alamat")}, target: txtAlamat, targetProperty: 'text') 
     bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged', 
       sourceValue: {getSelectedValue("telp")}, target: txtTelp, targetProperty: 'text') 
     bind(source: model.eventSelectionModel, sourceEvent: 'valueChanged', 
       sourceValue: {getSelectedValue("email")}, target: txtEmail, targetProperty: 'text') 

     panel(constraints: 'span, growx, wrap') { 
      flowLayout() 
      button("Simpan", actionPerformed: controller.simpan) 
      button("Hapus", enabled: bind (source: model.eventSelectionModel, 
        sourceEvent: 'valueChanged', sourceValue: {!model.eventSelectionModel.isSelectionEmpty()}), 
        actionPerformed: controller.hapus) 
      //button("Refresh", actionPerformed: controller.refresh) 
      button keluarAction 
     } 
    } 
} 

그것은 나를이주지 :

this

답변

0

http://artifacts.griffon-framework.org/plugin/glazedlists 플러그인있어이 있습니다 :

sql.eachRow("SELECT * FROM tblsupplier") { 
    // Look here! 
    Map baris = [kodeSupplier: model.kodeSupplier, namaSupplier: model.namaSupplier, 
     alamat: model.alamat, telp: model.telp, email: model.email] 
    ... 
} 

당신은 값이 SQL 쿼리에서 반환 된 사용하지 않습니다. 코드에 따라 model.kodeSupplier 일 수 있습니다. it.kodeSupplier을 의미합니까?

Btw, 이런 종류의 응용 프로그램에서는이 plugin을 사용하여 도메인 클래스 (from here)를 기반으로 MVCGroups를 생성합니다. 도메인 클래스를 사용하면지도를 사용하는 것보다 쉽습니다.

+0

안녕하세요. 감사. – hendrakmg

0

에게 JTable/JTree/JList로 작업하는 easistes 방법은 GlazedLists (http://www.glazedlists.com/)는, 다행히 glazedlists이 SupplierController.groovy에서

+0

안녕하세요, 아말감입니다. 방금 코드를 추가했습니다. 아마 제 코드를 살펴볼 수 있습니다. – hendrakmg

+0

루프 내부에서 목록을 업데이트하지 않는 편이 낫습니다. 그러나 만약 당신이 결과로 "스트림"을 원한다면, 나는 edt 대신에 doLater로 바꿀 것이다. 더 나은 방법은 doLater/edt/doOutside 대신 execInsideUIAsync/execInsideUISync/execOutsideUI로 전환하는 것입니다. 그것들은 100 % 동등하지만 이전 방법의 이름은 그들의 의도와 모호하지 않습니다. – aalmiray

관련 문제