How to display various kinds of dialogs from a form.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on programming forms in the Programmer's Guide |
The following code shows how to display a message box in response to a user clicking on a button.:
private void handle_btnShowMessage_actionPerformed(ActionEvent evt) { // Reset label values lblResultValue.setText(""); lblMethod.setText(""); agDialog.showMessage("Example message", "Display your message here."); lblMethod.setText("public void showMessage(String message)" + "\npublic void showMessage(String caption, String message)"); }
agDialog.showMessage()
and providing the dialog's title and message as parameters.
setText()
and provide the two method variants as a parameter.
The following code shows how to display an OK/Cancel dialog in response to the user clicking the OK/Cancel button. The code gets the user's response from the dialog box and sets the form's RESULT label appropriately.
private void handle_btnOKCancel_actionPerformed(ActionEvent evt) { // Reset label values lblResultValue.setText(""); lblMethod.setText(""); boolean response = agDialog.showMessageOKCancel("OK or Cancel", "Question which requires confirmation or cancellation."); if (response) lblResultValue.setText("true"); else lblResultValue.setText("false"); lblMethod.setText("public boolean showMessageOKCancel(String caption, String message)" + "\n\nReturns 'true' if OK, otherwise 'false'."); }
agDialog.showMessageOKCancel()
displays a dialog with OK and Cancel buttons. You specify the dialog's title and text (usually a question or statement requiring confirmation) as parameters. agDialog.showMessageOKCancel()
returns true if the user clicks the OK button, and returns false if the user clicked the Cancel button.
agDialog.showMessageOKCancel()
returns true, that is, the user clicked OK, lblResultValue.setText()
sets the RESULTS label to the text true.
agDialog.showMessageOKCancel()
returns false, that is, the user clicked Cancel, lblResultValue.setText()
sets the RESULTS label to the text false.
The following code shows how to display a Yes/No dialog in response to the user clicking the Yes/No button, making it the current focus. The code gets the user's response from the dialog box and sets the form's RESULT label appropriately.
private void handle_btnYesNo_actionPerformed(ActionEvent evt) { // Reset label values lblResultValue.setText(""); lblMethod.setText(""); boolean response; response = agDialog.showMessageYesNo("OK or Cancel", "Question which requires a Yes or No answer."); if (response) lblResultValue.setText("true"); else lblResultValue.setText("false"); lblMethod.setText("public boolean showMessageYesNo(String caption, String message)" + "\n\nReturns 'true' if Yes, otherwise 'false'."); }
agDialog.showMessageYesNo()
displays a dialog with Yes and No buttons. You specify the dialog's title and text (usually a question requiring a yes or no answer) as parameters. agDialog.showMessageYesNo()
returns true if the user clicks the Yes button, and returns false if the user clicked the No button. Note that this method does not create a Cancel button. To create a dialog with Yes, No, and Cancel buttons, see
Displaying a Yes/No/Cancel dialog.
agDialog.showMessageYesNo()
returns true, that is, the user clicked Yes, lblResultValue.setText()
sets the RESULTS label to the text true.
agDialog.showMessageYesNo()
returns false, that is, the user clicked No, lblResultValue.setText()
sets the RESULTS label to the text false.
The following code shows how to display a Yes/No/Cancel dialog in response to the user clicking the Yes/No/Cancel button, making it the current focus. The code gets the user's response from the dialog box and sets the form's RESULT label appropriately.
private void handle_btnYesNoCancel_actionPerformed(ActionEvent evt) { // Reset labels lblResultValue.setText(""); lblMethod.setText(""); Boolean response = agDialog.showMessageYesNoCancel("Yes, No or Cancel", "Question which requires a Yes or No answer, or cancellation."); if (response == null) lblResultValue.setText("null"); else if (response.booleanValue()) lblResultValue.setText("true"); else lblResultValue.setText("false"); lblMethod.setText("public Boolean showMessageYesNoCancel(String caption, String message)" + "\n\nReturns 'null' if Cancel, 'true' if Yes, otherwise 'false'."); }
agDialog.showMessageYesNoCancel()
displays a dialog with Yes, No, and Cancel buttons. You specify the dialog's title and text (usually a question requiring a yes or no answer) as parameters. agDialog.showMessageYesNo()
returns true if the user clicks the Yes button, returns false if the user clicked the No button, and returns null if the user clicks the Cancel button.
agDialog.showMessageYesNoCancel()
returns null, the code uses lblResultValue.setText("null")
, which sets the RESULTS label on the form to an empty string. That is, there is no text in it.
The following code attempts to create a substring (beginning at index=1) from a null string. Since this attempt results in an error, the system throws an exception. In the catch block, the exception handles the exception by displaying the error message in a dialog. This dialog has Error as its title and contains an OK button, which the user clicks to close the dialog.
private void handle_btnDisplayError_actionPerformed(ActionEvent evt) { Reset labels lblResultValue.setText(""); lblMethod.setText(""); try { String s = "".substring(1); } catch (Exception e) { agDialog.displayError(e); } }
substring()
creates a substring beginning at the specified index and ending at the last element of the string. Since the String that calls this method is null, an error occurs and the method throws an exception.
catch
block traps and processes the exception by calling agDialog.displayError()
, which displays the exception's message in a dialog box.
The following code displays a form as a modal dialog box. The form it displays is named dlgExample, which is stored in the Examples3_Java database. The code passes the contents of the record shown in the DATA RETURNED FROM SHOW FORM DIALOG area to the dlgExample form. The user can use the dialog to change either the customer name or the order date of the displayed record. The code then passes the updated values in the dialog to the form and updates the record in the database.
private void handle_btnShowFormDialog_actionPerformed(ActionEvent evt) { Reset Label values lblResultValue.setText(""); lblMethod.setText(""); Hashtable example = new Hashtable(); Object results[] = new Object[3]; example.put("sParam", CustomerName.getText()); example.put("iParam", Integer.toString(CustomerID.getValue())); example.put("dtParam", OrderDate.getValue()); try { results = (Object[]) agDialog.showFormDialog ("Example", "dlgExample", example); } catch (java.io.IOException e) { agDialog.displayError(e); return; } catch (AgoApiException e) { agDialog.displayError(e); return; } lblResultValue.setText("" + results[0]); if (!((String)results[0]).equalsIgnoreCase("***cancel***")) { CustomerID.setValue(((Integer) results[1]).intValue()); OrderDate.setValue((java.sql.Date) results[2]); try { agData.updateRows(); CustomerName.setText((String) results[0]); } catch (AgoApiException e) { agDialog.displayError(e); } } }
put()
method.
showFormDialog()
and supplying a dialog title, the name of the form, and the Hashtable.
CustomerID.setValue()
sets the form's Customer ID field to the ID that corresponds to the user's Customer selection in the dialog. For example, if the user selected SilverStream in the dialog's Customer field, the form's Customer ID field is set to 2.
OrderDate.setValue()
sets the form's Order Date field to the date that the user has entered in the Order Date text field in the dialog. For example, if the user entered Wednesday, September 9, 1998 in the dialog's Order Date field, the form's Order Date field is set to the same.
agData.updateRows()
updates the record in the database.
CustomerName.setText()
sets the form's Customer Name field to the company name that corresponds to the updated Customer ID. For example, if the Customer ID field is set to 2, then CustomerName.setText()
sets Customer Name to SilverStream Software, Inc.