### Inline Java UI Dialogs

Various examples of creating JavaFX8 dialogs. These dialogs apply as from JavaFX 8 update 40.
'Class module
'
'Examples to test (few examples only)
'joInlineLava.RunMethod("InformationDialog", Array("My Title", "My header", "MyContent"))
'Log(joInlineLava.RunMethod("ConfirmationDialog", Array("My Title", "My header", "MyContent")))
'Log(joInlineLava.RunMethod("TextInputDialog", Array("My Title", "My header", "MyLabel", "My Text")))
'Dim l As List : l.Initialize : l.AddAll(Array As String("a","b","c","d","e"))
'Log(joInlineLava.RunMethod("ChoiceDialog", Array("My Title", "My header", "MyLabel", l)))
'
`Private Sub Class_Globals
Private joInlineLava As JavaObject
Public Version As String = "0.5"
End Sub`
`Public Sub Initialize
joInlineLava = Me
End Sub`
'Show an Information Dialog
'Parameter Title, Header and Content as strings
'Returns None
`Public Sub InformationDialog(Title As String , Header As String, Content As String)
joInlineLava.RunMethod("InformationDialog", Array(Title, Header, Content))
End Sub`
'Show an Extended Dialog which has a expandable Textarea. This textarea is by default not expandedn.
'Parameter Title, Header, Content and Extended Content as strings
'Returns None
`Public Sub ExtendedDialog(Title As String , Header As String, Content As String, ContentExtended As String)
joInlineLava.RunMethod("ExtendedDialog", Array(Title, Header, Content, ContentExtended))
End Sub`
'Show an Warning Dialog
'Parameter Title, Header and Content as strings
'Returns None
`Public Sub WarningDialog(Title As String , Header As String, Content As String)
joInlineLava.RunMethod("WarningDialog", Array(Title, Header, Content))
End Sub`
'Show an Error Dialog
'Parameter Title, Header and Content as strings
'Returns None
`Public Sub ErrorDialog(Title As String , Header As String, Content As String)
joInlineLava.RunMethod("ErrorDialog", Array(Title, Header, Content))
End Sub`
'Show a Confirmation Dialog
'Parameter Title, Header and Content as strings
'Returns a Boolean = True for OK or False forCancel
`Public Sub ConfirmationDialog(Title As String , Header As String, Content As String) As Boolean
Return joInlineLava.RunMethod("ConfirmationDialog", Array(Title, Header, Content))
End Sub`
'Show a Yes No Cancel Dialog
'Parameter Title, Header and Content as strings
'Returns an Int = 1 for Yes, 0 for No, -1 for Cancel
`Public Sub YesNoCancelDialog(Title As String , Header As String, Content As String) As Int
Return joInlineLava.RunMethod("YesNoCancelDialog", Array(Title, Header, Content))
End Sub`
'Show a TextInput Dialog
'Parameter Title, Header, Input Label and the default text as strings
'Returns the text entered as string or an empty string
`Public Sub TextInputDialog(Title As String , Header As String, Label As String, Text As String) As String
Return joInlineLava.RunMethod("TextInputDialog", Array(Title, Header, Label, Text))
End Sub`
'Show a Choice Dialog
'Parameter Title, Header, Label for the Choices as strings. Choices as a List
'Returns the selection as string or an empty string
`Public Sub ChoiceDialog(Title As String , Header As String, Label As String, Choices As List) As String
Return joInlineLava.RunMethod("ChoiceDialog", Array(Title, Header, Label, Choices))
End Sub`
'Show a Login Dialog
'Parameter Title, Header as strings
'Returns a map username:username, password:password
'If cancel selected, the returned map will not be initialized.
`Public Sub LoginDialog(Title As String , Header As String) As Map
Return joInlineLava.RunMethod("LoginDialog", Array(Title, Header))
End Sub`
'Show a Login Dialog with Icon and default UserName
'Parameter Title, Header and default Username as strings
'Returns a map username:username, password:password
'If cancel selected, the returned map will not be initialized.
'Note: Ensure the icon file login.png is added to the B4J project.
`Public Sub LoginDialog2(Title As String , Header As String, UserName As String) As Map
Return joInlineLava.RunMethod("LoginDialog2", Array(Title, Header, UserName))
End Sub`
'Show DidYouKnow Dialog with Icon. The content is shown in am expanded TextArea.
'Parameter Title, Header and the Did You Know Content as strings.
'Returns None
'Note: Ensure the icon file dyk.png is added to the B4J project.
`Public Sub DidYouKnowDialog(Title As String , Header As String, Content As String) As Map
Return joInlineLava.RunMethod("DidYouKnowDialog", Array(Title, Header, Content))
End Sub`
'Show an Exception Dialog which has a expandable Textarea. This textarea is by default expanded.
'Parameter Title, Header, Content as strings. Ex as Exception
'Returns None
'Note: As the content, an option is to use LastException.Message
`Public Sub ExceptionDialog(Title As String , Header As String, Content As String, ex As Exception)
joInlineLava.RunMethod("ExceptionDialog", Array(Title, Header, Content, ex))
End Sub`
if JAVA
//The imports
import anywheresoftware.b4a.objects.collections.Map;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.scene.layout.Priority;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.util.Pair;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Optional;
import java.util.*;
public static void InformationDialog(String mtitle, String mheader, String mcontent) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
alert.initStyle(StageStyle.UTILITY);
alert.showAndWait();
};
public static void WarningDialog(String mtitle, String mheader, String mcontent) {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
alert.initStyle(StageStyle.UTILITY);
alert.showAndWait();
};
public static void ErrorDialog(String mtitle, String mheader, String mcontent) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
alert.initStyle(StageStyle.UTILITY);
alert.showAndWait();
};
public static boolean ConfirmationDialog(String mtitle, String mheader, String mcontent) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
alert.initStyle(StageStyle.UTILITY);
Optional result = alert.showAndWait();
if (result.get() == ButtonType.OK){
//BA.Log("OK selected");
return true;
} else {
//BA.Log("Cancel selected");
return false;
}
};
public static int YesNoCancelDialog(String mtitle, String mheader, String mcontent) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
alert.initStyle(StageStyle.UTILITY);
ButtonType buttonTypeYes = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType buttonTypeNo = new ButtonType("No", ButtonBar.ButtonData.NO);
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(buttonTypeYes, buttonTypeNo, buttonTypeCancel);
Optional result = alert.showAndWait();
//BA.Log("YesNoCancel Result = " + result.get());
if (result.get() == buttonTypeYes){
//BA.Log("YES selected");
return 1;
} else if (result.get() == buttonTypeNo) {
//BA.Log("NO selected");
return 0;
} else {
//BA.Log("CANCEL selected");
return -1;
}
};
public static String TextInputDialog(String mtitle, String mheader, String mlabel, String mtext) {
TextInputDialog dialog = new TextInputDialog(mtext);
dialog.setTitle(mtitle);
dialog.setHeaderText(mheader);
dialog.setContentText(mlabel);
dialog.initStyle(StageStyle.UTILITY);
Optional result = dialog.showAndWait();
if (result.isPresent()){
//BA.Log("TextInput Result:" + result.get());
return result.get();
}
else {
return "";
}
};
public static String ChoiceDialog(String mtitle, String mheader, String mlabel, List choices) {
ChoiceDialog dialog = new ChoiceDialog(null, choices);
dialog.setTitle(mtitle);
dialog.setHeaderText(mheader);
dialog.setContentText(mlabel);
dialog.initStyle(StageStyle.UTILITY);
Optional result = dialog.showAndWait();
if (result.isPresent()){
//BA.Log("ChoiceDialog Result:" + result.get());
return result.get();
}
else {
return "";
}
};
public static void ExtendedDialog(String mtitle, String mheader, String mcontent, String mcontentextended) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
TextArea textArea = new TextArea(mcontentextended);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(textArea, 0, 0);
alert.getDialogPane().setExpandableContent(expContent);
alert.showAndWait();
}
//Login Dialog
//Parameter: Title and Header information
//Result: Map with 2 entries holding username:username, password:password OR if cancelled a non initialized map
public static Map.MyMap LoginDialog(String mtitle, String mheader) {
//Create a new username and password map
Map.MyMap usernamepasswordmap = new Map.MyMap();
//Create a new dialog and set title and header
Dialog dialog = new Dialog();
dialog.setTitle(mtitle);
dialog.setHeaderText(mheader);
//Set the button types Login and Cancel
ButtonType buttonTypeLogin = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(buttonTypeLogin, buttonTypeCancel);
//Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
//username.setText(musername); NOT USED
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
//Enable/Disable login button depending on whether a username was entered = triggerd by textfield listener
final Node loginButton = dialog.getDialogPane().lookupButton(buttonTypeLogin);
loginButton.setDisable(true);
// Listen for TextField text changes
username.textProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, String oldValue, String newValue) {
loginButton.setDisable(newValue.trim().isEmpty());
}
});
dialog.getDialogPane().setContent(grid);
//Handle the dialog result based on the button clicked
Optional result = dialog.showAndWait();
if (result.get() == buttonTypeLogin){
//BA.Log("Login selected:" + username.getText() + "/" + password.getText());
//Create a new map with 2 entries holding username:username, password:password
Map m = new Map();
m.Initialize();
m.Put("username", username.getText());
m.Put("password", password.getText());
//Put the map to the B4J mymap
usernamepasswordmap.putAll(m.getObject());
return usernamepasswordmap;
} else {
//BA.Log("Cancel selected");
//Returns a non initialized B4J mymap
return null;
}
}
//Login Dialog2
//Parameter: Title and Header information, Default Username (can be empty)
//Result: Map with 2 entries holding username:username, password:password OR if cancelled a non initialized map
//In addition to the LoginDialog:
//* UserName as a Parameter
//* Shows in addition to the LoginDialog an icon. IMPORTANT = This icon "login.png" must be placed in the project files folder and added to the B4J IDE Files Manager.
public static Map.MyMap LoginDialog2(String mtitle, String mheader, String musername) {
//Create the fx variable as required for displaying the icon
anywheresoftware.b4j.objects.JFX fx = new anywheresoftware.b4j.objects.JFX();
//Create a new username and password map
Map.MyMap usernamepasswordmap = new Map.MyMap();
//Create a new dialog and set title and header
Dialog dialog = new Dialog();
dialog.setTitle(mtitle);
dialog.setHeaderText(mheader);
//Set the icon (must be included in the B4J project files list (=add to tab files)).
try {
dialog.setGraphic(new ImageView((javafx.scene.image.Image)(fx.LoadImage(anywheresoftware.b4a.keywords.Common.File.getDirAssets(),"login.png").getObject())));
} catch (Exception e) {
throw new RuntimeException(e);
};
//Set the button types Login and Cancel
ButtonType buttonTypeLogin = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(buttonTypeLogin, buttonTypeCancel);
//Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField username = new TextField();
username.setPromptText("Username");
username.setText(musername);
PasswordField password = new PasswordField();
password.setPromptText("Password");
grid.add(new Label("Username:"), 0, 0);
grid.add(username, 1, 0);
grid.add(new Label("Password:"), 0, 1);
grid.add(password, 1, 1);
//Enable/Disable login button depending on whether a username was entered = triggerd by textfield listener
final Node loginButton = dialog.getDialogPane().lookupButton(buttonTypeLogin);
loginButton.setDisable(username.getText().trim().isEmpty());
//Listen for UserName TextField text changes
username.textProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, String oldValue, String newValue) {
loginButton.setDisable(newValue.trim().isEmpty());
}
});
dialog.getDialogPane().setContent(grid);
//Handle the dialog result based on the button clicked Login or Cancel
Optional result = dialog.showAndWait();
if (result.get() == buttonTypeLogin){
//BA.Log("Login selected:" + username.getText() + "/" + password.getText());
//Create a new map with 2 entries holding username:username, password:password
Map m = new Map();
m.Initialize();
m.Put("username", username.getText());
m.Put("password", password.getText());
//Put the map to the B4J mymap
usernamepasswordmap.putAll(m.getObject());
return usernamepasswordmap;
} else {
//BA.Log("Cancel selected");
//Returns a non initialized B4J mymap
return null;
}
}
//Did You Know Dialog
//Parameter: Title and Header information, Content which can be shown in an extended way
//Result None
//IMPORTANT = This icon "dyk.png" must be placed in the project files folder and added to the B4J IDE Files Manager.
//Reference: http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/DialogPane.html
public static void DidYouKnowDialog(String mtitle, String mheader, String mcontent) {
//Create the fx variable as required for displaying the icon
anywheresoftware.b4j.objects.JFX fx = new anywheresoftware.b4j.objects.JFX();
//Create the dialog
Dialog dialog = new Dialog();
dialog.setTitle(mtitle);
dialog.setHeaderText(mheader);
//Set the icon (must be included in the B4J project files list (=add to tab files)).
try {
dialog.setGraphic(new ImageView((javafx.scene.image.Image)(fx.LoadImage(anywheresoftware.b4a.keywords.Common.File.getDirAssets(),"dyk.png").getObject())));
} catch (Exception e) {
throw new RuntimeException(e);
};
//Add an OK button to close the dialog. Standard action is closing by setting OK_DONE.
ButtonType buttonTypeOK = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(buttonTypeOK);
//Create the textarea showing the extended content.
TextArea textArea = new TextArea(mcontent);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(textArea, 0, 0);
//Set the expandable content and show
dialog.getDialogPane().setExpandableContent(expContent);
dialog.getDialogPane().setExpanded(true);
//Show the dialog till closed via OK button
Optional result = dialog.showAndWait();
//BA.Log("DYK Dialog Result = " + result.get());
}
//Exception Dialog = Show the content of the exception stacktrace
//Parameter: Title and Header information, Content, Exception
//Result None
public static void ExceptionDialog(String mtitle, String mheader, String mcontent, Exception e) {
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle(mtitle);
alert.setHeaderText(mheader);
alert.setContentText(mcontent);
//Build the exception string
//Create new StringWriter object
StringWriter sw = new StringWriter();
//Create PrintWriter for StringWriter
PrintWriter pw = new PrintWriter(sw);
//Print the stacktrace to PrintWriter created
e.printStackTrace(pw);
//Use toString method to get stacktrace to String from StringWriter object
TextArea textArea = new TextArea(sw.toString());
//The textarea can not be added and has no wrap (makes it better readable)
textArea.setEditable(false);
textArea.setWrapText(false);
textArea.setMaxWidth(Double.MAX_VALUE);
textArea.setMaxHeight(Double.MAX_VALUE);
GridPane.setVgrow(textArea, Priority.ALWAYS);
GridPane.setHgrow(textArea, Priority.ALWAYS);
GridPane expContent = new GridPane();
expContent.setMaxWidth(Double.MAX_VALUE);
expContent.add(textArea, 0, 0);
//Set the expandable content and show
alert.getDialogPane().setExpandableContent(expContent);
alert.getDialogPane().setExpanded(true);
alert.showAndWait();
}
end if

Changed: 2018-06-07 09:14, Category: Inline Java, File: T6C201M1481.htm, ID: 1481