For the latest version of JMP Help, visit JMP.com/help.


Scripting Guide > Display Trees > Construct Custom Windows > Interact with a Closing Window
Publication date: 11/29/2021

Interact with a Closing Window

A user can close a window by clicking the OK or Cancel button or by clicking the red Close button in the top corner of the window. A message can also be sent to close the window. On Validate and On Close expressions determine what happens you interact with the window.

On Validate evaluates its expression when the OK button is clicked. The expression is not evaluated for any other button click or action. If the expression evaluates as true, the window closes. If the expression evaluates as false, the window is not closed. The script for an OK button runs when you click the button, no matter what the On Validate expression does.

On Close evaluates its expression right before the window closes. The On Close return value is like the On Validate return value and can prevent the window from closing. However, the best practice for On Close is to always return 1 so that you do not accidentally create a window that cannot be closed.

If the window closes, On Close is evaluated. On Close is not evaluated if the On Validate expression returns false, because the window is not closed.

Tips:

On Close works for modal or non-modal windows. On Validate works only for modal windows.

Do not use On Close for validation because it does not distinguish between OK and Cancel. Use On Close to clean up resources (such as invisible data tables) because it always runs when the window closes.

Do not use On Validate to clean up resources. Use On Close instead or in addition to On Validate, because On Validate does not run for the Cancel button or red Close button.

On Close() Example

Suppose that a non-modal window is a report window, and the resource that needs cleaning up is an invisible data table. The On Close script closes the data table. This is good because the user might not know that the data table is open and has no easy way to close it.

dt = New Table( "Untitled", << Invisible,
    New Column( "x", Set Values( [1, 2, 3, 4] ) ),
    New Column( "y", Set Values( [4, 2, 3, 1] ) )
);
 
dt << Bivariate(
    y( dt:y ), x( dt:x ), <<On Close( Close( dt, "nosave" ) )
);

Before the script is run, no data tables are open. The Untitled invisible table is created and displayed in the Home Window and in the Data Tables list above the script. Then the graph is created. After you close the report window, the invisible table is closed by On Close.

On Validate() Example

Suppose that a modal window contains a question that the user must answer before closing the window. The following example shows how to validate the input when the user closes the window.

win = New Window( "Validate Example",
	<<Modal,
	<<Return Result,
	<<On Validate(
		If( (!Is Missing( variablebox << Get ) & 40 <= variablebox << Get <= 50),
			( 1 ), // else the user did not enter the correct answer
			tb << Set Text(
				Match( Random Integer( 1, 3 ),
					1, "Are you sure?",
					2, "Guess again.",
					3, "Please read the question."
				)
			);
			( 0 );
		)
	),
	Text Box( "I'm thinking of a number between 40 and 50. What's your guess?" ),
	variablebox = Number Edit Box( . ),
	H List Box( Button Box( "OK" ), Button Box( "Cancel" ) ),
	tb = Text Box( "" )
);
 
New Window( "Modal Window",
	<<Modal,
	Text Box(
		If(
 

// user clicked Cancel or the red Close button

			win["Button"] == -1, "Try again when you feel better.",
			win["variablebox"] == 42, "Good job.", // user entered 42
			"Take the test again." // user did not enter 42 and clicked OK
		)
	)
);
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).