I am new with Squeak Smalltalk. How I can catch a button click event and execute some code when the button is clicked.
I tried this code but it doesn't work!
I created a new Button Class:
SimpleButtonMorph subclass: #ButtonTest
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'test'
Inside this class I write this method:
handleMouseDown: event
^ true.
I want to use the button inside another Morph, so i created new Morph class:
RectangleMorph subclass: #RectangleMorphTest
instanceVariableNames: 'textField button stringLabel mouseAction'
classVariableNames: ''
poolDictionaries: ''
category: 'test'
and inside Initialize method i compose buttonMorph inside RectangleMorph, The initialize method for rectangleMorph:
initialize
super initialize.
self bounds: ((0@0) extent: (400@400)).
self color: Color gray.
textField := TextFieldMorph new.
textField color: Color lightYellow.
textField contents: 'text'.
textField bounds: ((25@25) extent: (300@75)).
button := ButtonTest new.
button borderWidth: 2.
button bounds: ((150@150) extent: (200@200)).
button label: 'print text'.
button target: button.
button mouseDownOn: #yellowButtonPressed event: [Transcript show: 'hello'].
stringLabel := StringMorph new contents: 'This is a string'.
stringLabel bounds: ((150@180) extent: (200@200)).
self addMorph: textField.
self addMorph: button.
self addMorph: stringLabel.
Question
I tried to handle a Button event inside RectangleMorph, but it didn't work. So how I can handle a Button click event inside the RectangleMorph?
True
(using an upper-case T), you refer to the subclass ofBoolean
that implements truthy behavior. Instead, you likely want to refer totrue
(using a lower-case t), the single instance ofTrue
. – Chil