Question

I'm having a problem to load table inside a JInternal frame, I can't describe it, but I'll show you a image so you understand what I mean.

pic

Here's my code:

--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
        }
    }
}

It'll give me this:

this

Was it helpful?

Solution

In your SupplierController.groovy, there is this:

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

You never use values returned from the SQL query. Depending on your code, model.kodeSupplier maybe null. Do you mean it.kodeSupplier?

Btw, for this kind of application, I'm using this plugin to generate MVCGroups based on domain class (from here). Using domain class will be easier than using a map.

OTHER TIPS

The easistes way to work with JTable/JTree/JList is to use GlazedLists (http://www.glazedlists.com/), fortunately there's a glazedlists plugin http://artifacts.griffon-framework.org/plugin/glazedlists

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top