J6/Grid/Examples

From J Wiki
Jump to navigation Jump to search
J6/Grid | Data Options Types | Actions Events Methods | Class DerivedClass | Hierarchical MultiDimensional MultiLine Virtual | Examples


Display numeric matrix given the data name

The following example creates a grid to display a numeric matrix. Only the data name is passed to the grid instance.

(!) Note: the grid control id and name in parent locale must be the same.

For J602 or J701(GTKWD) [{{#file: "griddemo1.ijs"}} Download script: griddemo1.ijs ]

require 'gtkwd jzgrid'
cocurrent 'myform'

CELLDATA=: *./~i.20

GDEMO=: 0 : 0
pc gdemo;
xywh 0 0 200 100;cc grid isigraph rightmove bottommove;
pas 0 0;
rem form end;
)

gdemo_run=: 3 : 0
wd GDEMO
grid=: '' conew 'jzgrid'
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
show__grid 'celldata'
wd 'pshow'
evtloop^:(-.IFJ6)''
)

create=: gdemo_run

destroy=: 3 : 0
destroy__grid''
wd 'pclose'
codestroy''
)

gdemo_close=: destroy
create''

For J701 without GTKWD

NB. gtk-griddemo1.ijs

require 'jzgrid'
cocurrent 'myform'

coinsert 'jgtk jgl2'

CELLDATA=: *./~i.20

gdemo_run=: 3 : 0
window=: gtk_window_new 0
gtk_window_set_title window;'gdemo'
consig3 window;'delete-event';'gdemo_close';coname''
canvas=: glcanvas 400 200;coname''
GRIDHWNDC=: canvas
GRIDPID=: 'gdemo'
GRIDID=: 'grid'
gtk_container_add window,canvas
gtk_widget_show window
grid=: '' conew 'jzgrid'
show__grid 'celldata GRIDPID GRIDID GRIDHWNDC'
)

create=: gdemo_run

destroy=: 3 : 0
destroy__grid ''
gtk_widget_destroy window
codestroy''
)

gdemo_close=: destroy
create''

Display and retrieve data given the data name

This is the same example as above, except that clicking a button sets a global with the current grid values. Try changing some cells, click Save Grid, and then display the global GRID: [{{#file: "griddemo2.ijs"}} Download script: griddemo2.ijs ]

require 'gtkwd jzgrid'
cocurrent 'myform'

CELLDATA=: *./~i.20

GDEMO=: 0 : 0
pc gdemo;
xywh 6 6 48 12;cc savegrid button;cn "Save Grid";
xywh 0 25 200 100;cc grid isigraph rightmove bottommove;
pas 0 0;
rem form end;
)

gdemo_run=: 3 : 0
wd GDEMO
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
grid=: '' conew 'jzgrid'
show__grid 'celldata'
wd 'pshow'
evtloop^:(-.IFJ6)''
)

create=: gdemo_run

destroy=: 3 : 0
destroy__grid''
wd 'pclose'
codestroy''
)

gdemo_savegrid_button=: 3 : 0
GRID_base_=: CELLDATA__grid
)

gdemo_close=: destroy
create''

Pass values to the grid using pack

The following example passes several values to the grid, using the pack verb to create the argument. When a cell is changed, a message is displayed in the session. The data is read from the grid when it is closed. [{{#file: "griddemo3.ijs"}} Download script: griddemo3.ijs ]

require 'gtkwd jzgrid numeric pack'
cocurrent 'myform'

CELLDATA=: 0.01 round o.?10 10$1e4
CELLFMT=: 'c2'
CELLALIGN=: 2
GRIDLOC=: 'gridloc'

GDEMO=: 0 : 0
pc gdemo;
xywh 0 0 200 100;cc grid isigraph rightmove bottommove;
pas 0 0;
rem form end;
)

gdemo_run=: 3 : 0
wd GDEMO
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
gridloc=: '' conew 'jzgrid'
show__gridloc pack 'CELLDATA CELLFMT CELLALIGN GRIDLOC'
wd 'pshow'
evtloop^:(-.IFJ6)''
)

create=: gdemo_run

destroy=: 3 : 0
destroy__gridloc''
wd 'pclose'
codestroy''
)

grid_gridhandler=: 3 : 0
res=. 1
if. y -: 'change' do.
  old=. 0j2 ": readcell__gridloc Cell__gridloc
  new=. 0j2 ": Value__gridloc
  smoutput 'changed cell ',(":Cell__gridloc),' from ',old,' to ',new
end.
res
)

gdemo_close=: 3 : 0
CELLDATA=: CELLDATA__gridloc
destroy''
)

create''

Grids in a tabbed dialog

The following example displays two grids in a tabbed dialog. Note that the gridpid is set to the tab form name. [{{#file: "griddemo4.ijs"}} Download script: griddemo4.ijs ]

require 'gtkwd jzgrid pack'
cocurrent 'myform'

CELLDATA0=: *./~i.12
CELLDATA1=: +./~i.15
GRIDOPTS=: 'celldata gridpid gridid hdrcol hdrrow'

GMAIN=: 0 : 0
pc gmain;
xywh 0 0 200 150;cc tabs tab rightmove bottommove;
pas 0 0;
rem form end;
)

gmain_run=: 3 : 0
wd GMAIN
wd 'set tabs "Tab0" "Tab1"'
wd 'creategroup tabs'
gtab0_run''
gtab1_run''
wd 'creategroup'
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
gmain_setshow 0
wd 'pshow'
evtloop^:(-.IFJ6)''
)

gmain_run1=: 3 : 0
wd GMAIN
wd 'set tabs "Tab0" "Tab1"'
wd 'creategroup tabs'
wd GTAB0
wd GTAB1
wd 'creategroup'
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
gtab0_run''
wd 'setselect tabs 1'
gtab1_run''
wd 'setselect tabs 0'
gmain_setshow 0
wd 'pshow'
evtloop^:(-.IFJ6)''
)


create=: gmain_run1`gmain_run@.IFJ6

destroy=: 3 : 0
destroy__grid0''
destroy__grid1''
wd 'pclose'
codestroy''
)

gmain_setshow=: 3 : 0
wd 'setshow gtab0 ',":y=0
wd 'setshow gtab1 ',":y=1
)

gmain_tabs_button=: 3 : 0
gmain_setshow 0 ". tabs_select
)

gmain_close=: destroy

GTAB0=: 0 : 0
pc gtab0;
xywh 10 10 180 120;cc grid0 isigraph rightmove bottommove;
)

gtab0_run=: 3 : 0
wd^:IFJ6 GTAB0
celldata=. CELLDATA0
gridpid=. 'gtab0'
gridid=. 'grid0'
grid0=: conew 'jzgrid'
hdrcol=. 'A'
hdrrow=. 0
show__grid0 pack GRIDOPTS
)

GTAB1=: 0 : 0
pc gtab1;
xywh 10 10 180 120;cc grid1 isigraph rightmove bottommove;
)

gtab1_run=: 3 : 0
wd^:IFJ6 GTAB1
celldata=. CELLDATA1
gridpid=. 'gtab1'
gridid=. 'grid1'
grid1=: conew 'jzgrid'
hdrcol=. hdrrow=. 1
show__grid1 pack GRIDOPTS
)

create''

Linked grid and plot

The following example displays a grid and corresponding plot in the same form. The plot is updated whenever the grid cells are changed. [{{#file: "griddemo5.ijs"}} Download script: griddemo5.ijs ]

NB. grid + plot demo
NB.
NB. plot is updated when grid changes
require 'gtkwd grid plot'
coclass 'demo'
coinsert 'jgl2'

NB. =========================================================
DEMO=: 0 : 0
pc demo;pn "Grid";
xywh 0 0 121 56;cc grid isigraph;
xywh 0 58 231 101;cc gplot isigraph rightmove bottommove;
pas 0 0;pcenter;
rem form end;
)

NB. =========================================================
create=: 3 : 0
wd DEMO
wd^:(-.IFJ6) 'pshow;pshow sw_hide'
grid=: ''conew'jzgrid'
show__grid 'celldata hdrrow hdrcol'
gplot=: conew 'jzplot'
PForm__gplot=: 'demo'
PFormhwnd__gplot=: wd 'qhwndp'
PId__gplot=: 'gplot'
wd 'pshow;'
evtloop^:(-.IFJ6)''
)

NB. =========================================================
destroy=: 3 : 0
wd'pclose'
destroy__grid''
destroy__gplot''
codestroy''
)

NB. =========================================================
demo_gplot_paint=: 3 : 0
'sbar' plot__gplot CELLDATA__grid
)

NB. =========================================================
demo_cancel=: demo_close=: destroy

NB. =========================================================
grid_gridhandler=: 3 : 0
if. y -: 'changed' do. demo_gplot_paint'' end.
1
)

NB. =========================================================
CELLDATA=: 3 + ?3 5$17
HDRROW=: HDRCOL=: 1
create''

Chess Grid

Scripts/Chess Grid uses J6/Grid control and Unicode to generate dynamic chess board.