Dan Dan - 1 year ago 67
Java Question

How can I programmatically 'set' all parameters of a Dialog, such as 'dialog title', 'ok text', etc., without having string literals as parameters?

This is more of a programming style-related question rather than a functionality-related question.

All examples I see for creating a CodenameOne dialog box appear something like this:

import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Button;
// Create a button
Button myButton = new Button("Click Me");

// Create an action listener for the button
myButton.addActionListener((e) -> Dialog.show("Dialog title", "Dialog text", "OK", null));

The above
command has 4 parameters inside. However, if possible, I wish to remove them, and 'set' them, as per the style below:

Dialog myDialog = new Dialog(); // Empty parameter list
myDialog.setTitle("Dialog Title"); // 1st parameter
myDialog.set??? // How can I 'set' "Dialog text" (2nd parameter) like I just did with Dialog Title?
myDialog.set??? // How can I 'set' "OK" (3rd parameter) like I just did with Dialog Title?
myDialog.set??? // How can I 'set' null (4th parameter) like I just did with Dialog Title?

// ...and then just do something like ...

myButton.addActionListener((e) -> myDialog.show()); // empty parameter list

How can I 'set' the dialog text, the ok button text, etc. in the same manner that I could use
? Is there a corresponding setter for each of those other parameters?

Any assistance would be greatly appreciated.

Answer Source

There are no setters for the parameters used in show (String title, String text, String okText, String cancelText) except for setTitle(). You can search in the official docs, except for the method show, there is no other references to text, okText or cancelText. You must put all the parameters for that overload of the method show, that's how the API is made.

If you want to do extra work to get more setters, you could do like @James H suggested (I don't know why I didn't think of that at first): create a derived class. I'm pretty sure that the parameters of show () don't have any corresponding field, so in the derived class (e.g. OkCancelDialog) you could add the fields text, okText and cancelText, create a default constructor and a parameterized constructor and the getters/setters for those fields, plus a show method that uses those fields. Then you just nees to change Dialog with OkCancelDialog.

public class OkCancelDialog extends Dialog {
    private String text=null, okText=null, cancelText=null;

    public OkCancelDialog (String text, String okText, String cancelText){
        super ();
        this.text = text;
        this.okText = okText;
        this.cancelText = cancelText;

    public void setText (String text) { this.text = text; }
    public void setOkText (String okText) { this.okText = okText; }
    public void setCancelText (String cancelText) {this.cancelText = cancelText; } 

    public boolean show () { 
        return super.show (this.title, this.text, this.okText, this.cancelText);